#!/bin/csh -f

# Do all the things needed so that cron-user can work on this system.

# daw; 11/12/20

switch (`uname -s``uname -r`)
case SunOS5*:
	exec echo do the Solaris version of $0
	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
	default:
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

set DQ = '"'
set TMP = /tmp/`basename $0`.$$
touch $TMP
set TSTAMP = `/bin/date +%y%m%d`
if (-e /etc/passwd.$TSTAMP) set TSTAMP = `/bin/date +%y%m%d.%H%M`
set ME = `/usr/bin/whoami`

# Gotta make cron-user local user for Ubuntu; 12/21/20

if (-e /etc/lsb-release) then
    grep Ubuntu /etc/lsb-release > /dev/null
    if (! $status) goto chkpasswd
endif

if ($?VERBOSE) echo `date +%T` Checking \~cron-user

( echo ~cron-user ) >& /dev/null
if (! $status) then
    if ($?VERBOSE) echo "   " \~cron-user works
    goto passwddone
else
#    echo "********************************"
    echo "   " passwd/shadow entry needed here.
#    echo Carefully watch code execution after removing
#    echo ${DQ}goto passwddone$DQ below.
#    echo "********************************"
endif
# remove this goto to test passwd/shadow modification code.
#goto passwddone

chkpasswd:
if ($?VERBOSE) echo `date +%T` Checking passwd

grep cron-user /etc/passwd > /dev/null
if (! $status) then
    if ($?VERBOSE) echo "   " cron-user is already in /etc/passwd
    goto passwddone
endif

/usr/bin/sudo /bin/cp -p /etc/passwd{,.$$}
echo "echo 'cron-user:x:555:1234::/home/cron-user:/bin/bash' >> /etc/passwd.$$" > $TMP
if ($status) goto done
/usr/bin/sudo /bin/bash $TMP
if ($?VERBOSE) tail -2 /etc/passwd.$$
echo $DEBUG /bin/mv /etc/passwd{,.$TSTAMP} > $TMP
echo $DEBUG /bin/mv /etc/passwd{.$$,}	  >> $TMP
/usr/bin/sudo /bin/bash $TMP
if ("$DEBUG" == "") /usr/bin/sudo /bin/rm -f /etc/passwd.$$

/usr/bin/sudo /bin/cp -p /etc/shadow{,.$$}
echo "echo 'cron-user:\!\!:18206::::::' >> /etc/shadow.$$" > $TMP
if ($status) goto done
/usr/bin/sudo /bin/bash $TMP
if ($?VERBOSE) /usr/bin/sudo tail -2 /etc/shadow.$$
echo $DEBUG /bin/mv /etc/shadow{,.$TSTAMP} > $TMP
echo $DEBUG /bin/mv /etc/shadow{.$$,}	  >> $TMP
/usr/bin/sudo /bin/bash $TMP
if ("$DEBUG" == "")/usr/bin/sudo /bin/rm -f /etc/shadow.$$

passwddone:

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

grep cron-user /etc/group > /dev/null
if (! $status) then
    if ($?VERBOSE) echo "   " cron-user is already in /etc/group
    goto groupdone
endif

set TSTAMP = `/bin/date +%y%m%d`
if (-e /etc/group.$TSTAMP) set TSTAMP = `/bin/date +%y%m%d.%H%M`

if ($?VERBOSE) echo `date +%T` Determining privileged group

#foreach GROUP ( slide )
# Not slide on edinburgh.cs; 12/16/20
foreach GROUP ( slide wheel )
    echo "grep ^%$GROUP /etc/sudoers >& $TMP.out" > $TMP
    echo "chown $ME $TMP.out"			 >> $TMP
    /usr/bin/sudo /bin/bash $TMP
    grep ^%$GROUP $TMP.out > /dev/null
    if (! $status) then
	set PGROUP = $GROUP
	break
    endif
    /usr/bin/sudo /bin/rm $TMP.out
    echo "grep ^%$GROUP /etc/sudoers.d/lcsr >& $TMP.out" > $TMP
    echo "chown $ME $TMP.out"			 >> $TMP
    /usr/bin/sudo /bin/bash $TMP
    grep ^%$GROUP $TMP.out > /dev/null
    if (! $status) then
	set PGROUP = $GROUP
	break
    endif
    /usr/bin/sudo /bin/rm $TMP.out
end

if (! $?PGROUP) then
    echo "   " Could not determine privileged group
    goto groupdone
else
    if ($?VERBOSE) echo "   " Privileged group is $PGROUP
endif

grep ^${PGROUP}: /etc/group > /dev/null
if ($status) then
    /usr/bin/sudo /bin/cp -p /etc/group{,.$$}
    echo "echo 'slide:x:1109:cron-user' >> /etc/group.$$" > $TMP
    if ($status) goto done
    /usr/bin/sudo /bin/bash $TMP
    if ($?VERBOSE) tail -2 /etc/group.$$ | sed 's;.;    &;'
    echo $DEBUG /bin/mv /etc/group{,.$TSTAMP} > $TMP
    echo $DEBUG /bin/mv /etc/group{.$$,}	  >> $TMP
    /usr/bin/sudo /bin/bash $TMP
    if ("$DEBUG" == "")/usr/bin/sudo /bin/rm -f /etc/group.$$
else
    /usr/bin/sudo /bin/cp -p /etc/group{,.$$}
    echo "/bin/sed -e 's;^"${PGROUP}":.*;&,cron-user;' /etc/group > /etc/group.$$" >> $TMP
    if ($status) goto done
    /usr/bin/sudo /bin/bash $TMP
    if ($?VERBOSE) grep "${PGROUP}" /etc/group.$$ | sed 's;.;    &;'
    echo $DEBUG /bin/mv /etc/group{,.$TSTAMP} > $TMP
    echo $DEBUG /bin/mv /etc/group{.$$,}	  >> $TMP
    /usr/bin/sudo /bin/bash $TMP
    /usr/bin/sudo /bin/rm -f /etc/group.$$
endif

groupdone:

if ($?VERBOSE) echo `date +%T` Checking home directory

if (! -d /home) then
    echo "Where shall I put cron-user's home directory?"
    goto homedone
endif

if (-e /home/cron-user) then
    if ($?VERBOSE) echo "   " cron-user already has a home directory
    goto homedone
endif

/usr/bin/sudo $DEBUG /bin/mkdir /home/cron-user
/usr/bin/sudo $DEBUG /bin/chown cron-user /home/cron-user
/usr/bin/sudo $DEBUG /bin/chmod 755 /home/cron-user

homedone:

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

/bin/rm -f $TMP*
