#! /bin/sh

# Revised 2012-03-02
#
# Open Text - FirstClass Division
#
# A utility to fix file permissions for FirstClass Network Stores (FCNS)
# 
# The target fcns folder must not be a link.
#
# Two arguments are expected:
#  - volume name   e.g. "Master" (OSX), "master" (Linux), or "all"
#  - NS folder     e.g. "fcns", "fcns8001", etc., or "all"

if [ "`uname -s`" = "Darwin" ] ; then
	EXEC_ROOT="/Library/FirstClass Server"
	DATA_ROOT="/Library/FirstClass Server"
	VOLUMES_ROOT="$DATA_ROOT/Volumes"
	MY_USER="fcadmin"
	MY_GROUP="admin"
	EXAMPLE_MASTER="Master"
	EXAMPLE_MIRROR="Mirror"
else
	EXEC_ROOT="/opt/fcsd"
	DATA_ROOT="/var/opt/fcsd"
	VOLUMES_ROOT="$DATA_ROOT/volumes"
	MY_USER="fcadmin"
	MY_GROUP="adm"
	EXAMPLE_MASTER="master"
	EXAMPLE_MIRROR="mirror"
fi
NEEDS_PASSWORD=1
PROCESS_LOWERCASE=0
GOT_ONE=0

BadFCNS()
{
	echo " "
	echo "Error: You must specify a valid post office folder name."
	echo " "
	echo "Syntax: `basename $0` [-c|-h] <Volume> <FCNSFolderName>"
	echo " "
	echo "Valid post office folders on volume '$1' are:"
	cd "$VOLUMES_ROOT/$1"
	for FCNS_NAME in fcns* ; do
		if [ -L "$VOLUMES_ROOT/$1/$FCNS_NAME" ] ; then
			echo "    $FCNS_NAME (*** Invalid, cannot be a symbolic link. ***)"
		elif [ -d "$VOLUMES_ROOT/$1/$FCNS_NAME" ] ; then
			echo "    $FCNS_NAME"
		fi
	done
	exit 3
}

BadVolume()
{
	echo " "
	echo "Error: You must specify a valid FirstClass volume name."
	echo " "
	echo "Syntax: `basename $0` [-c|-h] <Volume> <FCNSFolderName>"
	echo " "
	echo "Valid FirstClass volume names are:"
	cd "$VOLUMES_ROOT"
	for volname in * ; do
		if [ -L "$VOLUMES_ROOT/$1/$FCNS_NAME" ] ; then
			echo "    $FCNS_NAME (*** Invalid, cannot be a symbolic link. ***)"
		elif [ -d "$VOLUMES_ROOT/$volname" ] ; then
			echo "    $volname"
		fi
	done
	echo " "
	echo "Use \"`basename $0` -h\" for additional help and examples."
	exit 2
}

FCFixFCNS()
{
# Args: $1 is name of volume containing fcns folder
#       $2 is name of the fcns folder
	VOLUME_ROOT="$VOLUMES_ROOT/$1"
	FCNS_ROOT="$VOLUME_ROOT/$2"

	if [ "$NEEDS_PASSWORD" -ne "0" ] ; then
		echo "If prompted for a password, enter the password for '`id -un`'."
		sudo echo " "
		if [ $? -ne 0 ] ; then
			exit $?
		fi
		NEEDS_PASSWORD=0
	fi	

	GOT_ONE=1
	echo "Recognized the post office: \"$FCNS_ROOT\":"
	echo -n "    Setting permissions and ownership..."
	sudo chmod ug+rw,o= "$VOLUME_ROOT"
	echo -n "."
	sudo chown $MY_USER:$MY_GROUP "$VOLUME_ROOT"
	echo -n "."
	sudo find "$FCNS_ROOT" -iname ".DS_Store" -exec rm {} \;
	sudo chmod -R ug+rw,o= "$FCNS_ROOT"
	echo -n "."
	sudo chown -R $MY_USER:$MY_GROUP "$FCNS_ROOT"
	echo -n "."
	if [ "$PROCESS_LOWERCASE" -ne "0" ] ; then
		perl "$EXEC_ROOT/fclower.pl" "$FCNS_ROOT"
	fi
	echo "...done."
}

FCFixVolume()
{
# Args: $1 is name of volume containing fcns folder
#       $2 is $2 from commandline

# echo "FCFixVolume \"$1\" \"$2\""

    if [ "$2" = "" ] ; then
	BadFCNS "$1"
    fi
    if [ "$2" = "all" ] ; then
	FCNS_SPEC="fcns*"
    else
	FCNS_SPEC="$2"
	if [ -L "$VOLUMES_ROOT/$1/$2" ] ; then
		BadFCNS "$1"
	elif [ ! -d "$VOLUMES_ROOT/$1/$2" ] ; then
		if [ "$GOT_ONE" -ne "0" ] ; then
			return 0
		else
			BadFCNS "$1"
		fi
	fi
    fi

    cd "$VOLUMES_ROOT/$1"
    for FCNS_NAME in $FCNS_SPEC ; do
    	if [ ! -L "$FCNS_NAME" ] ; then
    		if [ -d "$FCNS_NAME" ] ; then
			FCFixFCNS "$1" "$FCNS_NAME"
		fi
	fi
    done
}

ProcessRequest()
{
	if [ "$1" = "" ] ; then
		BadVolume
	fi

	if [ "$1" = "all" ] ; then
		VOLUME_SPEC="*"
	else
		if [ ! -d "$VOLUMES_ROOT/$1" ] ; then
			BadVolume
		fi
		VOLUME_SPEC="$1"
	fi

	cd "$VOLUMES_ROOT"
	for volname in $VOLUME_SPEC ; do
		FCFixVolume "$volname" "$2"
	done

	# Now do general global fixups...
	sudo chmod ug+rw,o= "$VOLUMES_ROOT"
	sudo chown $MY_USER:$MY_GROUP "$VOLUMES_ROOT"
}

##### MAINLINE #####

if [ "$1" = "-h" ] ; then
	echo " "
	echo "Syntax: `basename $0` [-h|-x] [-c] <Volume> <FCNSFolderName>"
	echo " "
	echo "Both parameters are required, however you can"
	echo "specify 'all' (without the quotes) for either."
	echo " "
	echo " -c   converts the post office to lowercase (e.g. after a migration)"
	echo " -x   fix ownership and file permissions of the executable files under $EXEC_ROOT"
	echo " -h   displays this help"
	echo " "
	echo "Note: You must run this command as '$MY_USER'."
	echo " "
	echo "Examples:"
	echo "    `basename $0` $EXAMPLE_MASTER fcns"
	echo "    `basename $0` $EXAMPLE_MIRROR fcns8001"
	echo "    `basename $0` $EXAMPLE_MASTER all"
	echo "    `basename $0` all fcns"
	echo "    `basename $0` all all"
	exit 1
fi

if [ "$1" = "-x" ] ; then
	echo "If prompted for a password, enter the password for '`id -un`'."
	echo " "
	echo "Verifying and ensuring ownership and permissions of executable files..."
	if [ -f "$EXEC_ROOT/fcsd" ] ; then
		sudo chown root:$MY_GROUP "$EXEC_ROOT/fcsd"
		if [ $? -ne 0 ] ; then
			echo "Error: Could not set fcsd ownership."
			exit 1
		fi
		sudo chmod ug=rwxs,o= "$EXEC_ROOT/fcsd"
		if [ $? -ne 0 ] ; then
			echo "Error: Could not set fcsd permissions."
			exit 1
		fi
	fi
	if [ -f "$EXEC_ROOT/fcsd.rez" ] ; then
		sudo chown $MY_USER:$MY_GROUP "$EXEC_ROOT/fcsd.rez"
		if [ $? -ne 0 ] ; then
			echo "Error: Could not set fcsd.rez ownership."
			exit 1
		fi
		sudo chmod 660 "$EXEC_ROOT/fcsd.rez"
		if [ $? -ne 0 ] ; then
			echo "Error: Could not set fcsd.rez permissions."
			exit 1
		fi
	fi
	if [ -f "$EXEC_ROOT/fcisd" ] ; then
		sudo chown root:$MY_GROUP "$EXEC_ROOT/fcisd"
		if [ $? -ne 0 ] ; then
			echo "Error: Could not set fcisd ownership."
			exit 1
		fi
		sudo chmod ug=rwxs,o= "$EXEC_ROOT/fcisd"
		if [ $? -ne 0 ] ; then
			echo "Error: Could not set fcisd permissions."
			exit 1
		fi
	fi
	if [ -f "$EXEC_ROOT/fcisd.rez" ] ; then
		sudo chown $MY_USER:$MY_GROUP "$EXEC_ROOT/fcisd.rez"
		if [ $? -ne 0 ] ; then
			echo "Error: Could not set fcisd.rez ownership."
			exit 1
		fi
		sudo chmod 660 "$EXEC_ROOT/fcisd.rez"
		if [ $? -ne 0 ] ; then
			echo "Error: Could not set fcisd.rez permissions."
			exit 1
		fi
	fi
	echo "Done."
	exit 0
fi

if [ "`id -un`" != "$MY_USER" ] ; then
	echo "You must run this command as the '$MY_USER' user."
	echo " "
	echo "Use \"`basename $0` -h\" for additional help and examples."
	exit 1
fi

if [ "$1" = "-c" ] ; then
	PROCESS_LOWERCASE=1
	ProcessRequest $2 $3
else
	ProcessRequest $1 $2
fi
