#! /bin/sh 
#
# Automaticial produce list of new things on our fav archives.
# By moffatd@dcs.glasgow.ac.uk (Darren J Moffat)
# From an Idea by thomsoj@dcs.glasgow.ac.uk (James Thomson)
# 
# 28/05/93  V0.1  The begining                                       Lines: 73
# 03/06/93  V1.0  Now self configs, and should not produce errors from diff etc.
# 08/06/93  V1.1  Added support for directories
#                 Added catchup ie. get files but don't tells about new stuff
#                 Added support for getting single sites files by alias names
#                 Improved usage information                         Lines: 198
# 09/06/93  V1.2  Bug fix: don't continue if we have a bad alias expansion
#                                                                    Lines: 203
# 17/06/93  V1.3  Added support for recursive directory listing      Lines: 206
# 01/07/93  V1.4  Major Revamp so that only one connection is made per host
#                 in our .favsites                                   Lines: 240
# 28/03/94  V1.5  Removed dependancy on .netrc file

USER=${USER:-$LOGNAME}
PATH="/usr/ucb:/bin:/usr/bin" # make sure "standard" utils
SAVEOLD="$HOME/.archives"     # where we keep old copies
WORK="/tmp/$$wn"              # our work area
MAILER=/usr/ucb/mail
FTP_LOGIN=anonymous
FTP_PASSWORD="`whoami`@`domainname`"

# build the ftp macros to get the stuff
macrobuild()
{
for next in $fnames
do
	basenext=`basename $next`
        if [ "$1" = "d" ]
        then
                echo "dir $next $WORK/$site:$basenext"
        elif [ "$1" = "r" ]
        then
                echo "ls \"-lR $next\" $WORK/$site:$basenext" 
        elif [ "$1" = "f" ]
        then
                echo "cd `dirname $next`" #>> $WORK/ftpcmd
                echo "get `basename $basenext` $WORK/$site:$basenext" 
        else
                echo "$nextype is not a vaild entry for field 3"
                exit 1
        fi
        shift
done
echo "" #>> $WORK/ftpcmd
}

# Actually get the "newfile" from the archive
doftpbit()
{
site="$1"
fnames="`echo $2 | sed s/,/\ / ` "
types="`echo $3  |sed s/,/\ / `"
errfl="$WORK/$site.err"

# Actually get the stuff now
ftp -i -n 1> $errfl 2>&1 <<-EOF
macdef doit 
`macrobuild $types`


open  $site
user $FTP_LOGIN $FTP_PASSWORD
\$doit
EOF
}

#extract out the stuff that is new
extractnew()
{
site="$1"
fnames="`echo $2 | sed s/,/\ /` "
mailfl="$WORK/mailfl"
errfl="$WORK/$site.err"
for next in $fnames
do
   basenext=`basename $next`
   ftpfl=$WORK/$site:$basenext
   if [ -r $ftpfl ]
   then
	oldfl="$SAVEOLD/$site:$basenext"
	if [ -r $oldfl ]
	then
	   diff $oldfl $ftpfl | grep ">" > diffs
	   if [ -s diffs ]
	   then
		echo "New files for Site: $site  File: $ftpfl" >> $mailfl
		cat diffs >> $mailfl
		echo "--End of Listing for $site:$ftpfl--" >> $mailfl
		echo "" >> $mailfl
	   else
		echo "Nothing New for Site: $site  File: $basenext" >> $mailfl
		echo "" >> $mailfl
	   fi
	   rm -f diffs
	else
		echo "First Run for Site: $site File: $fname" >> $mailfl
		cat $ftpfl >> $mailfl
		echo "" >> $mailfl
	fi
	mv $ftpfl $oldfl
   else
	echo "Could not get File: $ftpfl From: $site" >> $mailfl
	echo "" >> $mailfl
   fi
done

if [ -s "$errfl" ]
then
	echo "Errors for Site: $site" >> $mailfl
	cat "$errfl" >> $mailfl
        rm -f "$errfl"
fi

}

# mail the user the list of new stuff
mailnewstuff()
{
$MAILER -s "New Archive Files" $USER <<-EOM
The following is a listing of new files on your fav archives

`cat $WORK/mailfl`

---End of Listing--

EOM
}

displaynewstuff()
{
echo "The following is a listing of new files on your fav archives"
echo
cat $WORK/mailfl
echo;echo "---End of Listing---";
}

usage()
{
echo "whatsnew V1.5 (28 Mar 94)"
echo "usage: `basename $0` [-cl] [-s sitealias] [-u username]"
echo " -c	catchup, get listings but don't show us new stuff"
echo " -l	list to stdout"
echo " -m	mail results (default)"
echo " -s	do for sitealias only, sitealias is field 4 in .favsites"
echo " -u       user to mail, only vaild if not using -l"
echo
echo "You must create some entries in you .favsites file "
echo "This is a multi line file one line per site/index file"
echo "eg. vax.foosite.edu:/pub/index::vaxy"
echo "    sun.foobar.gov:/pub:d:foosun"
echo "    macman.gcul.ac.uk:/pub"
echo 
echo "~/.favsites Field Descriptions:"
echo "1.	Full internet site name"
echo "2.	Full path to file at site"
echo "3.	d if field 2 is a directory,r for recursive, empty otherwise"
echo "4.	alias name"
exit 1
}


######## End of Functions ############

#main()
cmd="`basename $0`"
# get our args
while getopts lmcs:u: arg
do
	case $arg in
	l)	mesgtype="list";;
	m)	mesgtype="mail";;
	c)	mesgtype="catchup";;
	s)	sitealias=$OPTARG;;
	u)	USER=${OPTARG:-$LOGNAME};;
	*)	usage;;
	esac
done
mesgtype=${mesgtype:-"mail"} # set default 

# get the site name(s)
if [ -r $HOME/.favsites ]
then
	mkdir $WORK
	if [ "$sitealias" = "" ] # we are doing all sites
	then
		sitelist="`cat $HOME/.favsites`"
		echo "Run for all sites" > $WORK/mailfl
	else # just doing one
		echo "Run for $sitealias only" > $WORK/mailfl
		sitelist="`grep ":$sitealias$" $HOME/.favsites`"
	fi
	echo "" >> $WORK/mailfl
else
	usage
fi

if [ "$sitelist" = "" ]
then
	echo -n "$cmd: Could not resolve site name for alias: " 
	echo $sitealias
	echo "$cmd -\? for help"
	exit 1
fi
#try and be self configuring
if [ ! -d $SAVEOLD ]
then
	mkdir $SAVEOLD
fi

for next in $sitelist
do
	sitename="`echo $next | awk -F: '{print $1}'` "
	filenames="`echo $next | awk -F: '{print $2}'` " 
	type="`echo $next | awk -F: '{print $3}'` "
	doftpbit $sitename $filenames $type
	extractnew $sitename $filenames
done

# Now notify the user
if [ "$mesgtype" = "list" ]
then
	displaynewstuff
elif [ "$mesgtype" = "catchup" ]
then
	echo "Catchup complete."
elif [ "$mesgtype" = "mail" ]
then
	mailnewstuff
fi

rm -rf $WORK
exit 0

