#!/bin/bash
# Copyright 2010 The Foundry
#
# chkconfig: 345 99 06
# description: Foundry License Server

FnLicDir=/usr/local/foundry/RLM

#The Foundry Web Server is on port 4102

#NOTE : The Foundry Server Utility uses this element to locate the server daemon, 
#if you alter this ensure it still points to the FLT tools dir 
TOOL_DIR=/usr/local/foundry/LicensingTools6.0

BIN_DIR=${TOOL_DIR}/bin
LIC_DIR=${FnLicDir}
LOG_DIR=${FnLicDir}/log
DEF_ULIMIT=`ulimit -n`
FOUNDRY_ULIMIT=10240

case "$1" in
'start')
  if [ ${DEF_ULIMIT} -lt ${FOUNDRY_ULIMIT} ]
  then
    echo Default descriptor limit ${DEF_ULIMIT} insufficient, increasing to ${FOUNDRY_ULIMIT} 
    ulimit -n ${FOUNDRY_ULIMIT}
    TEST_ULIMIT=`ulimit -n`
    if [ ${TEST_ULIMIT} -eq ${FOUNDRY_ULIMIT} ]
    then
      echo Successfully increased descriptor limit to ${TEST_ULIMIT} 
    else
      echo Failed to increase descriptor limit, currently set to ${TEST_ULIMIT} 
    fi
  fi
  echo Starting Server
  ${BIN_DIR}/rlm.foundry -ws 4102 -c ${LIC_DIR} -dlog ${LOG_DIR}/foundry.log & >> ${LOG_DIR}/boot.log 2>&1
  ${BIN_DIR}/rlmutil rlmstat -i foundry 2>&1 >> ${LOG_DIR}/foundry.log
  ;;

'stop')
  echo Stopping Server
  ${BIN_DIR}/rlmutil rlmdown foundry -c ${LIC_DIR} -q & >> ${LOG_DIR}/boot.log 2>&1 
  #No, if we do this it'll kill the web server and then they can't restart the server
  #using that. Let's try and play nice - RMCE
  #killall -s SIGTERM rlm.foundry 
  ;;

'reload')
  echo Reloading licenses
  ${BIN_DIR}/rlmutil rlmreread foundry 2>&1 >> ${LOG_DIR}/foundry.log
  ;;

'restart')
  $0 stop
  $0 start
  ;;

*)
   echo "usage: $0 {start|stop|reload|restart}"
   ;;
esac
