#!/bin/csh -f

# When someone's UID or default GID changes, their crontabs frequently
# get messed up, reporting "bad user" or "setgid failed" errors.  This
# resets that.

# daw; 1/5/11

switch (`uname -s``uname -r`)
case SunOS5*:
    set CRONDIR = /var/spool/cron/crontabs
	breaksw
case Linux2*:
    set CRONDIR = /var/spool/cron
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

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

if (! $?DEBUG) set DEBUG

if ($?VERBOSE) echo `date +%T` Collecting cron users

set CRONUSERS = `/bin/ls $CRONDIR`

if ("$DEBUG" != "") echo CRONUSERS = $CRONUSERS

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

foreach CRONUSER ( $CRONUSERS )
    if ($?VERBOSE) echo "   " `date +%T` $CRONUSER
# If there's still uncommented lines, must reload crontab
    sed -e 's;#.*;;' $CRONDIR/$CRONUSER | grep . > /dev/null
    if (! $status) then
	$DEBUG su $CRONUSER -c "crontab -l | crontab" | sed 's;.;	&;'
    else
	if ($?VERBOSE) echo "	Not need"
    endif
end
