#! /bin/sh
#
# Example startup script for FLEXnet applications, on Redhat systems this file
# would go in the /etc/rc.d/init.d directory (make sure it's marked executable
# after copying it there), and then the command:
#
#       'chkconfig flexnet-service on'
#
# can be used to tell the system to run it on startup.
#
# On Solaris this script must be copied at least to /etc/rc3.d with an 
# appropriate file name to control when the FLEXnet application is started.
#
######################################################################
# Automatically create rc*.d symlinks for startup/shutdown
# Use sudo /sbin/chkconfig --add flexnet-service
# Also --reset option will recreate the links if messed up.
# Syntax     chkconfig levels start-priority stop-priority
# chkconfig: 2345 98 35
# description: flexnet-service is the FLEXnet application server.
######################################################################

# Source function library.
if [ -x /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi

BIN_DIR="/opt/FLEXnet/agent/site/bin"
FLEXNET_USER="flexcls"

### logfile for any output that doesn't end up in normal logfiles
CONSOLE_LOG="/opt/FLEXnet/agent/logs/console.err"

# Path to the tomcat launch script (direct don't use wrapper)
FLEXNET_SCRIPT="$BIN_DIR/flexnet"

RETVAL=0

start() {
    echo -n "Starting FLEXnet Application:"
    if [ -x /etc/rc.d/init.d/functions ]; then
        daemon --user $FLEXNET_USER "$FLEXNET_SCRIPT" -nowait start
    else
        su $FLEXNET_USER -c "$BIN_DIR/flexnet -nowait start" > "$CONSOLE_LOG" 2>&1 &
    fi

    echo
    RETVAL=$?
}

stop() {
    echo -n "Shutting down FLEXnet Application:"

    if [ -x /etc/rc.d/init.d/functions ]; then
        daemon --user $FLEXNET_USER "$FLEXNET_SCRIPT" stop
    else
        su $FLEXNET_USER -c "$FLEXNET_SCRIPT stop"
    fi

    echo
    RETVAL=$?
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    sleep 2
    start
    ;;
  *)
    echo "Usage: flexnet-service {start|stop|restart}"
    exit 1
    ;;
esac

exit $RETVAL
