#!/bin/csh -f

# Simple script to change "Batch/<something" to "/koko/system/watrous/Batch/<something"
# while testing cron job split.

# daw; 1/29/20

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

set DEBUG

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG = /bin/echo
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	case -n:
		set INSTALL = /bin/echo
		shift
		breaksw
	default:
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

set DQ = '"'
set BASE = `basename $0`
set TMP = /tmp/$BASE.$$
if (! $?INSTALL) set INSTALL

if ($?VERBOSE) echo `date +%T` Collecting current crontab

/usr/bin/crontab -l > $TMP.before

if ($?VERBOSE) echo `date +%T` Converting to ${DQ}secure$DQ crontab

#sed 's;\([	 ]\)Batch;\1/koko/system/watrous/Batch;g' $TMP.before > $TMP.after
echo 's;\([	 ]\)Batch;\1/koko/system/watrous/Batch;g' > $TMP.sed
sed -f $TMP.sed $TMP.before > $TMP.after

if ($?VERBOSE) echo `date +%T` Displaying differences

diff -C0 $TMP.{before,after} | sed "s;$TMP.;;"
if (! $status) then
    if ($?VERBOSE) echo "	    " none found
else
    if ($?VERBOSE) echo `date +%T` Installing ${DQ}secure$DQ crontab
    $DEBUG $INSTALL /usr/bin/crontab $TMP.after
endif

if ($?VERBOSE) echo `date +%T` Removing tmp files

/bin/rm -f $TMP*

if ($?VERBOSE) echo `date +%T` Done
