#!/bin/csh -f

switch (`uname -s``uname -r`)
case SunOS5*:
	breaksw
case Linux*:
    set LINUX
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

set HVERBOSE; set HDEBUG
set NVERBOSE; set NDEBUG

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	case -hv:
		set HVERBOSE = " -v"
		shift
		breaksw
	case -hd:
		set HDEBUG = " -d"
		shift
		breaksw
	case -nv:
		set NVERBOSE = " -v"
		shift
		breaksw
	case -nd:
		set NDEBUG = " -d"
		shift
		breaksw
	default:
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

#set DATE = `date "+%H %M" | sed 's;0\([0-9]\);\1;g'`
# avoid "@: Badly formed number." later
set DATE = `date "+%H %M" | sed 's;0\([0-9]\);\1;g'`

set TMP = /tmp/`basename $0`.$$
set HOST = `hostname | sed 's;\.[Rr][Uu][Tt][Gg][Ee][Rr][Ss]\.[Ee][Dd][Uu];;'`

#touch $TMP
echo "#" $0 run at `date` on `hostname` > $TMP

if ($?LINUX) echo "MAILTO=watrous@mx.farside.rutgers.edu" >> $TMP

# Run hourly first

@ DATE[2] += 2
if ($DATE[2] > 59) then
    @ DATE[1] ++
    @ DATE[2] = 0
endif
echo "$DATE[2]	$DATE[1]	*	*	*	Batch/hourly$HVERBOSE$HDEBUG" >> $TMP

# Then nightly (so hourly won't complain it's still running)

@ DATE[2] ++
if ($DATE[2] > 59) then
    @ DATE[1] ++
    @ DATE[2] = 0
endif
echo "$DATE[2]	$DATE[1]	*	*	*	Batch/nightly$NVERBOSE$NDEBUG" >> $TMP

# It should skip the du stuff

touch Batch/do-local-structures.skip.$HOST

# And finally, quickcheck (to fix up crontab)
# make this 2 mins later so possibly rapidly running hourly doesn't
# catch nightly still running.  11/26/08

@ DATE[2] += 2
if ($DATE[2] > 59) then
    @ DATE[1] ++
    @ DATE[2] = 0
endif
echo "$DATE[2]	$DATE[1]	*	*	*	Batch/quickcheck" >> $TMP

# and queue up quickcheck job

if (! -e Batch/Check/makecrontab.$HOST) ln -s makecrontab \
					    Batch/Check/makecrontab.$HOST

# now do the deed

crontab $TMP
if ($?VERBOSE) crontab -l
/bin/rm -f $TMP
