#! /bin/bash
# $Id: puzzler-update-local 658 2019-10-03 01:44:23Z goodger $
#
# This script updates the Polyform Puzzler web site.  Any .html document with
# a corresponding .txt file is regenerated if the .txt file has changed.
#
# Options:
#   -f    Provide feedback at start and end of program run.
#   -i    Do initial setup only (create directories, SVN checkout, HTML build).
#   -q    Run quietly.
#   -s    Skip synchronization with SourceForge.
#   -t    Run the script in trace mode ("set -o xtrace").
#   -u    Regenerate .html unconditionally.
#   -v    Run verbosely.

# exit on error
set -e

# make all newly created files group writeable
umask 002

# URL for SVN project checkout:
svnurl=https://puzzler.svn.sourceforge.net/svnroot/puzzler/trunk

# htdocs directory on SF.net
remoteproject=/home/groups/p/pu/puzzler
remotehtdocs=$remoteproject/htdocs

projdir=/home/groups/p/pu/puzzler
dest=$projdir/htdocs

function update_htdocs_source {
    cd working
    cp -ruf puzzler/* ../htdocs
    cp -ruf misc solutions ../htdocs
    cp -ruf web/* web/.[^.]* ../htdocs
    # remove SVN crud
    cd ../htdocs
    find -name .svn -type d -exec rm -rf {} \; -prune
    # ensure proper permissions are set
    find . -type f -print0 | xargs -0 chmod ug+rw 2> /dev/null || true
    find . -type d -print0 | xargs -0 chmod ug+rwxs 2> /dev/null || true
    cd ..
}

feedback=0
quiet=0
setup=0
skipsync=0
trace=0
unconditional=0
verbose=0

while getopts fiqstuv opt
do
    case $opt in
        f)  feedback=1;;
        i)  setup=1;;
        q)  quiet=1;;
        s)  skipsync=1;;
        t)  trace=1;;
        u)  unconditional=1;;
        v)  verbose=1
            feedback=1;;
        \?) exit 2;;
    esac
done
shift `expr $OPTIND - 1`

if [ $feedback -eq 1 -o $setup -eq 1 ] ; then
    echo "Starting puzzler-update-local run (`date +%F\ %T`)..."
fi

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

if [ $setup -eq 1 ] ; then
    echo 'Initial setup...'
    if [ ! -d htdocs ] ; then
        mkdir htdocs
    fi
    if [ -d working ] ; then
        cd working
        svn update
        cd ..
    else
        svn checkout $svnurl working
    fi
    update_htdocs_source
    cd htdocs
    python2 ~/projects/docutils/docutils/tools/buildhtml.py
    echo '...puzzler-update-local initial setup done.'
    exit
fi

# gather the materials.
# update the svn checkout:
cd working
if [ $quiet -eq 1 ] ; then
    svn update --quiet
else
    svn update
fi

# create info.py
rev=`svn info | grep Revision: | cut -d' ' -f2`
echo "revision = 'r$rev'" > puzzler/puzzler/info.py

# ensure proper permissions are set
find . -type f -print0 | xargs -0 chmod ug+rw 2> /dev/null || true
find . -type d -print0 | xargs -0 chmod ug+rwxs 2> /dev/null || true

# create the snapshots
exclude='--exclude=.svn'
tar -cz $exclude -f puzzler-snapshot.tgz puzzler
tar -cz $exclude -f solutions-snapshot.tgz solutions

cd ..

# plant the snapshots
mv -f working/*snapshot.tgz htdocs

update_htdocs_source

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

cd htdocs
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 [ $quiet -eq 0 ] ; then
                echo $txtfile
            fi
            python2 ~/projects/docutils/docutils/tools/rst2html.py --config=$dir/docutils.conf $txtfile $htmlfile
        fi
    fi
done

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

if [ $skipsync -eq 0 ] ; then
    # copy files to sourceforge:
    if [ $quiet -eq 1 ] ; then
        rsync -e ssh -r -u --delete ./ goodger,puzzler@web.sourceforge.net:$remotehtdocs
    else
        echo 'rsyncing to SourceForge:'
        rsync -v -e ssh -r -u --delete ./ goodger,puzzler@web.sourceforge.net:$remotehtdocs
    fi
fi

if [ $feedback -eq 1 ] ; then
    echo '...puzzler-update-local done.'
fi

# Local Variables:
# indent-tabs-mode: nil
# End: