#! /bin/sh -l
#  /etc/init.d/aof-extract-service

### BEGIN INIT INFO
# Provides:          aof-extract-service
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Short-Description: Starts the aof-extract-service
# Description:       This file is used to start the daemon
#                    and should be placed in /etc/init.d
### END INIT INFO

# Author:   timethy <taubnert@student.ethz.ch>
# Url:      
# Date:     23/03/2014

NAME="%s"
VERSION="%s"
DESC="%s"

# The path to Jsvc
EXEC="/usr/bin/jsvc"

# The path to the folder containing aof-extract-service.jar
FILE_PATH="/usr/share/java/$NAME"
VAR_PATH="/var"

# Our classpath including our jar files
CLASS_PATH="*"

# The fully qualified name of the class to execute
CLASS="%s"

# Any command line arguments to be passed to the our Java Daemon implementations init() method
ARGS="-c /etc/aof/$NAME.conf"

#The user to run the daemon as
USER="root"

# The file that will contain our process identification number (pid) for other scripts/programs that need to access it.
PID="$VAR_PATH/run/$NAME.pid"

# System.out writes to this file...
LOG_OUT="$VAR_PATH/log/$NAME.log"

# System.err writes to this file...
LOG_ERR="$VAR_PATH/log/$NAME.err"

jsvc_exec()
{  
    cd $FILE_PATH
    $EXEC -cp "$CLASS_PATH" -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS $ARGS
}

case "$1" in
    start) 
        echo "Starting the $DESC..."       
       
        # Start the service
        jsvc_exec
       
        echo "The $DESC has started."
    ;;
    stop)
        echo "Stopping the $DESC..."
       
        # Stop the service
        jsvc_exec "-stop"      
       
        echo "The $DESC has stopped."
    ;;
    restart)
        if [ -f "$PID" ]; then
           
            echo "Restarting the $DESC..."
           
            # Stop the service
            jsvc_exec "-stop"
           
            # Start the service
            jsvc_exec
           
            echo "The $DESC has restarted."
        else
            echo "Daemon not running, no action taken"
            exit 1
        fi
            ;;
    *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart}" >&2
    exit 3
    ;;
esac
