#! /bin/sh

# Create a Third year project group.
# This needs:
#	addition to /etc/group
#	creation of group directory in /usr/proj
#	adding a link to /usr/proj/SE/Groups
#
# Usage: creategroup groupname member member member member ...
#
#	Martin Guy, UKC, January 1989

groupname=$1
shift

grep -s "^$groupname:" /etc/group && {
	echo "Error: Group $groupname already exists:"
	grep "^$groupname:" /etc/group
	exit 1
}

gid=`gidhash $groupname`

grep -s ":$gid:" /etc/group && {
	echo "Error: The group-id hashed from that name clashes with an existing group:"
	grep ":$gid:" /etc/group
	echo "Pick another group name."
	exit 1
}

# ensure all the named users exist
for member
do
	pu -q -d $member > /dev/null || {
		echo "Warning: User $member does not exist on this machine."
		# Don't exit - it may be a project supervisor,
		# who should be mailed anyway
		# exit 1
	}
done

firstmember=$1
shift
othermembers=$*

# Construct the group entry for /etc/group
{
	echo -n "$groupname:*:$gid:"

	# do first member separately, not preceded by a comma
	echo -n $firstmember

	# and the rest of the members of the group
	for member in $othermembers
	do
		echo -n ",$member"
	done
	echo ''
} >> /etc/group

# add a line to /usr/proj/3YP (a record of all 3rd Year project groups)
echo $groupname $firstmember $othermembers >> /usr/proj/SE/Groups

# now make the project directory for them
groupdir=/usr/proj/SE/$groupname
mkdir $groupdir
chgrp $groupname $groupdir
chmod 070 $groupdir

# And mail them to tell them what had transpired

{
# correct grammar! (What? You want commas as well?! :-( )

echo "
I have created a group called \"$groupname\" for $othermembers
and $firstmember on `hostname`.

I have also created a directory for project work in $groupdir.
Please put all files relating to the project in this directory and use the
group bits of file permissions to allow other members of your group to gain
access to project files.

This also simplifies disc quotas (should they become necessary) and makes
tidying up when the project has finished less painful for me.

You don't need to do anything special to gain access to files belonging
to the group as 4.2BSD Unix allows you to be in up to 8 groups simultaneously.
The \"groups\" command will tell you your group access list.

	Martin Guy"
} | mail -s "Group $groupname" $firstmember $othermembers
