#!/bin/bash
#
#       /etc/rc.d/init.d/stairwell
# stairwell     This shell script takes care of starting and stopping
#               the stairwell forwarder service.
#
#
# processname: /usr/bin/stairwell
# config: /etc/stairwell/config.json
# pidfile: /var/run/stairwell.pid
# lockfile: /var/lock/subsys/stairwell
# chkconfig: 2345 12 87
# description: stairwell is the stairwell forwarder service.

. /etc/init.d/functions

PROG=stairwell
LOGFILE=/var/log/stairwell/fileshipper.log
PIDFILE=/var/run/stairwell.pid
LOCKFILE=/var/lock/subsys/$PROG

# Allow anyone to run status
if [ "$1" = "status" ] ; then
        status $PROG
        RETVAL=$?
        exit $RETVAL
fi

test $EUID = 0  ||  exit 4

# Start the service
run() {
  echo -n $"Starting Stairwell forwarder: "
  cd /usr/bin
  LOG_TARGET="${LOGFILE}" ./$PROG -sysvdaemon > /dev/null 2> /dev/null < /dev/null &
  pid=$!

  sleep 1

  status $PROG > /dev/null
  if [[ $? -eq 0 ]]; then
    echo $pid > $LOCKFILE
    echo $pid > $PIDFILE
    disown $pid
    success
    echo
  else
    failure
    echo
  fi
}

# Start the service
start() {
  status $PROG > /dev/null
  # If application is running
  if [[ $? -eq 0 ]]; then
    status $PROG
  else
    run
  fi
}

condrestart(){
  status $PROG > /dev/null
  # If application is running
  if [[ $? -eq 0 ]]; then
    stop
    sleep 1
    start
  else
    status $PROG
  fi
}

stop() {
  echo -n "Stopping Stairwell forwarder: "
  killproc $PROG
  rm -f $LOCKFILE
  echo
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    sleep 1
    start
    ;;
  condrestart)
    condrestart
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 1
esac
exit 0
