#! /bin/sh
# $Id$
#
# This script is installed as a cron job to automatically update the
# Docutils web site whenever the CVS files change.  Any .html document
# with a corresponding .txt file is regenerated whenever the .txt
# changes.
#
# Options:
#   -e    Access CVS as current user ("ext" instead of "pserver"; requires
#         authentication).
#   -p    Run from the project directory.
#   -t    Run the script in trace mode ("set -o xtrace").
#   -u    Regenerate .html unconditionally.

projdir=/home/groups/d/do/docutils
project=docutils
dest=$projdir/htdocs
tmp=$projdir/tmp/$project
pylib=$projdir/lib/python
lib=$pylib/$project
root=

export CVS_RSH=ssh
export CVSROOT=:pserver:anonymous@cvs1:/cvsroot/$project

trace=0
unconditional=0

while getopts eptu opt
do
    case $opt in
        e)  root="-d:ext:${USER}@cvs1:/cvsroot/docutils" ;;
        p)  export PYTHONPATH=$pylib:$lib;;
        t)  trace=1;;
        u)  unconditional=1;;
        \?) exit 2;;
    esac
done
shift `expr $OPTIND - 1`

if [ $trace -eq 1 ] ; then
    set -o xtrace
fi

# prep the tmp area
rm -rf $tmp 2>&1 > /dev/null
mkdir -p $tmp 2>&1 > /dev/null
cd $tmp

# gather the materials
cvs -Q -z3 $root export -rHEAD $project sandbox web

if [ $? -ne 0 ] ; then
	exit 1
fi

# remove junk
find -name .cvsignore | xargs rm

# ensure executable bits are set
chmod +x $project/*.py
chmod +x $project/tools/*.py
chmod +x $project/test/alltests.py
find $project/test -name 'test_*.py' | xargs chmod +x

# create the snapshots
tar -czf $project-snapshot.tgz $project
tar -czf $project-sandbox-snapshot.tgz sandbox
tar -czf $project-web-snapshot.tgz web
( cd sandbox/gschwant ; tar -czf ../../docfactory-snapshot.tgz docfactory )

# plant the snapshots
mv *snapshot.tgz $dest

# update htdocs
cd $project
cp -ru . $dest
cd ..
cp -ru sandbox $dest
cd web
cp -ru . $dest

# destroy the evidence
cd
rm -rf $tmp

# update library area
cd $lib
cvs -Q $root update 2>&1 > /dev/null

# update HTML docs
cd $dest/tools
for htmlfile in `find .. -name '*.html'` ; do
    dir=`dirname $htmlfile`
    base=`basename $htmlfile .html`
    txtfile=$dir/$base.txt
    if [ -e $txtfile ] ; then
        if [ $unconditional -eq 1 -o $txtfile -nt $htmlfile ] ; then
            if [ "${base:0:4}" == "pep-" ] ; then
                echo "$txtfile (PEP)"
                ~/bin/python $lib/tools/pep2html.py $txtfile
            else
                echo $txtfile
                ~/bin/python $lib/tools/html.py --config=$dir/docutils.conf $txtfile $htmlfile
            fi
        fi
    fi
done
