#!/bin/csh -f

# Given one or more directories, this script will force all files and
# directories to be in the same group and have read/write access for
# the group.

# daw; 8/3/04

# Modified to not use xargs as it does not play well with directories
# containing spaces.  Also added -r (just enforce read access).

# daw; 12/17/04

# Modified to use group number, not name in case group exists locally
# with a different gid.

# daw; 2/7/05

# Default is to enforce read/write

set RW = "rw"
set RWdn = 7
set RWfn = 6

switch (`uname -s``uname -r`)
case SunOS5*:
#    set XARGS = /usr/local/gnu/bin/xargs
# /bin/find does not understand "-not"
    set FIND = /usr/local/bin/find
    if (! -e $FIND) set FIND = /usr/local/gnu/bin/find
    if (! -e $FIND) exec echo ${0}: cannot find appropriate find
	breaksw
case Linux2*:
    set FIND = /usr/bin/find
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

set DEBUG
@ COUNT = 0
set TARGETS
while ( $COUNT <= $#argv )
    @ COUNT ++
    set TARGETS = ( $TARGETS "EndOfTargets" )
end

@ COUNT = 1

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG = /bin/echo
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	case -r:		# read access enforcement
		set RW = "r"
		set RWdn = 5
		set RWfn = 4
		shift
		breaksw
	default:
	    if (-d "$1") then
		set TARGETS[$COUNT] = "$1"
		@ COUNT ++
		shift
		breaksw
	    endif
	    if (-d "$1") goto intodirs # directories from here on out.
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

#if (! -e $XARGS) exec echo ${0}: Where is gnu xargs\?

intodirs:

#foreach TARGET ( $TARGETS )
while ( "$TARGETS[1]" != "EndOfTargets" )

    set TARGET = "$TARGETS[1]"

    set GROUP = `/bin/ls -dg "$TARGET" | awk '{print $3}'`
    set GID = `/bin/ls -dgn "$TARGET" | awk '{print $3}'`

    if ($?VERBOSE) echo `date +%T` Enforcing group " $GROUP (GID=$GID)" access to "$TARGET"

    if ($?VERBOSE) echo "   " `date +%T` chgrp $GID "(for directories and files)"
# rework these because spaces in directories or files screws this up:
#    $FIND $TARGET \( -type f -o -type d \) -not -group $GROUP -print | sed 's;\(.*\);"\1";' | $XARGS -r $DEBUG chgrp $GROUP
#    $FIND "$TARGET" \( -type f -o -type d \) -not -group $GROUP -exec $DEBUG chgrp $GROUP {} \;
    $FIND "$TARGET" \( -type f -o -type d \) -not -group $GID -exec $DEBUG chgrp $GID {} \;

# directories protected "drwx-wS---" produced 
# "WARNING: Group execution not permitted on (directory>, a lockable file"

    if ($?VERBOSE) echo "   " `date +%T` chmod g-s "(for select directories)"
#    $FIND $TARGET -type d -perm -2000 -not -perm -010 -print | sed 's;\(.*\);"\1";' | $XARGS -r $DEBUG chmod g-s 
    $FIND "$TARGET" -type d -perm -2000 -not -perm -010 -exec $DEBUG chmod g-s {} \;

    if ($?VERBOSE) echo "   " `date +%T` chmod g+${RW}xs "(for directories)"
#    $FIND $TARGET -type d -not -perm -2070 -print | sed 's;\(.*\);"\1";' | $XARGS -r $DEBUG chmod g+rwxs 
    $FIND "$TARGET" -type d -not -perm -20${RWdn}0 -exec $DEBUG chmod g+${RW}xs {} \;

    if ($?VERBOSE) echo "   " `date +%T` chmod g+${RW} "(for files)"
#    $FIND $TARGET -type f -not -perm -060 -print | sed 's;\(.*\);"\1";' | $XARGS -r $DEBUG chmod g+rw
    $FIND "$TARGET" -type f -not -perm -0${RWfn}0 -exec $DEBUG chmod g+${RW} {} \;

    shift TARGETS

end

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