#
# request script for texmf package mackay@cs.washington.edu   95/11/17

# echo $1, return 1 for yes 0 for no
#
askyesno ()
{
	while true
 	do
		echo "$1 (y/n): \c"
		read ans
		case $ans in

			"Y"|"y") return 1;;
		
			"N"|"n") return 0;;

			*) echo "\tInvalid Input: $ans";;
		esac
	done
}

#
# Get a single word answer
#
# echo $1, return non-null word in $2
#
getword ()
{
	echo "$1: \c"
	read $2
}

#
# Print choices for confirmation
#
printinfo ()
{
	echo "\tBase directory for installation: $1"
	echo "\tAdministrative id to be entered in password file: $2"
	echo "\tGroup to which this id belongs: $3"
}
#
askinfo ()
{
	askyesno "\nDo you wish to establish an administrative id for this package"
        if [ $? -eq 1 ]
	then
		getword "\nWhat name do you wish to use [texmf]" $1 
	        getword "\nWhat group should this id belong to [adm]" $2
		eval DOPASSWD="dopasswd"
	else
		eval DOPASSWD="notchanged"
	fi
}

#
# main()
#
trap 'exit 3' 15
echo "\n"
echo "Note:\tIt is preferable to install this package under a unique"
echo "\tuser id.  The default is texmf with a home directory of /opt/texmf"
echo "\tDefault loader paths assume that the package resides in /opt/texmf"
echo "\tIf you must put it elsewhere, consider a symbolic link to /opt/texmf"
echo "\tOtherwise all users will have to reset LD_LIBRARY_PATH"

SAVE_FILE_NAME=$1	# will get trashed by the "set" cmds below
TEXMFUID="texmf"
TEXMFGID="adm"
TEXMFUIDNO=-1

echo "\nThis package installs by default in $BASEDIR:\n"
askyesno "\t Is this where you want it? "
if [ $? -eq 1 ]
then
	getword "\nChoose an alternative base directory (use full path) " BASEDIR
	BASEDIR=$BASEDIR
fi

askinfo TEXMFUID TEXMFGID
if [ $DOPASSWD = "dopasswd" ]
then
	TEXMFUID=${TEXMFUID:-texmf}
	TEXMFGID=${TEXMFGID:-adm}
	if ( `egrep "^$TEXMFUID" /etc/passwd > /dev/null` )
	then 
		echo "\n\tThis login already exists. /etc/passwd will not be changed\n"
		eval DOPASSWD="notchanged"
	else
		echo "\nYou have entered the following values:\n"
		printinfo $BASEDIR $TEXMFUID $TEXMFGID
		
		askyesno "\nIs this correct? "
		if [ $? -eq 1 ]
		then
			TEXMFUID=$TEXMFUID
			TEXMFGID=$TEXMFGID
		fi
		askyesno "\nWill you let the system choose the uid for texmf? "
		if [ $? -eq 0 ]
		then
			getword "\nChoose a uid number (must be greater than 99) " TEXMFUIDNO
			if [ ${TEXMFUIDNO:-60002} -eq 60002 -o $TEXMFUIDNO -lt 100 ]
			then
				echo "\tNot a valid uid number.  exit pkgadd and try again" ; exit
			fi
		
			eval TESTUIDNO="`grep $TEXMFUIDNO /etc/passwd | \
				awk -F: ' { printf \"%s \", $3 }'`"
		        if [ ${TESTUIDNO:-60002} -eq $TEXMFUIDNO ]
			then
				echo "\n\t\tWARNING: THIS ID ALREADY IN USE"
				echo "\t\tReverting to system default\n"
				eval TEXMFUIDNO="-1"
			fi
		else
			eval TEXMFUIDNO="-1"
		fi
	fi
else
	echo "\tThis may result in all the files being owned by root"
	echo "\tafter installation, and will probably produce screensful"
        echo "\tof non-fatal \"failed to set attribute\" messages."
fi
#
# Export Parameters
#
cat >$SAVE_FILE_NAME <<!
DOPASSWD='$DOPASSWD'
BASEDIR='$BASEDIR'
TEXMFUID='$TEXMFUID'
TEXMFGID='$TEXMFGID'
TEXMFUIDNO='$TEXMFUIDNO'
!

