#!/bin/csh -f

# For explicit filespecs containing /grad/users to work after the grad
# cluster has been merged into ilab, the /grad/users automount must be
# turned into a soft link to /ilab/users/grad (which contains links to
# where /grad/users/<xxx> were moved during the merger.

# daw; 7/30/19

# remove-grad-users-automount2 from ilab merged into here

# daw; 8/2/19

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

set ARGS
set WHOAMI = `whoami`

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG
#		shift
#		breaksw
# fall through to verbose
	case -v:
	    set ARGS = ( $ARGS $1 )
		set VERBOSE
		shift
		breaksw
	case -u:
	    set ARGS = ( $ARGS $1 )
		set UNDO
		shift
		breaksw
	case -ns:
	    if ($WHOAMI != "root") then
		set DQ = '"'
		echo ${0}: I am $WHOAMI and $DQ-ns$DQ specified
		exit 1
	    endif
		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
set YMD = `date +%y%m%d`

set MOUNT = /bin/mount
if (! -e $MOUNT) exec echo ${BASE}: Where is mount\?
set UMOUNT = /bin/umount
if (! -e $UMOUNT) exec echo ${BASE}: Where is umount\?

set SYSTEMCTL = /bin/systemctl
if (! -e $SYSTEMCTL) exec echo ${BASE}: Where is systemctl\?

set SUDO = /usr/bin/sudo
if (! -e $SUDO) exec echo ${BASE}: Where is sudo\?

if ($WHOAMI != "root") then
    $SUDO $0 -ns $ARGS
    exit $status
endif

set VERBOSE			# watch this...

set HOST = `hostname | sed 's;\.rutgers\.edu;;'`

$MOUNT | grep -w /grad | grep " type autofs " > $TMP
@ LINES = `cat $TMP | wc -l`
if (! $LINES) then
    exec echo /grad is not automounted on $HOST
endif
if ($LINES > 1) exec echo ${BASE}: found $LINES lines automounting /grad
# /etc/auto.master.d/auto.grad on /grad type autofs (rw,relatime,fd=-1,pgrp=5556,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=-1)

set AUTO = `awk '{print $1}' $TMP`
set AUTOH = $AUTO.$YMD
set AUTOFS = `echo $AUTO | sed 's;auto.grad;grad.autofs;'`
set AUTOFSH = $AUTOFS.$YMD
if (! -e $AUTO) exec echo ${BASE}: cannot find copy of auto.grad
if (! -e $AUTOFS) exec echo ${BASE}: cannot find copy of grad.autofs

echo $AUTO | grep auto.master.d > /dev/null
if ($status) then
    echo $AUTO is not in /etc/auto.master.d
    echo Modify $BASE to edit /etc/auto.master
    exit
endif

set GUSERS = /grad/users
set GUSERSH = /grad/users.$YMD
set GUSERSLD = /ilab/users/grad

    if (! -e $AUTO) then
	echo automount files do not exist
	exit
    endif
    echo Undoing automount on $HOST
    if ($?VERBOSE) echo `date +%T`    /bin/mv $AUTO $AUTOH
    /bin/mv $AUTO $AUTOH
    if ($?VERBOSE) echo `date +%T`    /bin/mv $AUTOFS $AUTOFSH
    /bin/mv $AUTOFS $AUTOFSH

if ($?VERBOSE) echo `date +%T` $SYSTEMCTL restart autofs.service
$SYSTEMCTL restart autofs.service
set STATUS = $status
if ($status) then
    echo $DQ$SYSTEMCTL restart autofs.service$DQ exited with status $STATUS
endif

    $MOUNT | grep -w /grad  > $TMP
    if (! $status) then
	set MPTS = `awk '{print $3}' $TMP | sort -r`
	foreach MPT ( $MPTS )
	    if ($?VERBOSE) echo `date +%T`	$UMOUNT -l $MPT
	    $UMOUNT -l $MPT
	    set STATUS = $status
	    if ($status) then
		echo $DQ$UMOUNT -l $MPT$DQ exited with status $STATUS
	    endif
	end
    endif

    /bin/ls -l /grad |& grep ' users$' >& /dev/null
    if (! $status) then
	if ($?VERBOSE) echo `date +%T`	/bin/mv $GUSERS $GUSERSH
	/bin/mv $GUSERS $GUSERSH
	set STATUS = $status
	if ($status) then
	    echo $DQ/bin/mv $GUSERS $GUSERSH$DQ exited with status $STATUS
	endif
    endif

    if (! -e /grad) then
	if ($?VERBOSE) echo `date +%T`	umask 22
	umask 22
	if ($?VERBOSE) echo `date +%T`	/bin/mkdir /grad
	/bin/mkdir /grad
    endif

    if ($?VERBOSE) echo `date +%T`    /bin/ln -s $GUSERSLD $GUSERS
    /bin/ln -s $GUSERSLD $GUSERS

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

