#!/bin/sh
#
# @(#)lmgrd     1.11 06/06/96 Copyright (c) 1993 by Sun Microsystems, Inc.
#
# /etc/rc2.d/S85lmgrd - Start up the license manager daemon
#
# This script has been updated by patch #104174-01 for sparc-S2,
# #104187-01 for ppc-S2, or #104186-01 for intel-S2 and #104217-01 for SunOS
#

#
# /etc/opt/licenses holds the license manager, links to
# several license management utilities and the license
# files.  This directory is only required on the license
# server. 
#
licdir=/etc/opt/licenses

#
# The name of the FLEXlm license daemon is "lmgrd",
# to avoid conflict with daemons shipped by vendors
# other than Sun, the name has been changed to lmgrd.ste.
# As of FLEXlm 4.1, an "lmgrd" from any vendor can be
# used. 
#
lmgrd=lmgrd.ste

#
# lmdown is the best way to take down a server, if it is
# not available, kill can be used. Do not use kill -9 as
# the vendor daemons will not be properly taken down.
#
lmdown=lmdown

#
# licenses_combined is the file which should contain all the
# licenses for this server. 
#
licfile=licenses_combined

#
# /usr/tmp/license_log will contain output from the license manager.
# When the license manager is re-started, existing log file will be
# moved to /usr/tmp/license_log.<date>.  Format for <date> is
# <Day><Month><Year><Hour><Minute><Second>.  The log file is moved
# each time the license manager is started to help manage the size
# of the logs. 
#
logfile=/usr/tmp/license_log

#
# The script, /etc/opt/licenses/lic_srvr_start, is used to start the
# license daemon, lmgrd.ste.  This additional script is used so that
# this script does not have to care what shell the user who is to start
# the daemon is running.
#
lic_srvr_start=/etc/opt/licenses/lic_srvr_start

if [ -f /vmunix ]
then
  opt="-ax"
  LICPS="ps"
else
  opt="-ef"
  if [ -f /usr/bin/ps ]; then
    LICPS="/usr/bin/ps"
  elif [ -f /bin/ps ]; then
    LICPS="/bin/ps"
  else
    LICPS="ps"
  fi
fi

if [ "$1" = "" -o "$1" = "start" ]; then
  if [ -f $licdir/$lmgrd -a -f $licdir/$licfile ]; then
    ${LICPS} ${opt} | grep ${lmgrd} | grep -v grep > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
      echo "License Manager daemon already running"
    else
      if [ -f $logfile ]; then
# FLEXLM_OWNER_CHG_1
        chown FLEXLM_OWNER $logfile
      fi
# FLEXLM_OWNER_CHG_2
      su - FLEXLM_OWNER -c $lic_srvr_start
    fi
  fi
  
elif [ "$1" = "stop" ]; then
  if [ -f $licdir/$lmgrd -a -f $licdir/$licfile -a -f $licdir/$lmdown ]; then
    ${LICPS} ${opt} | grep ${lmgrd} | grep -v grep > /dev/null 2>&1
    if [ $? -eq 1 ]
    then
      echo "License Manager not running"
    else
      echo "Stopping License Manager Daemon using ${lmdown}"
      $licdir/${lmdown} -c $licdir/$licfile
    fi
  else
    LMGRD_PID=`${LICPS} ${opt} | grep ${lmgrd} | grep -v grep | awk '{ print $2 }'`
      if [ ! -z "${LMGRD_PID}" ]; then
	echo "Stopping License Manager Daemon with \"kill\""
	kill ${LMGRD_PID}
      else
	echo "License Manager not running"
      fi
  fi
  
else
  
  echo "Usage: $0 [ start | stop ]"
fi  
