#!/bin/bash
#
# Startup script for Power API
#
# chkconfig: 345 85 15     - start or stop process definition within the boot process
# description: Script for starting Power API as a daemon on system startup
# processname: papi

# Source function library.
. /etc/rc.d/init.d/functions

RUN_PORT=8080
SHUTDOWN_PORT=8818
CONFIG_DIRECTORY=/etc/repose

NAME=papi
DAEMON_HOME=/usr/lib/repose
LOG_PATH=/var/log/repose
LOG_FILE=current.log
START_PRIORITY=S85
TRUE=1
FALSE=0
EXIT_STATUS=0
daemon_running=$FALSE

###########################
# See if daemon is running
###########################
SeeIfDaemonIsRunning()
{
  PS_CMD="ps -e -o pid,args"

  # Get the list of processes whose status contains "papi" and filter
  # out the ones due to grep, [g]vi[m], this script executing, etc.
  #                    find daemon  no greps       no sh -x     no editing
  proc_list=`$PS_CMD | grep $NAME | grep -v grep | grep -v sh | grep -v vi`

  # producing a process id or a list of process ids.
   procs=`echo $proc_list | awk '{print $1}'`

  # If we're left with anything, then it means the daemon is running!
  if [ -n "$procs" ]; then
    daemon_running=$TRUE
  else
    daemon_running=$FALSE
  fi
}

###########################
# Start daemon
###########################
start()
{
  echo -n "Starting $NAME: "

  daemon java -jar /usr/lib/repose/$NAME.jar START -p $RUN_PORT -s $SHUTDOWN_PORT -c $CONFIG_DIRECTORY > $LOG_PATH/$LOG_FILE 2>&1 &
  EXIT_STATUS=$?

  echo
  [ $EXIT_STATUS -eq 0 ] && touch /var/lock/subsys/$NAME;

  # Make sure requests can get in
  /sbin/iptables -F
  /sbin/iptables -P INPUT ACCEPT
  /sbin/iptables -P OUTPUT ACCEPT  
}

###########################
# Stop daemon
###########################
stop()
{
  echo -n "Stopping $NAME: "

  daemon java -jar /usr/lib/repose/$NAME.jar STOP

  EXIT_STATUS=$?
  # remove lock file
  rm -f /var/lock/subsys/$NAME
  echo
}

###########################
# Get daemon status
###########################
getstatus()
{
  echo -n "Checking $NAME: "

  status $START_PRIORITY$NAME
  EXIT_STATUS=$?
}

# -----------------------------------------------------------------------------
# Script entry point...
# -----------------------------------------------------------------------------
# Switch to the daemon's home directory to do all of this...
cd $DAEMON_HOME

if [ $? -ne 0 ]; then
  echo "Unable to find $NAME's directory."
  exit 1
fi

EXIT_STATUS=0

# Check to see if the daemon is already running.
SeeIfDaemonIsRunning

case "$1" in
  start)
    if [ $daemon_running -eq $FALSE ]; then
      start
    else
      echo "$NAME appears already to be running."
    fi
    ;;

  stop)
    if [ $daemon_running -eq $TRUE ]; then
      stop
    else
      echo "$NAME does not appear to be running."
    fi
    ;;

  restart)
    if [ $daemon_running -eq $TRUE ]; then
      stop
      sleep 1
    fi
    start
    EXIT_STATUS=$?
    ;;

  status)
    getstatus
    ;;

  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}"
    exit 1
esac

exit $EXIT_STATUS
esac
