#!/bin/sh
#
# this script ftp's any modified files from your web page tree
# to your ISP's web server account
#
# written by:   Maciej Kalisiak <mac@dgp.utoronto.ca>
# latest version and info: http://www.eecg.utoronto.ca/~mac/projects
#
# things to do:
# -still sometimes lets Emacs backup files slip through
# -what about deleting unwanted files on the remote server?
# -if fails to connect to ftp server, should not update .sendwebpages file
#
# done, finished:
# - do not enqueue directories, only normal files
# - filter out temporary files, backup files, etc...
#
#

tfile=/var/tmp/sendwebpages.cmds.$$
mfile=/var/tmp/sendwebpages.mail.$$
ftphost=ftp.eecg.utoronto.ca
email_address=mac@localhost   # where to email debugging info

# this is the 'find' command that finds the files that we want to send;
# specifically, it picks out first all files that have been modified since
# last time, throws away any Emacs backup files (*~), throws away any
# hidden files (.*), and throws aways files which are not of regular type
# or symlinks (this mostly is aimed at removing the directories)
find_cmd="find ./ -follow -newer $HOME/.sendwebpages -not -name '*~' \
    -not -name '.*' \( -type f -or -type l \)"

debug=1   # this is just temporary, until I am convinced everything works
quiet=0

printSyntax ()
{
	# only allow 'debug' OR 'quiet' since they are opposite ends
	# of the same spectrum: debug==max, quiet==none
	echo "Syntax:	`basename $0` [debug | quiet]"
	echo ""
	exit 1
}

# check if want debug mode
if [ $# -gt 0 ]
then
	if [ $# -ne 1 ]
	then
		printSyntax
	fi

	case $1 in
		debug)	debug=1 ;;
		quiet)	quiet=1 ;;
		*) printSyntax;;
	esac
fi

cd ${HOME}/web/eecg

echo "binary" > $tfile
echo "verbose" >> $tfile
echo "cd ~/public_www" >> $tfile

anything_to_send=0

# had to throw the 'eval' in so that the find executes properly
for file in `eval $find_cmd`
do
	if [ $quiet -ne 1 ] 
	then
		echo "enqueing $file"
	fi

	echo "put $file" >> $tfile
	anything_to_send=1

	if `echo "$file" | grep -q ".html$"`
	then
		if [ $quiet -ne 1 ]
		then
			echo "datestamping $file"
		fi
		datewebpage.pl $file
	fi
done

# touch ~/.sendwebpages ; this is the earliest point we can do it in
# we should do it as early as possible in case the user is modifying 
# the web pages as we are sending them
touch ~/.sendwebpages

if [ $anything_to_send -ne 0 ]
then
    if [ $debug -ne 0 ] 
    then
	    echo "commands that will be executed:" >> $mfile
	    echo "" >> $mfile
	    cat $tfile >> $mfile

	    echo "" >> $mfile
	    echo "results:" >> $mfile
	    echo "" >> $mfile
	    ftp $ftphost < $tfile >>$mfile 2>&1
    else
	    if [ $quiet -eq 1 ]
	    then
		    ftp $ftphost < $tfile > /dev/null 2>&1
	    else
p		    ftp $ftphost < $tfile
	    fi
    fi

    # mail me the results if in debug mode
    if [ $debug -eq 1 ]
    then
	    sendmail $email_address < $mfile
    fi
else
    echo "    No modifications detected; nothing transferred."
fi

# clean up
rm $tfile
if [ -f $mfile ]
then
	rm $mfile
fi
