#!/bin/csh

# this script is from sao's HASS-D, runs from cron
# originally from wade's "mitdir" system.

# check for the pelott files in "FTPSITE" (FTP account "lot_ath")
# if they exist, then move them to "NEWLOCATION" (afs space)

set FTPSITE="/afs/athena.mit.edu/user/l/o/lot_ath"
set NEWLOCATION="/afs/athena.mit.edu/astaff/project/pelott-dev/datafiles"
#set ANNOUNCE="rlgogol@mit.edu,jenlu@mit.edu, mharper@mit.edu, gvkelly@mit.edu"
#set ADMIN_MAIL="mharper@mit.edu, jenlu@mit.edu, rlgogol@mit.edu"

set ANNOUNCE="rlgogol@mit.edu"
set ADMIN_MAIL="rlgogol@mit.edu"

foreach file (lotclass.txt intro.txt short.txt openclas.txt)
if ( -r $FTPSITE/$file ) then

	# this script copies the staff file to afs
	setenv KRBTKFILE /tmp/mitdir0

	/afs/net/tools/@sys/krbtgp -s -keyfile /etc/athena/srvtab rcmd.magnolia
	attach pelott-dev

	# send mail so we know something a file has arrived
	ls -lt $FTPSITE/$file |& /usr/ucb/mail -s "New $file submitted" $ADMIN_MAIL

	# get a checksum of the new file
	set LOCALSUM=`sum $FTPSITE/$file | awk '{print $1}'`

	cp $FTPSITE/$file $NEWLOCATION/$file

# confirm the file arrived intact and then remove it from incoming directory

	set IMOK = 0
	if ( -r $NEWLOCATION/$file ) then
		set AFSSUM=`sum $NEWLOCATION/$file | awk '{print $1}'`
		if ( $AFSSUM == $LOCALSUM ) then
			set IMOK = 1
			mv $FTPSITE/$file $FTPSITE/Private/$file
		endif
	endif

	if ( $IMOK == 0 ) then
		/usr/ucb/mail -s "Problem transferring $file to AFS" $ADMIN_MAIL < /dev/null
	else
		ls -lt $NEWLOCATION/$file | /usr/ucb/mail -s "New file $file placed successfully" $ANNOUNCE
	endif
	kdestroy
endif
end
