#!/bin/bash

[ -f /etc/local/volumes ] || exit 1
. /etc/local/volumes
[ "x$XDIR" = "x" ] && exit 1

if [ $# -eq 0 ]; then
#	echo "$0: directory name(s) required"
	echo "Usage: $0 <base directory> <read user/group> <write user/group> [create]"
	exit 1
	fi

if [ "x$1" = "x" ]; then
	echo "$0: base directory missing"
	exit 1
	fi
VOL=$XDIR/$1

if [ "x$2" = "x" ]; then
	echo "$0: read group missing"
	exit 1
	fi
ALL=$2

if [ "x$3" = "x" ]; then
	echo "$0: write group missing"
	exit 1
	fi
GRP=$3

#GRPUSER="nobody"
GRPUSER="admin"
#if [ "x$4" != "x" ]; then
#	GRPUSER=$4
#else
	id $GRP >/dev/null && GRPUSER=$GRP
#fi

if [ "x$4" != "x" ]; then
	if [ "x$4" == "xcreate" ]; then
		mkdir -p $VOL/$ALL/$GRP || exit
	else
		echo "Usage: $0 <base directory> <read user/group> <write user/group> [create]"
		exit 1
	fi
fi

test -d $VOL/$ALL/$GRP || exit
test -d $VOL/$ALL/$GRP/.AppleDouble || mkdir $VOL/$ALL/$GRP/.AppleDouble
test -d $VOL/$ALL/$GRP/.AppleDesktop || mkdir $VOL/$ALL/$GRP/.AppleDesktop
#chown $ALLUSER.$ALL $VOL/$ALL
chown root.$ALL $VOL/$ALL
chown $GRPUSER.$ALL $VOL/$ALL/$GRP
chmod u=rwX,g=rXs,o= $VOL/$ALL
chmod u=rwX,g=rXs,o= $VOL/$ALL/$GRP
echo "People in group $GRP has read/write access through AppleShare"
test $GRP = $ALL || echo "People in group $ALL has read access through AppleShare"
echo "Others have no access"
echo -n "Checking permissions..."
chown -R $GRPUSER.$GRP $VOL/$ALL/$GRP \
    && chmod -R u=rwX,g=rwXs,o=rX $VOL/$ALL/$GRP \
    && chmod u=rwX,g=rXs,o=rX $VOL/$ALL/$GRP
echo "."