#!/bin/sh

### BEGIN INIT INFO
# Provides:     jameica
# Required-Start:   $local_fs $remote_fs $network
# Required-Stop:    $local_fs $remote_fs $network
# Default-Start:    3 5
# Default-Stop:     0 1 2 6
# Short-Description:    Jameica Application Server
# Description:      Starts the Jameica Application Server
### END INIT INFO

#
# Determine the base and follow a runlevel link name.
#
base=${0##*/}
link=${base#*[SK][0-9][0-9]}

JAVA=`which java`
BASEDIR=`dirname $0`

### Please do not edit anything below this line! ########################
#                                                                       #

. /etc/rc.status
. "$BASEDIR/.jameica"

if [ -f "$WORKDIR/.jameica" ]; then
  . "$WORKDIR/.jameica"
fi

rc_reset

return=$rc_done
case "$1" in
    start)
      mkdir -p $WORKDIR
      cd $BASEDIR
      echo -n "Starting Jameica "
      echo $PASSWORD | $JAVA -Djava.net.preferIPv4Stack=true -Xmx512m -jar jameica-linux.jar -d -f $WORKDIR > /dev/null & PID=$!
      rc_status -v
      echo $PID > $PIDFILE
    ;;
    stop)
     if test -e $PIDFILE ; then
       echo -n "Shutting down Jameica "
       kill `cat $PIDFILE`
       rm $PIDFILE
       if [ -f $LOCKFILE ]; then
         rm $LOCKFILE
       fi
       rc_status -v
     else
      echo "Not running"
     fi
    ;;
    reload|restart)
      $0 stop
      sleep 1
      $0 start
    ;;

    status)
      echo -n "Checking for Jameica "
      if test -e $PIDFILE ; then
        echo " seems running"
      else
        echo " not running"
      fi
    ;;
    *)
      echo "Usage: $0 {start|stop|restart|status}"
      exit 1
  ;;
esac
rc_exit

