###############################################################################
#
# /etc/init.d/rcservices - Common services control script for rc scripts.
#
# Copyright (c) 2006, all rights reserved
# License- GPL
# Author-  Andre G Ancelin
# Company- Adtec Digital, Inc.
# Date-    2006-01-10
#
###############################################################################

# ensure that each of the daemons get the environment.
source /etc/env.global
export HOSTNAME=$(cat /etc/HOSTNAME)

#-------------------------------------------------------------------------------
# Return values (RESULT) accessible to LSB for all commands but status:
#
#  0 - success
#  1 - misc error
#  2 - invalid or excess args
#  3 - unimplemented feature (e.g. reload)
#  4 - insufficient privilege
#  5 - program not installed
#  6 - program not configured
#  7 - program not running
#  8 - program is running
#
declare -i RESULT
declare -i readonly SUCCESS=0
declare -i readonly FAILED=1
declare -i readonly INVALID_ARGS=2
declare -i readonly UNIMPLEMENTED_FEATURE=3
declare -i readonly INSUFFICIENT_PRIVELEGE=4
declare -i readonly PROGRAM_NOT_INSTALLED=5
declare -i readonly PROGRAM_NOT_CONFIGURED=6
declare -i readonly PROGRAM_NOT_RUNNING=7
declare -i readonly PROGRAM_IS_RUNNING=8
#
# Note that starting an already running service, stopping or restarting a 
# not-running service as well as the restart with force-reload
# (in case signalling is not supported) are considered a success.
#
print_result ()
{
  case $RESULT in
    $SUCCESS) # Success
      echo "..done"
      ;;
    $FAILED) # Failure
      echo "..failed"
      ;;
    $INVALID_ARGS) # Invalid arguments
      echo "..invalid arguments, failed"
      ;;
    $UNIMPLEMENTED_FEATURE) # Unimplemented feature
      echo "..unimplemented feature, missing"
      ;;
    $INSUFFICIENT_PRIVILEGE) # Insufficient privilege
      echo "..insufficient privilege, failed"
      ;;
    $PROGRAM_NOT_INSTALLED) # Program not installed
      echo "..program not installed, failed"
      ;;
    $PROGRAM_NOT_CONFIGURED) # Program not configured
      echo "..program not configured, failed"
      ;;
    $PROGRAM_NOT_RUNNING) # Program not running
      echo "..program not running, skipped"
      ;;
    $PROGRAM_IS_RUNNING) # Program already running
      echo "..program is running, skipped"
      ;;
    *) # Unknown
      echo "..failed (unknown)"
      ;;
  esac
}

#-------------------------------------------------------------------------------
# We have been passed an option for an rc* command in $1,
# call the do_* function based on this option.
# If the function does not exist, call the default do_usage().
#
dispatch_service ()
{
  RESULT=$SUCCESS
  if [ "`declare -f do_$1`" ]; then 
    do_$1
  else 
    do_usage $0
  fi
  exit $RESULT
}

#-------------------------------------------------------------------------------
# Default "do_status" function.
# Result has a slightly different meaning for the status command:
#  0 - service running
#  1 - service dead, but /var/run/  pid  file exists
#  2 - service dead, but /var/lock/ lock file exists
#  3 - service not running
#
declare -i readonly RUNNING=0
declare -i readonly DEAD_WITH_PID=1
declare -i readonly DEAD_WITH_LOCK=2
declare -i readonly NOT_RUNNING=3
#
do_status ()
{
  echo -n "Status for ${DAEMON}: "
  declare readonly EXE=${DAEMON##*/}
  RESULT=$NOT_RUNNING
  if [ -r "/var/run/${EXE}.pid" ] ; then
    RESULT=$DEAD_WITH_PID
    declare readonly PID=`cat /var/run/${EXE}.pid`
    if [ -e /proc/$PID ] ; then
      declare readonly PROCNAME=`/bin/ps h -p $PID -C bind`
      if [ -n "$PROCNAME" ]; then
        RESULT=$RUNNING
      fi
    fi
  fi
  case $RESULT in
    $RUNNING)        echo "running";;
    $DEAD_WITH_PID)  echo "dead, but has /var/run/${EXE}.pid file";;
    $DEAD_WITH_LOCK) echo "dead, but has /var/lock/ file";;
    $NOT_RUNNING)    echo "unused";;
    *)               echo "unknown";;
  esac
}

#-------------------------------------------------------------------------------
# Default "do_tryrestart" function.
# Stop the service and if this succeeds (service was running), start it again.
#
do_tryrestart ()
{
  do_stop && do_start
}

#-------------------------------------------------------------------------------
# Default "do_restart" function.
# Stop service and regardless of whether it was running or not start it again.
#
do_restart ()
{
  do_stop
  do_start
}

#-------------------------------------------------------------------------------
# Default "do_forcereload" function.
# Signal daemon to reload its config. Most daemons do this on signal 1 (SIGHUP).
# If it does not support it, restart.
#
do_forcereload ()
{
  if [ $RELOAD_SIGNAL ]; then
    do_reload
  else
    do_restart
  fi
}

#-------------------------------------------------------------------------------
# Default "do_reload" function.
# Like force-reload, but if daemon does not support signaling do nothing.
#
do_reload ()
{
  echo -n "Reloading configuration for $DAEMON "
  if [ $RELOAD_SIGNAL ]; then  
    echo -n "(signal $RELOAD_SIGNAL) "
    start-stop-daemon --quiet --stop --exec $DAEMON --signal $RELOAD_SIGNAL
    case $? in
      0) RESULT=$SUCCESS;;              # Started OK
      1) RESULT=$PROGRAM_NOT_RUNNING;;  # No process running
      *) RESULT=$FAILED;;               # Trouble
    esac
  else
    RESULT=$UNIMPLEMENTED_FEATURE
  fi
  print_result
}

#-------------------------------------------------------------------------------
# Default "do_usage" function.
#
do_usage ()
{
  echo "Usage: $1 {start|stop|status|tryrestart|restart|forcereload|reload}"
  RESULT=$FAILED
}

#------------------------------------------------------------------------------
# Check to make sure daemon file exists (if defined).
# If not, exit with the LSB program not installed code.
#
if [ $DAEMON ]; then
  if [ ! -x $DAEMON ]; then
    echo "ERROR- $DAEMON does not exist or is not executable."
    exit $PROGRAM_NOT_INSTALLED
  fi  
fi
