# This script will read the /etc/default/fppath file and the
# environmental variables PFPROG, PFDATA and PFDIR to locate
# the filePro programs and data files.  After they are located,
# they will be set to the correct ownership and permissions.
#
# Brought to you by: L.K. Weathers
#                on: 15 October 1991
#        revised on: 6 March 1992
#
#        revised by: Sam Cook
#     to correct "argument list too long" error under Linux
#        revised on: 4 April 2000
#     tested on Red Hat Linux v6.0
#
PATHFILE="/etc/default/fppath"

UID="`id | sed 's/^[^=]*=//
	s/(.*$//'`"
if [ "$UID" -ne 0 ]
then
	echo "You must be logged in as root in order to run this script."
	exit 1
fi

trap "echo '*** Set Permissions and Ownerships CANCELLED ***' ; exit 1" 2 3
trap 'rm -f /tmp/*$$' 0

	if [ ! -f $PATHFILE ]
	then
		echo "*** CANNOT FIND $PATHFILE ***"
		echo "Unable to set permissions and ownerships."
		echo "Please re-install filePro."
		exit 1
	fi

	echo "Checking for user 'filepro'..." ; echo

	if [ `grep -c "^filepro:" /etc/passwd` -eq 0 ]
	then
		echo "*** CANNOT FIND USER FILEPRO ***"
		echo "Unable to set permissions and ownerships."
		echo "Please create a user named 'filePro'."
		exit 1
	else
		echo "User 'filepro' found, proceeding normally..."
		echo
	fi


	if [ "$PFPROG" = "" ]
	then
		PFPROG="`sed -n 1p <$PATHFILE`"
	fi
	if [ "$PFDATA" = "" ]
	then
		PFDATA="`sed -n 2p <$PATHFILE`"
	fi
	if [ "$PFDIR" = "" ]
	then
		PFDIR="`sed -n 3p <$PATHFILE`"
	fi

# first, change exceptions like fppath

	chown filepro $PATHFILE
	chmod 644 $PATHFILE
	cd $PFDATA/$PFDIR
	cd ..
	chown root appl
	chmod 777 appl

# then change the programs
	cd /bin
	i=0
	haveP=false
	while [ $i -ne 2 ]
	do
		if [ -f p ]
		then
			chown filepro p 
			chmod 4755 p
			haveP=true
			echo "p is located in `pwd`"
		fi
		if [ -f P ]
		then
			chown filepro P
			chmod 4755 P
			haveP=true
			echo "P is located in `pwd`"
		fi
		cd /usr/bin
		i="`expr $i + 1`"
	done
	echo

	if $haveP
	then
		: ok
	else
		echo
		echo "Unable to locate the program 'p' in /bin or /usr/bin."
		loop=true
		while $loop
		do
			echo "Continue setting permissions & ownerships? (y/n) \c"
			read yesno
			case "$yesno" in
			[Yy]* )
				loop=false
				echo "Continuing...";;
			[Nn]* )
				exit 0;;
			* )
				echo 'Please answer "Y" or "N".'
				;;
			esac
		done
	fi

	echo "\nSetting permissions and ownerships for filePro programs"
	echo "located in $PFPROG/fp"
	echo "Please wait...\n"
	echo
	cd $PFPROG/fp/scripts
	if [ -f fp.setperms -a -f fp.list ]
	then
		: okay
	else
		echo "
Cannot find fp.setperms and fp.list

Please restore these files from the installation tape or the last floppy volume
and copy them into the $PFPROG/fp/scripts directory.
"
		exit 1
	fi
	cp fp.setperms /tmp/fp.setperms
	cp fp.list /tmp/fp.list
	cd ..
	/tmp/fp.setperms
	/tmp/fp.set2
	rm -f /tmp/fp.setperms /tmp/fp.list /tmp/fp.awk /tmp/fp.set2

	echo "\nSetting permissions and ownerships for filePro menus"
	if [ "$PFMENU" != "" ]
	then
		cd $PFMENU 
		echo "located in $PFMENU"
	elif [ "$FPMENU" != "" ]
	then
		cd $FPMENU 
		echo "located in $FPMENU"
	else
		cd $PFPROG/fp/menus
		echo "located in $PFPROG/fp/menus"
	fi
	echo "Please wait..."
	echo

	for menu in *
	do
		echo $menu
		chown filepro $menu
		case "$menu" in
			*.-* )	chmod 755 $menu ;;
			* )	chmod 644 $menu ;;
		esac
	done

#lastly, set perms and ownerships for filePro data files
	echo "\nSetting permissions and ownerships for filePro data files"
	if [ "$PFDATA" != "" ]
	then
		echo "located in $PFDATA$PFDIR/filepro"
	else
		echo "located in $PFDIR/filepro"
	fi
	echo "Please wait..."
	echo
	cd $PFDATA$PFDIR/filepro
	chown filepro .
	chmod 700 .
	for dir in *
	do
		if [ -d $dir ]
		then
			cd $dir
			echo `pwd`
			chown -R filepro . 
			chmod 700 .
			chmod 600 *
			cd ..
		else
			echo "Warning!  $dir is not a directory."
		fi
	done
	echo
	echo "Done"
exit 0

