#!/bin/sh
#
# @(#)postinstall	1.23 01 Nov 1993 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
#
# On the HP platform, because the /etc/rc2.d scripts are not executed at
# startup, this script will place an entry in the /etc/rc file which will
# invoke the license startup script when the machine is booted.  
#

licdir="${PKG_INSTALL_ROOT}/etc/opt/licenses"
lmgrdname="lmgrd.ste"
adjust_script="adjust_flexlm_owner -n"
combined_lic="licenses_combined"
daemon_options="daemon_options"
pkg_daemon_options_file=$BASEDIR/SUNWste/license_tools/daemon_options

#
# If on an HP machine, put an entry in the /etc/rc file to invoke the
# license manager daemon at boot time
#
OS=unknown
if [ -x ${PKG_INSTALL_ROOT}/bin/uname ]; then
	# HP platform
	OS=`${PKG_INSTALL_ROOT}/bin/uname -s`
elif [ -x ${PKG_INSTALL_ROOT}/usr/bin/uname ];then
	# Solaris 2 platform
	OS=`${PKG_INSTALL_ROOT}/usr/bin/uname -s`
fi

if [ ${OS} = "HP-UX" ]; then
	#
	# Check to see if the call is already in /etc/rc
	#
	TFILE=${PKG_INSTALL_ROOT}/tmp/rc.input$$
	grep /etc/rc2.d/S85lmgrd ${PKG_INSTALL_ROOT}/etc/rc > ${PKG_INSTALL_ROOT}/dev/null 2>&1
	if [ $? -eq 1 ]; then
	    # Add this to the /etc/rc file:
	    echo '' > ${TFILE}
	    echo '	###############################################' >> ${TFILE}
	    echo '	#' >> ${TFILE}
	    echo '	# Invoke the FLEX license manager' >> ${TFILE}
	    echo '	#' >> ${TFILE}
	    echo '	if [ -f /etc/rc2.d/S85lmgrd ]; then' >> ${TFILE}
	    echo '		nohup /etc/rc2.d/S85lmgrd >/tmp/licstart.log 2>&1' >> ${TFILE}
	    echo '	fi' >> ${TFILE}
	    echo '	#' >> ${TFILE}
	    echo '	##############################################' >> ${TFILE}

	    ed ${PKG_INSTALL_ROOT}/etc/rc <<CMDS > ${PKG_INSTALL_ROOT}/dev/null 2>&1
/^localrc
/: 
.r ${TFILE}
w
q
CMDS
	fi

	rm ${TFILE}
fi

# 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 > ${PKG_INSTALL_ROOT}/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 > ${PKG_INSTALL_ROOT}/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 > ${PKG_INSTALL_ROOT}/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 >> ${PKG_INSTALL_ROOT}/tmp/license_log 2>&1 &
fi

else

#
# execute the adjust_flexlm_owner script to update the license daemon
# startup scripts so that the license daemon is not owned by root
#
   if [ -f $licdir/$adjust_script ]; then
      echo "executing /etc/opt/licenses/adjust_flexlm_owner..."
      $licdir/$adjust_script
      if [ $? -ne 0 ]; then
         exit 1
      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

   else
      echo "An error occurred trying to execute the script,"
      echo "/etc/opt/licenses/adjust_flexlm_owner.  This script"
      echo "must be executed before the License Daemon can"
      echo "be started using /etc/rc2.d/S85lmgrd"
   fi

fi
# END OF DONTDOTHIS CONDITIONAL

exit 0
