#!/bin/csh -f

#
# Added "-h" flag to all chown -hs and chgrp -hs (got sick of seeing errors 
# from cron every day!)          matt 	2/26/99

set LS=/usr/bin/ls
set ADMIN=tmmccoy
set SEMESTER=spr01
set ECHO=
set VERBOSE=

##########

if ($1 == "-v") set VERBOSE=1

##########

# setting exception for cs325 - contains drop boxes for students - owned by
# them.  I'm also running a watchdog script which monitors usage.
# --elgersma

set groups=(`ypcat group | grep '^cs' | sed -e 's/:.*//' | grep -v owner | grep -v cs325 | sort`)

cd /csinfo/www/courses/archive/$SEMESTER

foreach directory ($groups)
	if (-d $directory) then
		if ($VERBOSE) echo ""
		if ($VERBOSE) echo " processing directory $directory..."
		if ($VERBOSE) echo "  chgrp -h -R $directory $directory"
		${ECHO} chgrp -h -R $directory $directory
		if ($VERBOSE) echo "  chmod -f -R g+rw $directory"
		${ECHO} chmod -f -R g+rw $directory
		if ($VERBOSE) echo "  find $directory -type d -exec chmod u+x,g+xs {} \;"
		${ECHO} find $directory -type d -exec chmod u+x,g+xs {} \;
#		set owner=`ypcat group | grep "^${directory}-owner" | sed -e 's/^.*:.*://'`
		set owner=`${LS} -ld ${directory} | awk '{print $3}'`
		if ( "$owner" == "" ) then
			if ($VERBOSE) echo "  chown -h -R $ADMIN $directory"
			${ECHO} chown -h -R $ADMIN $directory
		else
			if ($VERBOSE) echo "  chown -h -R $owner $directory"
			${ECHO} chown -h -R $owner $directory
		endif
		if ($VERBOSE) echo "  find $directory -type d -exec chmod u+rwx {} \;"
		${ECHO} find $directory -type d -exec chmod u+rwx {} \;
	endif
end
