#! /bin/sh

echo
echo "   Maple 7 license installation"
echo
echo
echo " This tool assumes that you have received a network license file from Waterloo Maple Inc."
echo " If this is not the case, obtain a license file from Waterloo Maple Inc. and rerun this tool"
echo " Note: You will have a chance to review all the data that you enter before any files are modified."

LICFILE=
DEFLIC=maple7.lic
MAPLE=
DEFMAPLE=/usr/local/maple
LICLOC=
DEFLOC=FLEXlm/maple7.lic
SERVER=
DEFSERVER=

while [ x"$LICFILE" = x ]
do
	echo
	echo " Enter the path to the new license file: [$DEFLIC] "
	read LICFILE
	if [ x"$LICFILE" = x ]
	then
		LICFILE=$DEFLIC
	fi
	if [ -d $LICFILE ]
	then
		LICFILE=$LICFILE/$DEFLIC
	fi

	if [ ! -f $LICFILE ]
	then
		if [ -f $LICFILE.txt ]
		then
			LICFILE=$LICFILE.txt
			break
		fi
		echo
		echo "No file named $LICFILE could be found. Please try again." 
		echo
		LICFILE=
	else
		break
	fi
done

echo
echo " If you do not already have a Maple 7 installation, please go through the Maple 7 installation process before restarting this tool."

while [ x"$MAPLE" = x ]
do
	echo
	echo " Enter the location of your existing Maple 7 installation: [$DEFMAPLE] "
	read MAPLE
	if [ x"$MAPLE" = x ]
	then
		MAPLE=$DEFMAPLE
	fi
	if [ ! "`echo $MAPLE | sed 's;\(.\).*;\1;'`" = "/" ]
	then
		echo "You need to enter an absolute path (starting with /). Please try again."
		echo
		MAPLE=
	elif [ ! -d $MAPLE ]
	then
		echo "No directory named $MAPLE could be found. Please try again."
		echo
		MAPLE=
	else
		if [ ! -f $MAPLE/license/license.dat ]
		then
			echo "No valid Maple 7 installation was found in $MAPLE. Please try again."
			echo
			MAPLE=
		else
			break
		fi
	fi
done

NOPERM=
if [ -f $MAPLE/.ttt ]
then
	/bin/rm 2> /dev/null $MAPLE/.ttt
	if [ -f $MAPLE/.ttt ]
	then
		NOPERM=1
	fi
else
	touch 2> /dev/null $MAPLE/.ttt
	if [ ! -f $MAPLE/.ttt ]
	then
		NOPERM=1
	fi
	/bin/rm 2> /dev/null $MAPLE/.ttt
fi

if [ -n "$NOPERM" ]
then
	echo
	echo " You do not have write permission on the Maple 7 installation directory $MAPLE."
	echo " Please rerun this tool with the proper permissions. If unsure, contact your sytem administrator."
	echo
	exit 1
fi

DEFLOC=$MAPLE/$DEFLOC

while [ x"$LICLOC" = x ]
do
	echo 
	echo " Where would you like to put the network license file:  [$DEFLOC] "
	read LICLOC
	if [ x"$LICLOC" = x ]
	then
		LICLOC=$DEFLOC
	fi
	if [ -d $LICLOC ]
	then
		LICLOC=$LICLOC/maple7.lic
	fi

	if [ ! -d `dirname $LICLOC` ]
	then
		echo "No directory named `dirname $LICLOC` could be found. Please try again."
		echo
		LICLOC=
	else
		break
	fi
done
	
DEFSERVER=`sed 's;#.*;;' $MAPLE/license/license.dat |grep "SERVER" | grep -v "USE_SERVER" | head -1 | sed 's;.*SERVER \([^ ]*\).*;\1;'`
if [ "$DEFSERVER" = "<servername>" ]
then
	DEFSERVER=
fi

while [ x"$SERVER" = x ]
do
	echo
	echo " Please enter the name or IP address of your license server."
	echo " If you are not sure what to enter, contact your system administrator."
	echo " License server: [$DEFSERVER] "
	read SERVER
	if [ x"$SERVER" = x ]
	then
		SERVER=$DEFSERVER
	fi
done

echo
echo " Your license will now be installed using the following data:"
echo "    License file:       " $LICFILE
echo "    Maple installation: " $MAPLE
echo "    License location:   " $LICLOC
echo "    License server:     " $SERVER

if [ -f $LICLOC ]
then
	echo
	echo "Note: An existing license file was found at $LICLOC and may be modified during the license installation."
fi

PROCEED=
while [ x"$PROCEED" = x ]
do
	echo
	echo " Do you want to proceed (yes/no) ?"
	read PROCEED
	if [ x"$PROCEED" = x ]
	then
		echo " You must enter yes or no."
	elif [ "$PROCEED" = "no" ]
	then
		echo " aborting without changing any existing files."
		exit 1
	elif [ "$PROCEED" = "yes" ]
	then
		PROCEED=1
	else
		echo " You must enter yes or no."
		PROCEED=
	fi
done


grep "# MAPLE_APPEND" $LICFILE > /dev/null
if [ "$?" = "0" ]
then
	# appending
	APPEND=1
	if [ ! -f $LICLOC ]
	then
		APPEND=0
	fi
else
	# overwriting
	APPEND=0
fi

if [ "$APPEND" = 1 ]
then
	echo
	echo " Appending new license file to existing file in $LICLOC."
	cat $LICLOC $LICFILE > $LICLOC.tmp
	chmod +w $LICLOC.tmp
	echo "" >> $LICLOC.tmp
else
	echo " Storing new license file in $LICLOC"
	cp $LICFILE $LICLOC.tmp
fi	

# update SERVER line

NUMLINES=`sed 's;#.*;;g' $LICLOC.tmp | grep "SERVER" | grep -v "USE_SERVER" | wc -l | sed 's;\ *;;g'`

MULTIPLESERVERS="no"

if [ "$NUMLINES" = "0" ]
then
	echo "Your network license file does not contain any SERVER lines. Aborting without changing any files."
	/bin/rm $LICLOC.tmp
	exit 1
elif [ "$NUMLINES" = "1" ]
then
	echo " Updating SERVER line in $LICLOC"
	expand $LICLOC.tmp | sed -e 's;#\(.*\)SERVER;#\1 SeRvErX;g' -e "s;SERVER[ ]*[^ ]*;SERVER $SERVER;" -e 's;SeRvErX;SERVER;g' > $LICLOC.tmp1
	/bin/mv $LICLOC.tmp1 $LICLOC.tmp
else
	MULTIPLESERVERS="yes"
fi

echo " Updating SERVER line in $MAPLE/license/license.dat"
expand $MAPLE/license/license.dat | sed -e 's;#\(.*\)SERVER;#\1 SeRvErX;g' -e 's;USE_SERVER;USE_SeRvErX;g' -e "s;SERVER[ ]*[^\ ]*;SERVER\ $SERVER;" -e 's;SeRvErX;SERVER;g' > $MAPLE/license/license.tmp

mv $MAPLE/license/license.tmp $MAPLE/license/license.dat
mv $LICLOC.tmp $LICLOC

if [ "$MULTIPLESERVERS" = "yes" ]
then
	echo
	echo " Your network license file $LICLOC contains more than one SERVER lines."
	echo " You will need to manually update the correct SERVER line using the server $SERVER."
	echo
fi

echo " license installation is complete."


