#!/bin/sh

# Build a list of files changed since the last
# update and send them all to the ftp server.
# Excludes emacs' backup files ("*[~#]").
#
# Relies upon the .netrc feature of ftp
# to handle the login.
#
# When using 'tails' option, only sends the last 4k
# of the file over.  Much faster updates to the usual
# log files that way, useful when the network is being
# very bad.
#
# Usage: doupdate [noindex | indexonly | pretend | tails [size] | help]
#

trap "echo ABORTED;rm -f chronolog.htm chronorevlog.htm;exit 0" INT

INDEX=f
if [ $# -lt 1  ] ; then
    INDEX=t
else
    case $1 in
	i*) INDEX=only;;
	p*) INDEX=list;;
	n*) INDEX=f;;
	t*) INDEX=tails;;
	h*|-*) echo usage: $0 "[noindex | indexonly | pretend | tails]"; exit 0;;
    esac
fi

if [ $INDEX = "t" -o $INDEX = "only" ] ; then
##    echo "Making index..."
##    ./chronolog
##    echo "Making reverse index..."
##    ./chronolog chronorevlog.html r
    echo "Making indices..."
    chronolog chronolog.htm chronorevlog.htm || exit 1
# If a new file is different overwrite the old one, else throw it
# away.  This keeps the timestamp more valid, and lets this script
# do its job better.
    if cmp -s chronolog.htm chronolog.html ; then
	rm chronolog.htm
    else
	mv chronolog.htm chronolog.html
    fi
    if cmp -s chronorevlog.htm chronorevlog.html ; then
	rm chronorevlog.htm
    else
	mv chronorevlog.htm chronorevlog.html
    fi
    [ $INDEX = "only" ] && exit 0
fi

[ $INDEX != "list" ] && echo "Transferring new files..."

TAILS=4096
if [ $INDEX = "tails" -a $# == 2 ] ; then
    TAILS=$2
fi

[ -f newerthan ] && NTHAN="-a -newer newerthan"

LIST=`find . ! -name '*[~#]' -a ! -name '.*' -a ! -name '*+' -a ! -name newerthanflash -a ! -name chrono\*log.htm -a -type f $NTHAN`

LIST2=`find . ! -name '*[~#]' -a ! -name '.*' -a ! -name '*+' -a ! -name newerthanflash -a ! -name chrono\*log.htm -a -type f $NTHAN |
while read file; do
    SIZE=0
    [ -f "$file" ] && SIZE=$(ls -l "$file"|sh -c 'read p l u g s r; echo $s')
    [ "$file" = "chronolog.html" ] && SIZE=0
    [ "$file" = "chronorevlog.html" ] && SIZE=0
    if [ $INDEX = "tails" -a $SIZE -ge $TAILS ] ; then
	echo restart $(expr $SIZE - $TAILS)
	echo put \\"$file\\"
    else
	echo put \\"$file\\" transfer
	echo rename transfer \\"$file\\"
    fi
done`

if [ -z "$LIST" ] ; then
    echo "No out-of-date files found"
    exit 0
fi

if [ $INDEX = "list" ] ; then
    echo $LIST
    exit 0
else
    echo "Ready to transfer:"
    echo "$LIST" | sed 's/^/	/'
fi

# If we can't reach the hosting site, stop now.

#SITEHOST=userweb.windwireless.net
#SITEHOST=tank.windwireless.net
SITEHOST=192.168.1.2

if ! ping -q -c 1 $SITEHOST >/dev/null
then
    echo "Unable to reach $SITEHOST, aborting."
    exit 1
fi

# Don't touch the timestamp file if the ftp fails!
# Hash mode seems to be a little more reliable when the network's being bad.

#if ftp -g -v $SITEHOST <<-EOT
if sftp $SITEHOST <<-EOT
	cd Sites
#	bin
#	hash
#	umask 022
	$LIST2
	EOT
then touch newerthan; fi

#
# To automatically check in (snapshot) recently modified files do:
#
# find . -maxdepth 1 -name \*.html ! -name chrono\* -perm +200 -exec ci -msnapshot -u {} \;
#
