#!/bin/ksh
#
# Script to start, stop, install, and uninstall the FLEXlm license manager
# for Engineous products.
#
# run 'flexlm' with no arguments for a usage description.

#######################################
#
# Locally Customizable Settings
#

# set -x  # uncomment this line to get debugging output during boot.

# You must edit the next two lines to suit your installation
# Normally this is done by the installer.

# FLEXLM_HOME - the absolute path to where FLEXlm was installed.
#   If this is not set, the directory of this script is used.
#   this is always set when installing this script into /etc/init.d
FLEXLM_HOME=

# FLEXLM_USER - the user who should run the license manager daemon.
#   Leave this blank to run the license manager as root.
FLEXLM_USER=""

# PATH is required for HP-UX
PATH=/sbin:/usr/sbin:/usr/bin:/bin:/usr/ucb:/usr/bsd
export PATH
unset cd
unset CDPATH

# Try to default FLEXLM_HOME if it is not set
if [ "$FLEXLM_HOME" = "" ]; then
    dir=`dirname "$0"`
    dir=`(cd "$dir"; cd ..; /bin/pwd)`
    FLEXLM_HOME="${dir}/license"
    #echo "FLEXLM_HOME=$FLEXLM_HOME"
fi

# The following should be OK but you may want to change them.

# LM_LICENSE_FILE - location of the license file
LM_LICENSE_FILE="${FLEXLM_HOME}/license.dat"

# lmgrd_log - location of the FLEXlm log file
# Make sure that ${lmgrd_log} is writable by ${FLEXLM_USER}
lmgrd_log="${FLEXLM_HOME}/debug.log"

#
# End of Locally Customizable Settings
#
#######################################

# License programs
lmgrd="${FLEXLM_HOME}/lmgrd"
lmdown="${FLEXLM_HOME}/lmdown"

# Make the license file generally available to programs
export LM_LICENSE_FILE


# Function to kill a named process.
# This is overridden on Linux by /etc/rc.0/init.d/functions
killproc() {
    pid=`ps -e | awk '$4 == name { print $1 }' name=$1 -`
    [ "$pid" != "" ] && kill $pid
}

# Run with -p -2 if FLEXLM_USER is set (limit lmdown to root user)
if [ -n "${FLEXLM_USER}" ]
then
    lmgrd_flags="-p -2 -x lmremove"
else
    lmgrd_flags="-x lmremove"
fi

# base directory for all rc files.  Contains directories rc2.d, rc0.d
# This is overridden below for some platforms.
rcbase=/etc/rc.d

# Names of the startup and shutdown scripts as installed in $rcbase
startfile=rc2.d/S45FiperLicense
killfile=rc0.d/K30FiperLicense
scriptfile=init.d/FiperLicense

# Command to include in front of the start command
# This is set below for some platforms
preStart=

#
# ------ Platform-specific settings
#
os=`uname`
case "$os" in
  HP-UX)
    rcbase=/sbin
    ;;

  AIX)
    # AIX normally uses /etc/inittab to run startup scripts
    # but as of 5.1, it also has a BSD-style /etc/rc.d
    rcbase=/etc/rc.d
    # AIX doesn't use kill files
    killfile=""
    ;;

  Linux)
    rcbase=/etc/rc.d

    # SOME version of linux use a special function library from RC
    # scripts.  Use it if it is available.
    if [ -f ${rcbase}/init.d/functions ]; then

        # define preStart to use these functions.
        preStart=". ${rcbase}/init.d/functions; daemon "

        # Source Linux init function library.
        # This redefined killproc, and defines 'daemon'
        . ${rcbase}/init.d/functions
    fi

    # SuSE linux doesn't have /etc/rc.d/init.d
    if [ ! -d /etc/rc.d/init.d ]; then
        # SuSE - scripts in /etc/init.d and symlink from /etc/init.d/rc3.d
        rcbase=/etc/init.d
        scriptfile=FiperLicense
    fi
    ;;

  SunOS)
    rcbase=/etc
    ;;
esac


#
# Start/stop flexlm license daemon (lmgrd)
#
RETVAL=0
case "${1}" in

  start)
    # Start the license manager.  Can be run manually or from /etc/rc
    # Notice how stdin and stdout are closed to avoid problems with how Java
    # waits for processes to complete

    if [ -x "${lmgrd}" -a -r "${LM_LICENSE_FILE}" ] ; then
        echo "Starting Fiper license manager daemon"
        # Clear the log file
        echo "" > ${lmgrd_log}
        
        # CD to a known writable directory.
        # also, this allows the CD to eject when the installer starts flexlm
        cd ${TMPDIR:-/usr/tmp}

        # Avoid 'su' if this script is run by FLEXLM_USER
        if [ "$FLEXLM_USER" != "" ]; then
            # Solaris requires /usr/xpg4/bin/id
            id=/usr/xpg4/bin/id
            if [ ! -x $id ]; then
                id=/usr/bin/id
            fi
            user=`$id -u -n`
            if [ "$user" = "$FLEXLM_USER" ]; then
                FLEXLM_USER=""
            fi
        fi

        if [ -n "${FLEXLM_USER}" ] ; then
            su ${FLEXLM_USER} -c "${preStart} ${lmgrd} ${lmgrd_flags} -c ${LM_LICENSE_FILE} -l ${lmgrd_log} </dev/null >/dev/null 2>/dev/null"
        else
            eval ${preStart} ${lmgrd} ${lmgrd_flags} -c ${LM_LICENSE_FILE} -l ${lmgrd_log} </dev/null >/dev/null 2>/dev/null
        fi
        RETVAL=$?
        if [ $RETVAL -eq 0 ] ; then
                echo "Fiper license manager successfully started"
            else
                echo "Fiper license manager daemon startup failed."
            fi
    else
        echo "${0}: cannot execute ${lmgrd} and/or read ${LM_LICENSE_FILE}"
        RETVAL=1
    fi
    ;;

  stop)
    # Stop the license manager.  Can be called manually or from /etc/rc
    if [ -x "${lmdown}" -a -r "${LM_LICENSE_FILE}" ]
    then
        echo "Stopping license manager daemon"
        # Close stdin.  Otherwise lmdown can hang if multiple servers use the 
		# default port.  If multiple servers use the same port, lmdown won't
	    # stop any of them.  This is preferrable to hanging.
        ${lmdown} -q -c ${LM_LICENSE_FILE} </dev/null
        RETVAL=$?
    else
        echo "Cannot stop license manager - $lmdown or $LM_LICENSE_FILE missing"
        RETVAL=1
    fi
    ;;

  start_msg)
    # Used only by HP-UX rc script
    echo "Starting the SIMULIA license manager"
    ;;

  stop_msg)
    # Used only by HP-UX rc script
    echo "Stopping the SIMULIA license manager"
    ;;

  restart)
    # called only by Linux RC Script
    $0 stop
    $0 start
    RETVAL=$?
    ;;

  reload)
    # Called only by Linux RC Script
    echo -n "Reloading Fiper license manager daemon: "
    ${FLEXLM_HOME}/lmreread -c ${LM_LICENSE_FILE}
    RETVAL=$?
    echo
    ;;

  status)
    # Called only by Linux RC script
    status lmgrd
    RETVAL=$?
    ;;

  install)
    # Install this script in the rc directories so it will
    # automatically start after a reboot
    if [ ! -d ${rcbase}/rc2.d ]; then
        echo "Cannot install Fiper startup script.  Directory ${rcbase}/rc2.d does not exist"
        exit 2
    fi

    # Silently Remove any existing scripts to allow clean re-install
    if [ "$os" = "AIX" ]; then
        # AIX is different
        rm -f ${rcbase}/${startfile} >/dev/null 2>/dev/null
    else
        # Other platforms must remove more.
        rm -f ${rcbase}/${killfile} ${rcbase}/${killfile2} ${rcbase}/${startfile} ${rcbase}/${scriptfile} >/dev/null 2>/dev/null
    fi

    # Actually install the scripts
    # AIX has to be different!
    if [ "$os" = "AIX" ]; then
        dest=${rcbase}/${startfile}
    else
        dest=${rcbase}/${scriptfile}
    fi

    # Copy to destination, setting FLEXLM_HOME as absolute path
    sed -e "s:^FLEXLM_HOME=.*:FLEXLM_HOME='$FLEXLM_HOME':" "${0}" > $dest
    chmod 775 $dest
    RETVAL=$?

    # Finish the install.  AIX is already done
    if [ "$os" != "AIX" -a $RETVAL -eq 0 ]; then
        # symlink from  rc2.d and rc0.d
        ln -s ../${scriptfile} ${rcbase}/${startfile} && \
        ln -s ../${scriptfile} ${rcbase}/${killfile}
        RETVAL=$?
    fi

    if [ $RETVAL -eq 0 ]; then
       echo "The license manager is not started by this install."   
    fi
    ;;

  uninstall)
    if [ "$os" = "AIX" ]; then
        # AIX has no kill script and no init.d
        echo "Removing Fiper startup script from ${rcbase}/${startfile}"
        rm -f ${rcbase}/${startfile}
        RETVAL=$?
    else
        echo "Removing Fiper startup script from ${rcbase}/rc2.d directory"
        rm -f ${rcbase}/${killfile} ${rcbase}/${startfile} ${rcbase}/${scriptfile}
        RETVAL=$?
    fi
    ;;

  *)
    # Any thing else, offer help and stop
    echo "Usage: ${0} {start|stop|install|uninstall}"
    echo "   start   - start the license manager"
    echo "   stop     - stop the license manager if it is running"
    echo "   install   - install this script so it runs at system startup"
    echo "   uninstall - undo the effects of '${0} install"
    RETVAL=2
    ;;
esac

exit $RETVAL
