#!/bin/sh
#
# ees        Starting, stopping and restarting the ees daemon.
#
# Author:    Aram Verstegen
#
# chkconfig: - 99 01
# description: Starts and stops EES daemon

prefix=/usr
exec_prefix=/usr
prog="ees"
fullprog="/usr/sbin/ees"
configfile="/etc/ees.conf"
lockfile="/var/lock/subsys/ees"
nonprivaccount="ees"

# Source function library
if [ -f /etc/redhat-release ]; then
  # Redhat-style
  . /etc/rc.d/init.d/functions

  start_ees_daemon="daemon --user $nonprivaccount $fullprog $configfile"
elif [ -f /etc/debian_version ]; then
  # Debian-style
  . /lib/lsb/init-functions
  echo_success(){
    echo success
  }
  echo_failure(){
    echo failure
  }
  start_ees_daemon="start-stop-daemon --start --chuid $nonprivaccount --exec $fullprog $configfile"
elif [ -f /etc/SuSE-release ]; then
  # SUSE-style
  . /etc/rc.status
  echo_success(){
    rc_reset
    rc_status -v
  }
  echo_failure(){
    rc_failure
    rc_status -v
  }
  start_ees_daemon="start-stop-daemon --start --chuid $nonprivaccount --exec $fullprog $configfile"
else
  echo_success(){
    echo success
  }
  echo_failure(){
    echo failure
  }
  echo -n "Warning! Unable to determine the correct daemon runner for your distribution."
  start_ees_daemon="exec $fullprog $configfile"
fi

start() {
    echo -n "Starting EES: "
    # Check if there should be an EES instance running
    if [ -f $lockfile ]; then
      eespid=`pidofproc $fullprog`
      if [ -z "$eespid" ]; then
          echo -n "removing stale lockfile... "
          rm -f $lockfile
      else
          # EES already running
          echo -n "already running. "
          echo_failure
          echo
          exit $?
      fi
    fi
    # Starting EES daemon now.
    ${start_ees_daemon} && touch $lockfile
    if [ $? -eq 0 ]; then
        echo_success 
    else
        echo -n "EES failed to start. "
        echo_failure
    fi
    echo
}

stop()  {
    # Stopping EES
    echo -n "Stopping EES: "
    if [ -f $lockfile ]; then
        # Killing EES now.
        kill -TERM `pidofproc $fullprog`
        rm -rf $lockfile
        echo_success
    else
        # No EES running, nothing to kill
        echo -n "no EES running. "
        echo_failure
    fi
    echo
}

restart(){
    # Restarting EES
    stop
    start
}

reload(){
    echo -n "Restarting EES. "
    eespid=`pidofproc $fullprog`
    if [ -f $lockfile ]; then
        kill -HUP $eespid 
        if [ $? -eq 0 ]; then
            echo_success
        else
            echo -n "unable to restart. dead? "
            echo_failure
        fi
    else
        echo -n "EES not running. "
        echo_failure
    fi
    echo
}

# See how we are called
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    reload)
	reload
	;;
    status)
        if [ -f $lockfile ]; then
            echo "ees is running"
        else
            echo "ees is stopped"
        fi
        ;;
    *)
        echo "Usage $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0
