#!/bin/sh

# This is a brute force script that updates the content tree with CVS data
# and prints a fairly verbose report about what local files have been
# modified. It also performs some really basic permissions maintenance
# to accomodate the numerous methods people want to use to update content.

DIR=/exp/public/public/messaging_6/docs
REPORT=cvs_status.html

# No User Servicable Parts Inside.
# No Touching

cd $DIR

# This reirects STDOUT to a file.
exec 1> $REPORT

I=0
while [ $I -ne 1 ]
  do
    echo \
	"<html>
	<body>
	<p><i>All your mods are belong to us.</i></p>
	<p>CVS Status of doc root as of `date +"%H:%M:%S %d:%m:%Y"`<br>
	This is being determined by massaging the output of a
	<tt>cvs -A -d update</tt>.</p>

	<pre>"

	cvs update -A -d | egrep -v "(cvs_status)"

    echo "</pre>"
 
    #   Perform magic to determine number of modified files
    FILES=`egrep "^[?UPMCRA] " $REPORT | wc -l | awk '{print $1}'`

    if [ "$FILES" -lt 1 ]
    then
	echo "<p>The tree is in sync with CVS. Yay!</p>"
	I=1
    else
	echo \
	    "<p>If a document you just added to the doc tree has a question mark
	    next to it then it is not yet in the CVS tree. See
	    <a href=odds_ends/adding_content.html>this page</a> for information about how to
	    add the document(s) to CVS. You needn't worry about other letters -
	    I'll take care of them.</p>"

	    I=1
    fi
done

# My groups are slightly messed up. What is "wheel" on my machine is "staff"
# elsewhere. Really. Don't mess with it.

# Making sure that "nobody" and "staff" own the files ensures that people
# that publish via Composer and NES can make mods
if chown -R nobody:wheel *
then
  echo \
    "<p>As part of the update I've changed the ownership of all the
    files to <code>nobody</code>, and the group to <code>staff</code>.</p>"
else
  echo \
    "<p>Something broke during the update, however, and I was unable to
    chown the files. This needs attention.</p>"
fi

# Enabling the "write" bit makes sure that whatever else messed up method
# people want to modify the docs won't be blocked
if chmod -R a+w *
then
  echo \
    "<p>I've also enabled the global \"write\" bit on all files to allow
    for the various methods of content updating.</p>"
else
  echo \
    "<p>I wasn't able to enable the \"write\" bit on some files.
    That sucks.</p>"
fi

# I don't want .shtml docs to be modified without some amount of effort.
# It's too easy to mess them up if care isn't taken.
if chmod -R a-w project/master.shtml
then
  echo \
    "<p>To keep accidents from happening I've disabled the \"write\" bit
    for the master.shtml document.</p>"
else
  echo \
    "<p>I was unable to disable the \"write\" bit on master.shtml.
    I feel very not powerfull.</p>"
fi

# This closes the STOUT redirection
exec 1>&-
