#! /bin/sh

#
# (c) Jan Gehring <jan.gehring@gmail.com>
# 
# vim: set ts=3 sw=3 tw=0:
# vim: set expandtab:
   
### BEGIN INIT INFO
# Provides:          Rex-Endpoint-HTTP
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: initscript to start rex http endpoint
# Description:       initscript to start rex http endpoint
### END INIT INFO


DAEMON=/usr/bin/rex_endpoint_http
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
NAME="Rex-Endpoint-HTTP"

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

log_daemon_msg()
{
   echo -n $*
}

log_end_msg()
{
   if [ "$1" = "0" ]; then
      echo "[ok]"
   else
      echo "[failed]"
   fi
}

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	
   hypnotoad $DAEMON
	RETVAL="$?"
	exit "$RETVAL"
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
	
   hypnotoad -s $DAEMON
	RETVAL="$?"
	exit "$RETVAL"
}

#
# Function that checks the status of the daemon/service
#
do_status()
{
   pidof $DAEMON >/dev/null 2>&1
   if [ "$?" = "0" ]; then
      log_daemon_msg "$NAME is running"
      log_end_msg 0
   else
      log_daemon_msg "$NAME is NOT running"
      log_end_msg 1
   fi
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
   pidof $DAEMON >/dev/null 2>&1
   ;;
  restart)
	log_daemon_msg "Restarting $NAME"
	do_stop
	sleep 1
	do_start
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart}" >&2
	exit 3
	;;
esac
