#!/bin/csh -f

#
# $Id: clean_dir.csh,v 1.4 2004/01/19 23:17:34 jweiss Exp $
#

set account = $1;
set hosttype = `/bin/athena/machtype`

if ("$hosttype" =~ "decmips") then
	if (`/usr/ucb/whoami` != "root") then
		echo "You must be root to run this script on a DECstation."
		exit 1
	endif
endif

if ($account !~ sipb[0-7]) then
	echo You can only clean a valid guest account.
	exit 1
else
	echo Are you sure you want to obliterate the account ${account}\?
	echo -n 'Enter YES to do it: '
	set answer = $<
	if ($answer != YES) then
		echo "Cancelled."
		exit 1
	endif
endif

set source = /afs/sipb.mit.edu/project/guests/prototype
set dest = /afs/sipb.mit.edu/user/$account
set auid = `hesinfo $account passwd | awk -F: '{print $3}'`

set fs = /bin/athena/fs
if ( -x /etc/chown ) then
	set chown = /etc/chown
else if ( -x /sbin/chown ) then
	set chown = /sbin/chown
else
	set chown = /bin/chown
endif
set find = /usr/bin/find
if (${hosttype}x =~ "sun4x") then
	set chown = /usr/bin/chown
	set find = /afs/athena.mit.edu/project/gnu/sun4bin/gfind
	# Solaris find sux and insists on doing a depth-first find
endif

set cmd # = echo
set echo

# Give admins all bits
$cmd $find $dest -type d -exec $fs setacl {} system:administrators all \;

# blow away everything
echo "Ignore the error about a read-only filesystem."
$cmd /bin/rm -rf $dest/

# fixup acls.
$cmd $fs setacl $dest $account none -clear
$cmd $fs setacl $dest $account all
$cmd $fs setacl $dest system:administrators all
$cmd $fs setacl $dest system:anyuser rl

# install template files/directories
$cmd /bin/cp $source/{*,.??*} $dest
$cmd /bin/mkdir $dest/Mail
$cmd /bin/chmod 700 $dest/Mail
$cmd $chown -R $auid $dest || $cmd $find $dest -exec $chown $auid {} \;

# fix mail acl
$cmd $fs setacl $dest/Mail system:anyuser none

# remove admin
$cmd $fs setacl $dest/Mail system:administrators none
$cmd $fs setacl $dest system:administrators none
