#!/bin/bash

##We're wanting to set a directory as owned by a group  and setGID
##also sets files rw setGID group 

#  $1 should be modify
#  $2 is the group you want
#  $3 is the directory to modify

##additionally setting the umask to 002 in /etc/profile and /etc/login.defs
##will keep all current and new files in this scheme.

case $1 in

	worldread)
                chown -R root.$2 $3
                find $3 -type d -exec chmod u=rwx,g=srwx,o=rx {} \;
                ##find all of the (sub)directories, and pass them one at a time
                ##to chmod which then sets permissions 775, and setGID
                find $3 -type f -exec chmod u=rw,g=srw,o=r {} \;
                ##as above but for files
                echo
                echo "Permissions have been changed."
                echo
        ;;

	private)
                chown -R root.$2 $3
                find $3 -type d -exec chmod u=rwx,g=srwx,o= {} \;
                ##find all of the (sub)directories, and pass them one at a time
                ##to chmod which then sets permissions 770, and setGID
                find $3 -type f -exec chmod u=rw,g=srw,o= {} \;
                ##as above but for files
                echo
                echo "Permissions have been changed."
                echo
	;;

	*)
		echo
		echo "USAGE: setgid.sh [worldread|private] group_name directory_path"
		echo
		echo "This will change the a directory and it's subdirectories"
		echo " to a specified group setGID, as well as allow read/execute permissions"
		echo " on all (sub)directories.  It will also change the permissions"
		echo " of all files contained in the (sub)directories to be"
		echo " If you chose worldread, permissions will be set so everyone can see"
		echo " If you choose private, only group members can see"
		echo
		echo
	;;
esac
