#!/bin/sh
#
# @(#)postinstall	1.23 11/1/93 Copyright 1993 by Sun Microsystems, Inc.
#
# postinstall script for lmgrd

# WE WOULD LIKE TO START THE DAEMON UP AUTOMATICALLY FROM THE POSTINSTALL
# SCRIPT. HOWEVER, A HIGHLAND BUG PREVENTS THE DAEMON FROM BEING AUTOMATICALLY
# STARTED FROM PKGADD.
# SO, FOR NOW, THIS CAN"T BE DONE, AND WE DON"T EXECUTE THE SCRIPT AT ALL.
# INSTEAD WE WARN THE USER TO MANUALLY START THE DAEMON

licdir="/etc/opt/licenses"
lmgrdname="lmgrd.ste"
combined_lic="licenses_combined"
daemon_options="daemon_options"
pkg_daemon_options_file=$BASEDIR/SUNWste/license_tools/daemon_options

# If the daemon options file in /etc/opt/licenses/daemon_options is
# already present, then don't bother adding this stuff on, since
# it's already in there, or the user has explicitely removed it
if [ ! -f ${licdir}/${daemon_options} ]
then
	cat  ${pkg_daemon_options_file} > ${licdir}/${daemon_options}
fi
	
DONTDOTHIS=1
if [ $DONTDOTHIS -eq 0 ]
then

if [ ! -f $licdir/$combined_lic ]; then
	if [ -f $licdir/$combined_lic.BAK ]; then
			mv $licdir/$combined_lic.BAK $licdir/$combined_lic
			echo "Backup license file copied from $licdir/$combined_lic.BAK to $licdir/$combined_lic"
	fi
fi
ps -ef | grep ${lmgrdname} | grep -v grep > /dev/null 2>&1
if [ $? -eq 0 ]; then
	if [ -f $licdir/$combined_lic ]
	then
		${licdir}/lmdown -c $licdir/$combined_lic -q &
	fi
fi
#This code sucks, but, unfortunately, the daemon is not necessarily down
#when lmdown completes, so we don't want to start up a new daemon until
#the old one goes down.

i=0
downstatus=1
ps -ef | grep ${lmgrdname} | grep -v grep > /dev/null 2>&1
while [ $? -eq 0 ]
do
	sleep 1
	i=`expr $i + 1`
	if [ $i -gt 60 ]
	then
		downstatus=0
		break
	fi
	ps -ef | grep ${lmgrdname} | grep -v grep > /dev/null 2>&1
done
if [ $downstatus -eq 0 ]
then
	echo "Unable to take down license daemon ${lmgrdname}"
elif [ -f $licdir/$combined_lic ]; then
	echo "Starting License Manager Daemon"
	echo "It should be ready for use in 5 minutes"
	$licdir/$lmgrdname  -c $licdir/$combined_lic >> /tmp/license_log 2>&1 &
fi

else

# Display this message to the user untill we are able to automatically
# bring up the daemon
echo "IF YOU ARE NOT INSTALLING LICENSES USING lit, PLEASE RESTART THE\nLICENSE DAEMON By RUNNING THE COMMAND /etc/rc2.d/S85lmgrd"

fi
# END OF DONTDOTHIS CONDITIONAL

exit 0
