#!/bin/sh -f
### BEGIN INIT INFO
# this is a direct copy&change of the script provided by SWI 
# Provides:          swi-ws_hist
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Start/stop SWI-Prolog Real/hist server
### END INIT INFO

# Installation
#
#   1. Copy this file to /etc/init.d/swi-ws_hist
#   2. Edit the Configuration section below
#   3. Run
#	 % update-rc.d swipl-ws_hist defaults
#	 % /etc/init.d/swipl-ws_hist start

# Configuration section
#
SWIPL=/usr/local/users/nicos/local/git/bin/swipl
DIR=/srv/www/html/real/ws_hist
SCRIPT=ws_hist
USER=nicos
PORT=7171
WLOOP=real   # which wait loop for the web server
DAEMONARGS=
# Real specific: you can delete those if your system knows where to find
# the R libs and executables without your shell initialization files
local=/usr/local/users/nicos/local
PATH=$PATH:$local/bin
LDL=$local/lib/R/lib
R_HOME=$local/lib/R

# Uncomment to only listen for connections from localhost
# DAEMONARGS="$DAEMONARGS --ip=localhost"

WSHISTID=swipl-ws_hist-$PORT
PIDFILE=/var/run/$WSHISTID.pid
SYSLOG=/var/run/$WSHISTID

# End of normal configuration

. /lib/lsb/init-functions
test -f /etc/default/rcS && . /etc/default/rcS

DAEMONARGS="$DAEMONARGS --port=$PORT --user=$USER --wait=$WLOOP --fork"
if [ ! -z "$SYSLOG" ]; then DAEMONARGS="$DAEMONARGS --syslog=$SYSLOG"; fi
if [ ! -z "$PIDFILE" ]; then DAEMONARGS="$DAEMONARGS --pidfile=$PIDFILE"; fi

pidofserver()
{
if [ -f "$PIDFILE" ]; then
    cat "$PIDFILE"
  else
    ps aux | grep "[0-9] *$SWIPL.*--port=$PORT" 2>/dev/null | awk '{print $2}'
  fi
}

running()
{ if [ -z "$1" ]; then return 1; fi

  if kill -0 $1 2> /dev/null; then
    return 0
  else
    return 1
  fi
}

waitserver()
{ i=0;

  while running $1; do
    if [ $i = '60' ]; then
      return 1;
    else
      if [ $i = '0' ]; then
        echo -n " ... waiting "
      else
        echo -n "."
      fi
      i=$(($i+1))
      sleep 1
    fi
  done
}

case $1 in
        start)
	  log_daemon_msg "Starting hist server" "$WSHISTID"
	  # you can delete PATH, LD_LIBRARY_PATH AND R_HOME (probably) if you have them
	  # installed where you system knows where to find them without your personal 
	  # shell settings. Using a '&' at the end as real does a busy wait ? 
	  if (cd $DIR && env PATH=$PATH LD_LIBRARY_PATH=$LDL R_HOME=$R_HOME $SWIPL -f none -q -s $SCRIPT -- $DAEMONARGS ); then
	    log_end_msg 0
	  else
	    log_end_msg 1
	  fi
        ;;
        debug)
	  log_daemon_msg "Debugging hist server" "$WSHISTID"
	  if (cd $DIR && env PATH=$PATH LD_LIBRARY_PATH=$LDL R_HOME=$HOME $SWIPL -f none -q -s $SCRIPT -- $DAEMONARGS --ws_hist_debug=true); then
	    log_end_msg 0
	  else
	    log_end_msg 1
	  fi
        ;;
	stop)
	  log_daemon_msg "Stopping hist server" "$WSHISTID"
	  PID=$(pidofserver)
	  echo killing $PID
	  kill $PID
	  if waitserver $PID; then
	    log_end_msg 0
	  else
	    kill -9 $PID
	    waitserver $PID
	  fi
	;;
	restart)
	  $0 stop && $0 start
	;;
	status)
	  echo here argh
	  PID=$(pidofserver)
	  echo there $PID
	  if running "$PID"; then
	    echo "SWI-Prolog hist server is running (pid $PID)."
	    exit 0
	  else
	    echo "SWI-Prolog hist server is NOT running."
	    if [ -e $PIDFILE ]; then
	      exit 1
	    else
	      exit 3
	    fi
	  fi
	;;
	*)
	log_success_msg "Usage: /etc/init.d/swipl-ws_hist {start|stop|restart|status}"
	exit 1
esac
