#!/bin/sh
#
# /etc/init.d/luciadlicenseserver
#
# Init script of the Luciad License Service.
#
### BEGIN INIT INFO
# Provides: luciadlicenseserver
# chkconfig: 2345 20 80
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Luciad License Service
# Description: Luciad License Service
# processname: licenseserver
# pidfile: /run/luciad/licenseserver.pid
### END INIT INFO

# Source function library.
. /lib/lsb/init-functions

# The user under which the daemon should be started.
USER=luciad

# Home directory of the license server.
BIN_HOME=/opt/luciad/licenseserver

# Full location of the executable binary to start.
BIN_EXEC=$BIN_HOME/licenseserver

# The file in which to store the process id.
PID_FILE=/run/luciad/licenseserver.pid

# The log file for the license server
LOG_FILE=/var/log/luciad/licenseserver.log

# Directory _only_ containing the license files to serve.
LICENSE_DIR=/etc/luciad/licenses/

DAEMON=/usr/bin/daemon

start() {
    echo "Starting the Luciad License Service"
    mkdir `dirname $PID_FILE` > /dev/null 2>&1 || true
    mkdir `dirname $LOG_FILE` > /dev/null 2>&1 || true
    chown "$USER.$USER" `dirname $PID_FILE`
    chown "$USER.$USER" `dirname $LOG_FILE`
    $DAEMON --user=$USER --pidfile=$PID_FILE -- $BIN_EXEC -log $LOG_FILE $LICENSE_DIR/*.txt
}

stop() {
    echo "Shutting down the Luciad License Service"
    killproc -p $PID_FILE $BIN_EXEC
}

restart() {
  stop
  start
}

case "$1" in 
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       restart
       ;;
    status)
       status_of_proc -p $PID_FILE $BIN_EXEC "Luciad License Service"
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0
