#!/bin/sh
# @(#)config_template.sh	1.58 07/13/95 Copyright 1993 SMI
# Copyright (c) 1993 Sun Microsystems, Inc.  All Rights Reserved.
# Sun considers its source code as an unpublished, proprietary trade
# secret, and it is available only under strict license provisions.
# This copyright notice is placed here only to protect Sun in the event
# the source is deemed a published work.
#
# RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the
# Government is subject to restrictions as set forth in subparagraph
# (c)(1)(ii) of the Rights in Technical Data and Computer Software
# clause at DFARS 52.227-7013 and in similar clauses in the FAR and
# NASA FAR Supplement.
#
# All variables above this line must be set up during config script building
# on the license server.
# The variables below this point are part of the
# standard config file distributed with the product
lic_server=0
license_daemon="/etc/opt/licenses/lmgrd.ste"
lic_daemon_name="lmgrd.ste"
licdir="/etc/opt/licenses"
daemon_option_file="/etc/opt/licenses/daemon_options"	#default options file
set_daemon_options=0	#default is that no options were written
combined_file="/etc/opt/licenses/licenses_combined"
tmp_daemon_opt_path="/tmp/tmp_daemon_opt"
#have to fill in a port number
portno=1726

if [ -f /vmunix ]; then
   OSREL="4.x"
   thishost=`hostname`
   LICPS="ps"
   opt="-ax"
   lic_startup_script="/etc/opt/licences/lic_mgr"
else
   OSREL="5.x"
   thishost=`uname -n`
   LICPS="/usr/bin/ps"
   opt="-ef"
   lic_startup_script="/etc/rc2.d/S85lmgrd"
fi
exitcode=0
INSTALLED=1
DUPLICATE=2
INSTALL_WARNINGS=99
node_suffix="node"
LIT_RUN="notfromtty"
accept_input=0
DUMMY_VSTRING="DONTUSETHISASAVSTRING"


do_daemon_file_merge()
{
	from_file=$1
	to_file=$2

	while read keyword field1 field2 field3 field4
	do
		if [ ! -f ${to_file} ]
		then
			echo $keyword $field1 $field2 $field3 $field4 >> $to_file
			continue
		fi
		case ${keyword} in

		EXCLUDE)
		# Can't really do checking on this,
		# since there can be more than 1 per feature
			echo $keyword $field1 $field2 $field3 $field4 >> $to_file
			;;
		INCLUDE)
		# Can't really do checking on this,
		# since there can be more than 1 per feature
			echo $keyword $field1 $field2 $field3 $field4 >> $to_file
			;;
		LINGER)
# Check if already set for this feature
			if [ "$field1" != "" -a "$field2" != "" ]
			then
				grep $keyword $to_file | grep " $field1 "  > /dev/null 2>&1
				if [ $? -ne 0 ]
				then
					echo $keyword $field1 $field2 $field3 $field4 >> $to_file
				fi
			else
				echo $keyword $field1 $field2 $field3 $field4 >> $to_file
			fi
			;;
		NOLOG)
# Check if already set for this feature
			if [ "$field1" != "" ]
			then
				grep $keyword $to_file | grep " $field1" > /dev/null 2>&1
				if [ $? -ne 0 ]
				then
					echo $keyword $field1 $field2 $field3 $field4 >> $to_file
				fi
			else
				echo $keyword $field1 $field2 $field3 $field4 >> $to_file
			fi
			;;
		REPORTLOG)
# Check if already set for this feature
			if [ "$field1" != "" ]
			then
				grep $keyword $to_file  > /dev/null 2>&1
				if [ $? -ne 0 ]
				then
					echo $keyword $field1 $field2 $field3 $field4 >> $to_file
				fi
			else
				echo $keyword $field1 $field2 $field3 $field4 >> $to_file
			fi
			;;
		RESERVE)
		# Can't really do checking on this,
		# since there can be more than 1 per feature
			echo $keyword $field1 $field2 $field3 $field4 >> $to_file
			;;
		TIMEOUT)
# Check if already set for this feature
			if [ "$field1" != "" -a "$field2" != "" ]
			then
				grep $keyword $to_file | grep " $field1 " > /dev/null 2>&1
				if [ $? -ne 0 ]
				then
					echo $keyword $field1 $field2 $field3 $field4 >> $to_file
				fi
			else
				echo $keyword $field1 $field2 $field3 $field4 >> $to_file
			fi
			;;
		esac

	done < ${from_file}
}
#
# Merges daemon files in situation where path to options file is
# different from that already in /etc/opt/licenses/licenses_combined
# Places the feature line into the file specified as $1
# before any increment lines
#

merge_daemon_files()
{
	dmn=${1}
	old_dmnpth=${2}
	dmnpth=${3}
	dmnfound=0
# Check if daemon file exists. If it doesn't, and we are able to accept
# input, then ask user for daemon file name
#
	while true
	do
		if [ "${old_dmnpth}" = "" -o ! -f "${old_dmnpth}" ]
		then
			if [ $accept_input -eq 0 ]
			then
				echo Cannot find options file $old_dmnpth for daemon $dmn.
				if [ "${OSREL}" = "4.x" ]
				then
					echo Skipping daemon file merge operation.
					echo You may need to do this operation manually.
				else
					echo Skipping daemon file merge operation.\nYou may need to do this operation manually.
				fi
				return
			else
				echo
				echo Cannot find options file $old_dmnpth for daemon $dmn.
				ans=`ckyorn -e "Do you want to specify a daemon options file to merge for ${dmn} [y | n ]?" \
				-p "Do you want to specify a daemon options file to merge [y | n ]?"`
				if [ $ans = "y" -o $ans = "yes" -o $ans = "Y" \
	    		    -o $ans = "YES" -o $ans = "Yes" ]
				then
					echo Please enter full path to daemon options file for daemon $dmn: \c
					read old_dmnpth
				else
					if [ "${OSREL}" = "4.x" ]
					then
						echo Skipping daemon file merge operation.
						echo You may need to do this operation manually.
					else
						echo Skipping daemon file merge operation.\nYou may need to do this operation manually.
					fi
					return
				fi
			fi
		else
			break
		fi
	done

	dmn_flag=0
	if [ -f ${combined_file} ]
	then

		dmn_flag=0
		while read keyword field1 field2 field3 field4
		do
			if [ "${keyword}" = "DAEMON" ]
			then
				dmnfound=1
# Do the daemons match
				
				if [ "${dmn}" = "${field1}" ]
				then
					dmn_flag=1
					if [ "$old_dmnpth" != "" -a "$old_dmnpth" != "$field3" ]
					then
						do_daemon_file_merge $old_dmnpth $field3
					#
					# To make sure we don't execute next loop
					fi
				fi
			elif  [ $dmnfound -eq 1 ]
			then
				if [ $dmn_flag -eq 0 ]
				then
					if [ "$dmnpth" != "" -a "$old_dmnpth" != "$dmnpth" ]
					then
						do_daemon_file_merge $old_dmnpth $dmnpth
					fi
				fi
				break
			fi
		done < ${combined_file}
	else
		if [ "$dmnpth" != "" -a "$old_dmnpth" != "$dmnpth" ]
		then
			do_daemon_file_merge $old_dmnpth $dmnpth
		fi
	
	fi
	
}

output_feature_line ()
{
	ofl_filename="$1"
	featureline="FEATURE ${2} ${3} ${4} ${5} ${6} ${7}"
	awk 'BEGIN	{ 
				foundfeature = 0
				printfeature = 0
			}
$0 == ""			{
						printf ("\n")
					}
$1 == "DAEMON" || $1 == "SERVER"		{ printf("%s\n", $0) }
$1 == "FEATURE" || $1 == "INCREMENT" || $1 == "UPGRADE"		{
						foundfeature=1
						if (printfeature == 0) 
						{
							if (vstring != dummyvstring)
								printf("%s %s %s\n", featureline, vstring, hostid);
							else
								printf("%s \"\" %s\n", featureline, hostid);
							printfeature = 1
						}
						printf("%s\n", $0)
					}
$0 != "" && $1 != "SERVER" && $1 != "FEATURE" && $1 != "INCREMENT" && $1 != "UPGRADE" && $1 != "DAEMON"			{
						printf("%s\n", $0)
					}
END			{
				if (foundfeature == 0 && printfeature == 0)
							if (vstring != dummyvstring)
								printf("%s %s %s\n", featureline, vstring, hostid);
							else
								printf("%s \"\" %s\n", featureline, hostid);
			}' dummyvstring="${DUMMY_VSTRING}" featureline="${featureline}" vstring="${8}" hostid="${9}" ${ofl_filename} > /tmp/licout

	cp /tmp/licout ${ofl_filename}
	rm /tmp/licout
}
#
# Summarizes for the user what errors have occurred in the license installation
#
print_summary ()
{
	read exitcode < /tmp/exitcode.$$
	if [ ${exitcode} -ne $INSTALL_WARNINGS ]
	then
		echo "**All Licenses Were Successfully Installed**"
		ps_i=1
		while [ $ps_i -le $num_feature ]
		do
			
			grep $INSTALLED /tmp/featstatus${ps_i}.$$ > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
				feature_name=feature_name$ps_i
				eval f1=\$$feature_name
				feature_version=feature_version$ps_i
				eval f3=\$$feature_version
				featexpdate=featexpdate$ps_i
				eval f4=\$$featexpdate
				featuser=featuser$ps_i
				eval f5=\$$featuser
				featcode=featcode$ps_i
				eval f6=\$$featcode
				echo "$f1" "$f3" "$f4" "$f5" "$f6"
			fi
			ps_i=`expr $ps_i + 1`
		done
	else
		if [ "${OSREL}" = "4.x" ]
		then
			echo "**Some duplicate licenses were detected during installation**"
		else
			echo "\n**Some duplicate licenses were detected during installation**"
		fi
		echo
		echo "The following licenses were successfully installed"
		ps_i=1
		while [ $ps_i -le $num_feature ]
		do
			
			grep $INSTALLED /tmp/featstatus${ps_i}.$$ > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
				feature_name=feature_name$ps_i
				eval f1=\$$feature_name
				feature_version=feature_version$ps_i
				eval f3=\$$feature_version
				featexpdate=featexpdate$ps_i
				eval f4=\$$featexpdate
				featuser=featuser$ps_i
				eval f5=\$$featuser
				featcode=featcode$ps_i
				eval f6=\$$featcode
				echo "$f1" "$f3" "$f4" "$f5" "$f6"
			fi
			ps_i=`expr $ps_i + 1`
		done
		echo
			if [ "${OSREL}" = "4.x" ]
			then
					echo "The following duplicate licenses were detected and not fully re-installed."
					echo "These licenses may still be usable:"
					echo ""
			else
					echo "The following duplicate licenses were detected and not fully re-installed\nThese licenses may still be usable:\n"
			fi
		ps_i=1
		while [ $ps_i -le $num_feature ]
		do
			
			grep $DUPLICATE /tmp/featstatus${ps_i}.$$ > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
				feature_name=feature_name$ps_i
				eval f1=\$$feature_name
				feature_version=feature_version$ps_i
				eval f3=\$$feature_version
				featexpdate=featexpdate$ps_i
				eval f4=\$$featexpdate
				featuser=featuser$ps_i
				eval f5=\$$featuser
				featcode=featcode$ps_i
				eval f6=\$$featcode
				echo "$f1" "$f3" "$f4" "$f5" "$f6"
			fi
			ps_i=`expr $ps_i + 1`
		done
	fi
}
				
status_cleanup()
{
	rm -f /tmp/exitcode.$$ > /dev/null 2>&1
	ps_i=1
	while [ $ps_i -le $num_feature ]
	do
		rm -f /tmp/featstatus${ps_i}.$$ > /dev/null 2>&1
		ps_i=`expr $ps_i + 1`
	done
}

#
# Places the license server lines into the file specified as $1
#
echo_license_servers ()
{
	els_i=1
	els_filename="$1"
	while [ $els_i -le $num_server ]
	do
		hostname=hostname$els_i
		eval f1=\$$hostname
		if [ "$f1" != "" ]
		then
			hostid=hostid$els_i
			eval f2=\$$hostid
			hostport=portno$els_i
			eval pno=\$$hostport
			if [ "${pno}" != "" ]
			then
				useportno=${pno}
			else
				useportno=${portno}
			fi
			
			echo SERVER $f1 $f2 ${useportno} >>  ${els_filename}
			if [ $? -ne 0 ]
			then
				echo "Cannot write to file  ${els_filename}"
				return 1
			fi
		fi
		els_i=`expr $els_i + 1`
	done
}
create_tmp_daemon_options_file ()
{
	j=1
	rm -f $tmp_daemon_opt_path > /dev/null 2>&1
	while [ $j -le $num_daemon_opt ]
	do
		dptr=daemon_opt_val$j
		eval daemon_opt_val=\$$dptr
		if [ "$daemon_opt_val" != "" ]
		then
			echo $daemon_opt_val  >> $tmp_daemon_opt_path
			if [ $? -ne 0 ]
			then
				echo "Cannot write daemon option to file /tmp/tmp_daemon_opt"
				echo $daemon_opt_val
			fi
		fi
		j=`expr $j + 1`
	done
}
#
# Places the license daemon line into the file specified as $1
#
write_daemon_line ()
{
	wdl_i=1
	wdl_filename="$1"
	while [ $wdl_i -le $num_daemon ]
	do
		daemon=daemon$wdl_i
		eval d1=\$$daemon
		if [ "${d1}" != "" ]
		then
		
			daemonpath=daemonpath$wdl_i
			eval d2=\$$daemonpath
			daemon_opt=daemon_opt$wdl_i
			eval d3=\$$daemon_opt
			echo DAEMON ${d1} ${d2} ${d3} >>  ${wdl_filename}
			if [ $? -ne 0 ]
			then
				echo "Cannot write to file  ${wdl_filename}"
				return 1
			fi
		fi
		wdl_i=`expr $wdl_i + 1`
	done
}
#
# Places the license daemon line into the file specified as $1
# - Used for the combined license file
#
echo_daemon_line ()
{
	edl_i=1
	edl_filename="$1"
	while [ $edl_i -le $num_daemon ]
	do
		daemon=daemon$edl_i
		eval d1=\$$daemon
		if [ "${d1}" != "" ]
		then
		
			daemonpath=daemonpath$edl_i
			eval d2=\$$daemonpath
			daemon_opt=daemon_opt$edl_i
			eval d3=\$$daemon_opt
			daemonline="DAEMON ${d1} ${d2} ${d3}"
			awk 'BEGIN	{ 
						founddaemon = 0 
						foundfeature=0
					 	printdaemon = 0 
					}
$0 == ""			{
						print ""
					}
$1 == "DAEMON"		{ 
						if ( $2 == dname )
						{
								founddaemon = 1
							if (NF >= 4)
				  				print
							if (NF == 2)
								printf("%s %s %s %s\n", $1, $2, dpath, dopt)
							if (NF == 3)
								printf("%s %s %s %s\n", $1, $2, $3, dopt)
						} else {
						  print
						}
					}
$1 == "SERVER"		{ print }
$1 == "FEATURE"		{
						foundfeature=1
						if (founddaemon == 0 && printdaemon == 0) 
						{
							print daemonline
							printdaemon = 1
						}
						print
					}
$1 == "INCREMENT"		{
						foundfeature=1
						if (founddaemon == 0 && printdaemon == 0) 
						{
							print daemonline
							printdaemon = 1
						}
						print
					}
$1 == "UPGRADE"		{
						foundfeature=1
						if (founddaemon == 0 && printdaemon == 0) 
						{
							print daemonline
							printdaemon = 1
						}
						print
					}
$0 != "" && $1 != "SERVER" && $1 != "FEATURE" && $1 != "INCREMENT" && $1 != "UPGRADE" && $1 != "DAEMON"			{
						print
					}
END			{
				if (foundfeature == 0 && foundaemon == 0 && printdaemon == 0)
							print daemonline
			}' dname="$d1" dpath="$d2" dopt="$d3" daemonline="$daemonline" ${edl_filename} > /tmp/licout

#			echo DAEMON ${d1} ${d2} ${d2} >>  ${edl_filename}
			cp /tmp/licout ${edl_filename}
			rm /tmp/licout
		fi
		edl_i=`expr $edl_i + 1`
	done
}

#
# Places the single feature line specified by $2 into the license file
# specified by $1
echo_feature_lines ()
{
	efl_filename="$1"
	efl_i=$2
	feature=feature$efl_i
	eval f1=\$$feature
	ui=useincrement$efl_i
	eval useincrement=\$$ui
	featd=featd$efl_i
	eval f2=\$$featd
	featversion=featversion$efl_i
	eval f3=\$$featversion
	featexpdate=featexpdate$efl_i
	eval f4=\$$featexpdate
	featuser=featuser$efl_i
	eval f5=\$$featuser
	featcode=featcode$efl_i
	eval f6=\$$featcode
	featvstring=featvstring$efl_i
	eval f7=\$$featvstring
	feathostid=feathostid$efl_i
	eval f8=\$$feathostid

	featoverdraft=featoverdraft$efl_i
	eval f9=\$$featoverdraft
	feathostidkey=feathostidkey$efl_i
	eval f10=\$$feathostidkey
	featvendstr=featvendstr$efl_i
	eval f11=\$$featvendstr
	featoptstr=featoptstr$efl_i
	eval f12=\$$featoptstr


	#Get from version if this is an upgrade line
	if [ $useincrement -eq 2 ]
	then
		fromversion=fromversion$efl_i
		eval fver=\$$fromversion
	fi


	# Check if this feature line is already present in the license file
	#
	# If any FLEXlm 4.1 values are present, the line must be treated
	# as a 4.1 license
	#
	if [ ! -z "${f9}" -o ! -z "${f10}" -o ! -z "${f11}" -o ! -z "${f12}" ]; then
		flexver=4
	  
		FEAT_LINE="${f1} ${f2} ${f3} ${f4} ${f5} ${f6} ${f8} ${f9} ${f10} ${f11} ${f12}"
		UPGR_LINE="${f1} ${f2} ${fver} ${f3} ${f4} ${f5} ${f6} ${f8} ${f9} ${f10} ${f11} ${f12}"
	else 
		flexver=2
	  
		FEAT_LINE="${f1} ${f2} ${f3} ${f4} ${f5} ${f6} \"${f7}\" ${f8}"
		UPGR_LINE="${f1} ${f2} ${fver} ${f3} ${f4} ${f5} ${f6} \"${f7}\" ${f8}"
	fi
	

	# Check if this feature line is already present in the license file
	if [ ${flexver} = 4 ]; then
		grep " ${f1} " ${efl_filename} | grep " ${f3} " | grep " ${f4} " | grep " ${f5} " | grep " ${f6} " > /dev/null 2>&1
	else 
	  grep " ${f1} " ${efl_filename} | grep " ${f3} " | grep " ${f4} " | grep " ${f5} " | grep " ${f6} " | grep \"${f7}\" > /dev/null 2>&1
	fi

	if [ $? -eq 0 ]
	then
		echo "Duplicate of Feature ${f1} Version ${f3} found in file ${efl_filename}."

		#Save status of this install as a duplicate
		echo "${INSTALL_WARNINGS}" > /tmp/exitcode.$$
		echo "${DUPLICATE}" > /tmp/featstatus${efl_i}.$$
		return 1
	else
		case $useincrement in

		0)
			if [ ! -f ${efl_filename} ]
			then
				echo FEATURE ${FEAT_LINE} >> ${efl_filename}
			else
			  if [ "${f7}" = "" ]
			  then
			    f7="${DUMMY_VSTRING}"
			  fi

			  output_feature_line ${efl_filename} ${FEAT_LINE}
			fi
			;;
		2)
			echo UPGRADE ${UPGR_LINE} >> ${efl_filename};;
		*)
			echo INCREMENT ${FEAT_LINE} >> ${efl_filename};;
		esac
		if [ $? -ne 0 ]
		then
			echo "Cannot write to file  ${efl_filename}"
			return
		fi
		grep $DUPLICATE /tmp/featstatus${efl_i}.$$ > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
			echo $INSTALLED > /tmp/featstatus${efl_i}.$$
		fi
	fi
	return 0
}
#
# Places the single feature line specified by $2 into the license file
# specified by $1
echo_feature_lines_and_check ()
{
	efl_filename="$1"
	efl_i=$2
	basefile=$3
	feature=feature$efl_i
	eval f1=\$$feature
	ui=useincrement$efl_i
	eval useincrement=\$$ui
	featd=featd$efl_i
	eval f2=\$$featd
	featversion=featversion$efl_i
	eval f3=\$$featversion
	featexpdate=featexpdate$efl_i
	eval f4=\$$featexpdate
	featuser=featuser$efl_i
	eval f5=\$$featuser
	featcode=featcode$efl_i
	eval f6=\$$featcode
	featvstring=featvstring$efl_i
	eval f7=\$$featvstring
	feathostid=feathostid$efl_i
	eval f8=\$$feathostid

	featoverdraft=featoverdraft$efl_i
	eval f9=\$$featoverdraft
	feathostidkey=feathostidkey$efl_i
	eval f10=\$$feathostidkey
	featvendstr=featvendstr$efl_i
	eval f11=\$$featvendstr
	featoptstr=featoptstr$efl_i
	eval f12=\$$featoptstr

	# Get from version if this is an upgrade line
	if [ $useincrement -eq 2 ]
	then
		fromversion=fromversion$efl_i
		eval fver=\$$fromversion
	fi

	# Check if this feature line is already present in the license file
	#
	# If any FLEXlm 4.1 values are present, the line must be treated
	# as a 4.1 license
	#
	if [ ! -z "${f9}" -o ! -z "${f10}" -o ! -z "${f11}" -o ! -z "${f12}" ]; then
		flexver=4

		FEAT_LINE="${f1} ${f2} ${f3} ${f4} ${f5} ${f6} ${f8} ${f9} ${f10} ${f11} ${f12}"
		UPGR_LINE="${f1} ${f2} ${fver} ${f3} ${f4} ${f5} ${f6} ${f8} ${f9} ${f10} ${f11} ${f12}"
	else 
		flexver=2
		FEAT_LINE="${f1} ${f2} ${f3} ${f4} ${f5} ${f6} \"${f7}\" ${f8}"
		UPGR_LINE="${f1} ${f2} ${fver} ${f3} ${f4} ${f5} ${f6} \"${f7}\" ${f8}"
	fi
	

	ls ${basefile}* 2> /dev/null | grep  ${basefile} > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		for fn in `ls ${basefile}*`
		do
			if [ ${flexver} = 4 ]; then
				grep " ${f1} " ${fn} | grep " ${f3} " | grep " ${f4} " | grep " ${f5} " | grep " ${f6} " > /dev/null 2>&1
			else 
				grep " ${f1} " ${fn} | grep " ${f3} " | grep " ${f4} " | grep " ${f5} " | grep " ${f6} " | grep \"${f7}\" > /dev/null 2>&1
			fi

			if [ $? -eq 0 ]
			then
				echo "Duplicate of Feature ${f1} Version ${f3} found in file ${fn}."
				echo "${INSTALL_WARNINGS}" > /tmp/exitcode.$$
				echo "${DUPLICATE}" > /tmp/featstatus${efl_i}.$$
				return 1
			fi
		done
	fi
	case $useincrement in

	0)
			if [ ! -f ${efl_filename} ]
			then
				echo FEATURE ${FEAT_LINE} >> ${efl_filename}
			else
			  if [ "${f7}" = "" ]
			  then
			    f7="${DUMMY_VSTRING}"
			  fi
			  
			  output_feature_line ${efl_filename} ${FEAT_LINE}
			fi
			;;
	2)
		echo UPGRADE ${UPGR_LINE} >> ${efl_filename};;
	*)
		echo INCREMENT ${FEAT_LINE} >> ${efl_filename};;
	esac
	if [ $? -ne 0 ]
	then
		echo "Cannot write to file  ${efl_filename}"
		return 1
	fi
	grep $DUPLICATE /tmp/featstatus${efl_i}.$$ > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo $INSTALLED > /tmp/featstatus${efl_i}.$$
	fi
	return 0
}
#
# Places the single feature line specified by $2 into the license file
# specified by $1
check_feature_lines ()
{
	cf_filename="$1"
	cf_i=$2
	basefile=$3
	feature=feature$cf_i
	eval f1=\$$feature
	featd=featd$cf_i
	eval f2=\$$featd
	featversion=featversion$cf_i
	eval f3=\$$featversion
	featexpdate=featexpdate$cf_i
	eval f4=\$$featexpdate
	featuser=featuser$cf_i
	eval f5=\$$featuser
	featcode=featcode$cf_i
	eval f6=\$$featcode
	featvstring=featvstring$cf_i
	eval f7=\$$featvstring
	feathostid=feathostid$cf_i
	eval f8=\$$feathostid
	    
	featoverdraft=featoverdraft$cf_i
	eval f9=\$$featoverdraft
	feathostidkey=feathostidkey$cf_i
	eval f10=\$$feathostidkey
	featvendstr=featvendstr$cf_i
	eval f11=\$$featvendstr
	featoptstr=featoptstr$cf_i
	eval f12=\$$featoptstr

	#
	# If any FLEXlm 4.1 values are present, the line must be treated
	# as a 4.1 license
	#
	if [ ! -z "${f9}" -o ! -z "${f10}" -o ! -z "${f11}" -o ! -z "${f12}" ]; then
		flexver=4
	else 
		flexver=2
	fi

	# Check if this feature line is already present in the license file
	ls ${basefile}* 2> /dev/null | grep  ${basefile} > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		for fn in `ls ${basefile}*`
		do
			if [ ${flexver} = 4 ]; then
				grep " ${f1} " ${fn} | grep " ${f3} " | grep " ${f4} " | grep " ${f5} " | grep " ${f6} "  > /dev/null 2>&1
			else 
				grep " ${f1} " ${fn} | grep " ${f3} " | grep " ${f4} " | grep " ${f5} " | grep " ${f6} " | grep \"${f7}\" > /dev/null 2>&1
			fi
			if [ $? -eq 0 ]
			then
				echo "Duplicate of Feature ${f1} Version ${f3} found in file ${fn}."
				echo "${INSTALL_WARNINGS}" > /tmp/exitcode.$$
				echo "${DUPLICATE}" > /tmp/featstatus${cf_i}.$$
				return 1
			fi
		done
	fi
	grep $DUPLICATE /tmp/featstatus${cf_i}.$$ > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo $INSTALLED > /tmp/featstatus${cf_i}.$$
	fi

	return 0
}
	
#
# Adds path to router file
#
fix_router_files ()
{
	licfilename=$1
	rfile=$2
	frf_i=1
	if [ "$rfile" = "" ]
	then
		break
	fi
	if [ -f $rfile ]
	then
		awk ' BEGIN {
						found_path = 0
					}
				{	if ($0 ~ /^#/)
						printf("%s\n", $0)
				  	else
					{
						if ($0 == "")
							printf("%s\n", $0)
				  		else
						{
							if (index($0, licfilename))
							{
								printf("%s\n", $0)
							}
							else
							{
								if(found_path == 0)
									printf("%s:%s\n", licfilename, $0)
								else
									printf("%s\n", $0)
							}
							found_path = 1
						}
					}
				}
END		{
			if (found_path == 0)
				printf("%s\n", licfilename)
		}' licfilename="$licfilename" ${rfile} > /tmp/routerout

		cp $rfile $rfile.BAK
		cp /tmp/routerout  $rfile 
	else
		echo $licfilename > $rfile
	fi
}
#
# Builds the elementary license file for the feature specified by $1 and
# the filename specified by $2. No suffix added to this filename -
# When building files in the standard directory, never use a sequence number,
# since then the postinstall script for the product won't know which
# of these files to pull over.
# Limitation - Don't switch to different servers prior to installing the
# product.
build_base_file()
{
	outputfile=$2
	thisfeat=$1
	if [ ! -f ${outputfile} ]
	then
		echo_license_servers $outputfile
		echo_daemon_line $outputfile
		chmod 644 ${outputfile}
	fi

	echo_feature_lines ${outputfile} ${thisfeat}
}

#
# Builds the elementary license file for the feature specified by $1
# with the file name base specified by $2, and a sequence number to
# distinguish it from other files for the same feature (possibly
# with different server names
# Determines the next sequence number to use as the file name suffix - Based on
# the next file in the sequence that is not present.
#
build_sequence_file()
{
	filebase=$2
	thisfeat=$1
	bsf_i=1
	while [ $bsf_i -le 100 ]
	do
		outputfile="$filebase",$bsf_i
		if [ ! -f $outputfile ]
		then
			break
		fi
		bsf_i=`expr $bsf_i + 1`
	done
	if [ $bsf_i -ge 100 ]
	then
		fp=feature${thisfeat}
		eval feat_print=\$$fp
		echo Cannot build elementary license file for ${feat_print}
		return 1
	fi
	
	#
	# Check if feature is nodelocked. If it is, then we are
	# not supposed to place server and daemon lines in file
	#
	nodelock=featnodelock${thisfeat}
	eval n1=\$$nodelock
	if [ ${n1} -ne 1 ]
	then
		echo_license_servers $outputfile
		write_daemon_line $outputfile
	fi

	echo_feature_lines_and_check ${outputfile} ${thisfeat} ${filebase}
	chmod 644 ${outputfile}
# If this line already appears in another file, then remove file
	if [ $? -ne 0 ]
	then
		rm ${outputfile}
	else
# Files named ${filelink}.router (i.e., prefix.loc.router) are considered
# router files. They contain paths to files that need to be updated
# with the new license file path
		filelink=$3
		if [ -f ${licdir}/${filelink}.router ]
		then
			while read router_file
			do
				fix_router_files  "${sunpro_router_rel_path}${outputfile}" $router_file
			done < ${licdir}/${filelink}.router
		fi
	fi
}
#
# Builds the elementary license file for the feature specified by $1
# with the file name base specified by $2, and a sequence number to
# distinguish it from other files for the same feature (possibly
# with different server names
# Determines the next sequence number to use as the file name suffix - Based on
# the next file in the sequence that is not present.
#
build_same_file()
{
	filebase=$2
	thisfeat=$1
	i=1
# Special code for nodelocked/DEMO licenses - put them all into 1 file
# if usesamefile is set
#
	nodelock=featnodelock${thisfeat}
	eval n1=\$$nodelock
	if [ ${n1} -eq 1 ]
	then
		outputfile=${filebase},${node_suffix}
	else
		fileexists=0
		ls ${filebase}* 2> /dev/null | grep  ${filebase} > /dev/null 2>&1
		if [ $? -eq 0 ]
		then
			for infile in `ls ${filebase}*`
			do
				outputfile=$infile
				nodelock=featnodelock${thisfeat}
				eval n1=\$$nodelock
				if [ ${n1} -eq 1 ]
				then
#					foundfile=$outputfile
#					fileexists=1
					fileexists=0
					break
				else
				# See if first license server is in license file
				# If it is, then use this file. Else, continue
					grep $hostname1 $outputfile | grep $hostid1 > /dev/null 2>&1
					if [ $? -eq 0 ]
					then
						foundfile=$outputfile
						fileexists=1
						break
					fi
				fi
			done
		fi
	# If we haven't found an existing file, then we must find a new file,
	# with a sequence number that hasn't been used yet
		if [ $fileexists -eq 0 ]
		then
			same_i=1
			while [ $same_i -lt 100 ]
			do
				if [ ! -f ${filebase},$same_i ]
				then
					outputfile=${filebase},$same_i
					break
				else
					same_i=`expr $same_i + 1`
				fi
			done
	
			if [ $same_i -gt 100 ]
			then
				fp=feature${thisfeat}
				eval feat_print=\$$fp
				echo Cannot build elementary license file for ${feat_print}
				return 1
			fi
		fi
	fi
	
	check_feature_lines ${outputfile} ${thisfeat} ${filebase}
	if [ $? -eq 0 ]
	then
	#
	# Check if feature is nodelocked. If it is, then we are
	# not supposed to place server and daemon lines in file
	#
		nodelock=featnodelock${thisfeat}
		eval n1=\$$nodelock
		if [ ${n1} -ne 1 ]
		then
			if [ $fileexists -eq 0 ]
			then
				echo_license_servers $outputfile
				write_daemon_line $outputfile
			fi
		
		fi

		if [ -f ${outputfile} ]
		then
			chmod 644 ${outputfile}
		fi
		echo_feature_lines_and_check ${outputfile} ${thisfeat} ${filebase}
		if [ -f ${outputfile} ]
		then
			chmod 644 ${outputfile}
		fi
	fi
# Files named ${filelink}.router (i.e., prefix.loc.router) are considered
# router files. They contain paths to files that need to be updated
# with the new license file path
	bsf_filelink=$3
	if [ -f ${licdir}/${bsf_filelink}.router ]
	then
		while read router_file
		do
			fix_router_files  "${sunpro_router_rel_path}${outputfile}" $router_file
		done < ${licdir}/${bsf_filelink}.router
	fi
}
#
# Builds the elementary license file for the feature specified by $1
# Handles 2 cases:
#	- When the product package has been installed first and a location file
#	is present. In this case, build_sequence file will be used to create
#	a license file in the product directory with a sequence number attached.
#
#	- When the license file is installed before the product package.
#	In this case, build_sequence file will be used to create
#	a license file in the product directory with a sequence number attached.
#
build_elementary_file ()
{
	bef_i=$1
	feature=feature$bef_i
	flink=featlink$bef_i
	eval filelink=\$$flink
	fname=featfile$bef_i
	eval bef_filename=\$$fname
	if [ "${bef_filename}" = "" ]
	then
		return
	fi
	usf=usesamefile$bef_i
	eval usesamefile=\$$usf
	if [ -f ${licdir}/${filelink} ]
	then
		while read tempfile
		do
			if [ ! -d  ${tempfile} ]
			then
				echo Cannot access location ${tempfile} to store product licenses
				continue
			fi
			tempfile="$tempfile"/"$bef_filename"
			if [ ${usesamefile} -eq 0 ]
			then
				build_sequence_file $bef_i $tempfile ${filelink}
			else
				build_same_file $bef_i $tempfile ${filelink}
			fi
		done < ${licdir}/${filelink}
	else
		outputfile="${licdir}/${bef_filename}"
		if [ ${usesamefile} -eq 0 ]
		then
			build_sequence_file $bef_i $outputfile ${filelink}
		else
			build_same_file $bef_i $outputfile ${filelink}
		fi
	fi
# Now update the router files, if any
}

#
# Cycles through calls to echo_feature_lines in the building
# of the combined license file
#
setup_feature_lines ()
{
	sfl_filename=$1
	sfl_i=1
	while [ $sfl_i -le $num_feature ]
	do
		nodelock=featnodelock$sfl_i
		eval n1=\$$nodelock
		if [ ${n1} -ne 1 ]
		then
			echo_feature_lines $sfl_filename $sfl_i
		fi
		sfl_i=`expr $sfl_i + 1`
	done
}

#
# Builds the combined license file
#
build_combined_license_file ()
{
	bclf_filename="$1"
	echo_license_servers $bclf_filename
	echo_daemon_line $bclf_filename
	setup_feature_lines $bclf_filename
	chmod 644 ${bclf_filename}
}

#The main body of the shell script
# root test for 4.x and 5.x is different
if [ "$OSREL" = "4.x" ]
then
# 4.x test
	ret=`whoami`
	if [ $ret != "root" ]
	then
		echo You must be root to execute this script
		exit 1
	fi
else
# 5.x test
	ret=`id | grep root`
	if [ $? -ne 0 ]
	then
		echo You must be root to execute this script
		exit 1
	fi
fi
#Set initial install status
echo 0 > /tmp/exitcode.$$
# Check for args
# If first arg is "notfromtty", then this is being run from
# the lit or lit_tty, and we should not ask for user input
# on the daemon options file
#
if [ $# -ge 1 ]
then
	if [ $1 = $LIT_RUN ]
	then
		accept_input=0
	else
		accept_input=1
	fi
else
	accept_input=1
fi
if [ ! -d ${licdir} ]
then 
	mkdir -p ${licdir}
	retstatus=$?
	if [ ! -d ${licdir} ]
	then
		echo Cannot set up licensing directory ${licdir}
		echo mkdir exit status is $retstatus - Exiting
		exit $retstatus 

	fi
fi
	
#Make copy of combined file, in case user needs because we botched
if [ -f ${combined_file} ]
then
	cp ${combined_file} ${combined_file}.BAK
fi

if [ "$OSREL" = "4.x" ]
then
	opt="-ax"
else
	opt="-ef"
fi
sunpro_router_rel_prefix="/net"
#
# Check if automounter is running. If it isn't, then don't add /net/machine
# to the path stuffed into the router file

${LICPS} ${opt} | grep automount > /dev/null 2>&1

if [ $? -ne 0 ]
then
	sunpro_router_rel_path=""
else
	sunpro_router_rel_path="${sunpro_router_rel_prefix}/${thishost}"
fi

#
# Check if there are any licenses that are not nodelocked.
# If all licenses are nodelocked, then don't build the combined file
#
allnodelock=1
main_i=1
while [ $main_i -le $num_feature ]
do
	nodelock=featnodelock${main_i}
	eval n1=\$$nodelock
	if [ ${n1} -ne 1 ]
	then
		allnodelock=0
	fi
	main_i=`expr $main_i + 1`
done

if [ ${allnodelock} -eq 0 ]
then
	for host in ${hostname1} ${hostname2} ${hostname3} ${hostname4} ${hostname5}
	do
	
		if [ "${thishost}" = "${host}" ]
		then
			lic_server=1
		fi
	done
fi
# Check if this is a valid license server combination
# Do this check ONLY if there is already a combined license file on this
# machine (indicating that the machine is a license server), and
# and this machine is being used as a license server in the new license
if [ -f ${combined_file} -a ${lic_server} -eq 1 ]
then
	count_server=`grep SERVER ${combined_file} 2> /dev/null | wc -l`
	if [ ${count_server} -ne ${num_server} ]
	then
		echo "***The licenses requested cannot be installed!!!"
		echo "The license server combination requested does not match the"
		echo "existing combination in the license file - /etc/opt/licenses/licenses_combined."
		echo "The server count is different.***"
		exit 1
	fi
	cnt=1
	while [ ${cnt} -le ${num_server} ]
	do
		hn=hostname${cnt}
		eval host=\$$hn
		grep SERVER ${combined_file} | grep ${host} > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
			echo "***The licenses requested cannot be installed!!!"
			echo "The license server combination requested does not match the"
			echo "existing combination in the license file - /etc/opt/licenses/licenses_combined."
			echo "The server ${host} is not in the license file.***"
			exit 1
		fi
		cnt=`expr $cnt + 1`
	done

fi	

filename=${combined_file}
if [ $lic_server -eq 1 ]
then
	if [ -f ${combined_file} ]
	then
#
# The license file already exists, check daemon line and append feature
# lines to it
#
		echo_daemon_line $filename
		setup_feature_lines $filename
	else
		build_combined_license_file $filename

	fi
fi

# Merge daemon_options files gotten from read_from_file with
# existing daemon_options files
if [ $lic_server -eq 1 ]
then
	main_i=1
	while [ $main_i -le $num_daemon ]
	do
		dmnptr=daemon$main_i
		eval dmn=\$$dmnptr
		old_dmnpthptr=old_daemon_opt$main_i
		eval old_dmnpth=\$$old_dmnpthptr
		dmnpthptr=daemon_opt$main_i
		eval dmnpth=\$$dmnpthptr
		if [ "${old_dmnpth}" != "" ]
		then
			set_daemon_options=1
			merge_daemon_files $dmn $old_dmnpth $dmnpth
		fi
		main_i=`expr $main_i + 1`
	done

# Now write daemon options to the daemon options file
# Since we can't tell which file to write the options to, write them to
# all daemon options files listed
#
	if [ $num_daemon_opt -ne 0 ]
	then
		create_tmp_daemon_options_file
		main_i=1
		while [ $main_i -le $num_daemon ]
		do
			dmnpthptr=daemon_opt$main_i
			eval dmnpth=\$$dmnpthptr
			if [ "${dmnpth}" != "" ]
			then
				set_daemon_options=1
				merge_daemon_files $dmn $tmp_daemon_opt_path $dmnpth
			fi
			main_i=`expr $main_i + 1`
		done
	fi
fi
#
# Now build elementary license files for each product.
#

featloop=1
while [ $featloop -le $num_feature ]
do
	build_elementary_file $featloop
	featloop=`expr $featloop + 1`
done


#
# Now start/restart the license daemon, if it exists already
#

if [ ${lic_server} -eq 1 -a  -f ${license_daemon} -a  -f ${lic_startup_script} ]
then
#
# Check if any of the vendor daemons are running. If they are, and
# we can't find where they are being started from, then tell the user that
# the daemon must be killed before starting a new daemon.
#
	main_i=1
	dontstart=0
	while [ $main_i -le $num_daemon ]
	do
		daemon=daemon$main_i
		eval d1=\$$daemon
		if [ "${d1}" != "" ]
		then
		
			daemonpath=daemonpath$main_i
			eval d2=\$$daemonpath
			#
			# Is the vendor daemon already running
			#
			${LICPS} ${opt} | grep ${d1} | grep -v grep > /dev/null 2>&1
			if [ $? -eq 0 ]
			then
				#
				# has it been started from the correct license file
				#
				if [ "$OSREL" = "4.x" ]
				then
# Can't grep on $combined_file because the whole thing might not be in in the
# grep result
					${LICPS} ${opt} | grep ${d1} | grep "${licdir}" > /dev/null 2>&1
				else
					${LICPS} ${opt} | grep ${d1} | grep "${combined_file}" > /dev/null 2>&1
				fi
				if [ $? -ne 0 ]
				then
					echo License daemon ${d1} already executing
					if [ "$OSREL" = "4.x" ]
					then
# Can't grep on $combined_file because the whole thing might not be in in the
# grep result
						${LICPS} ${opt} | grep ${d1} | grep "${licdir}"
					else
						${LICPS} ${opt} | grep ${d1} | grep "${combined_file}"
					fi
				
					echo Only one such daemon can be running at any time
					echo This daemon must be killed before a new one can be started
					dontstart=1
				fi
			fi
		fi
		main_i=`expr $main_i + 1`
	done

	if [ $dontstart -eq 0 ]
	then
		${LICPS} ${opt} | grep ${lic_daemon_name} | grep -v grep > /dev/null 2>&1
		if [ $? -eq 0 ]
# Nonzero Exit status from grep indicates that it did not find the license
# daemon line
		then
			if [ $set_daemon_options -eq 0 ]
			then
				echo Rereading License File
				/etc/opt/licenses/lmreread -c ${combined_file}
			else
				count=0
				startdaemon=1
				echo Must take down and restart license daemon
				${licdir}/lmdown -c $combined_file -q
				${LICPS} ${opt} | grep ${lic_daemon_name} | grep -v grep > /dev/null 2>&1
				while [ $? -eq 0 ]
				do
					sleep 1
					count=`expr $count + 1`
					if [ $count -gt 60 ]
					then
						echo Unable to take down license daemon ${lic_daemon_name}
						startdaemon=0
						break
					fi
					${LICPS} ${opt} | grep ${lic_daemon_name} | grep -v grep > /dev/null 2>&1
				done
				if [ $startdaemon -eq 1 ]
				then
					echo Starting License Daemon
                                        ${lic_startup_script}
				fi
			fi

		else
			echo Starting License Daemon
                        ${lic_startup_script}
		fi
	fi
fi
#Make copy of combined file in case user needs
if [ -f ${combined_file} ]
then
	cp ${combined_file} ${combined_file}.DUP
fi
print_summary
status_cleanup
exit ${exitcode}
