#!/bin/bash
# condor   This is the glideinmonitor startup script
# chkconfig: 35 90 30
# description: Starts and stops the glidienmonitor

# Source function library or its emulation
if [ -f /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
else
    [ -f ./initscript_functions ] && . ./initscript_functions
fi

# LSB Exit codes for status:
#0    program is running or service is OK
#1    program is dead and /var/run pid file exists
#2    program is dead and /var/lock lock file exists
#3    program is not running
#4    program or service status is unknown
#5-99    reserved for future LSB use
#100-149    reserved for distribution use
#150-199    reserved for application use
#200-254    reserved
# LSB Exit codes for non status:
#1    generic or unspecified error (current practice)
#2    invalid or excess argument(s)
#3    unimplemented feature (for example, "reload")
#4    user had insufficient privilege
#5    program is not installed
#6    program is not configured
#7    program is not running
#8-99    reserved for future LSB use
#100-149    reserved for distribution use
#150-199    reserved for application use
#200-254    reserved
RET_OK=0
RET_ERROR=1
RET_STATUS_DEADWPIDFILE=1
RET_STATUS_DEADWLOCK=2
RET_STATUS_NOT_RUNNING=3
RET_STATUS_UNKNOWN=4
RET_BAD_SYNTAX=2
RET_NOT_IMPLEMENTED=3
RET_NO_PRIVILEGE=4
RET_NOT_INSTALLED=5
RET_NOT_CONFIGURED=6
RET_NOT_RUNNING=7

LOCK=/var/lock/subsys/glideinmonitor-webserver
RETVAL=0
BINARY=/usr/bin/glideinmonitor-webserver
CONFIG_FILE=/etc/glideinmonitor-webserver.conf

[ -f /etc/sysconfig/glideinmonitor ] && . /etc/sysconfig/glideinmonitor

help_usage() {
    echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
}

start() {
    action $"Starting GlideinMonitor Web server:" "$BINARY" -c "$CONFIG_FILE" && touch "$LOCK"
    RETVAL=$?
}

stop() {
    # TODO: how should it stop?
    action $"Stopping GlideinMonitor Web server:" rm -f "$LOCK"
    RETVAL=$?
}

restart() {
    stop
    start
}

status() {
    # TODO: how should it check?
    if [ -f $LOCK ]; then
        renew_status="running"
        RETVAL=$RET_OK
    else
        renew_status="stopped"
        RETVAL=$RET_STATUS_NOT_RUNNING
    fi
    echo "GlideinMonitor Web server is $renew_status."
}

case $1  in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|force-reload)
        restart
        ;;
  reload)
        # TODO: signal to reload configuration? is standard reload action implemented
        if [ "$2" != "" ]; then
            systemd_reload $2
        else
           echo "MUST SUPPLY A PID WITH THIS ARGUMENT"
        fi
        ;;
  condrestart)
        [ ! -f "$LOCK" ] && restart
        ;;
  status)
        status
        ;;
  *)
        help_usage
        exit $RET_NOT_IMPLEMENTED
esac

exit $RETVAL



case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    force-reload|restart)
        restart
        ;;
    status)
        "$GLIDEINMONITOR_CHECK" 
        RETVAL=$?
        #TODO: Should it print something?
        ;;
    reload)
        if [ "$2" != "" ]; then
            systemd_reload $2
        else
           echo "MUST SUPPLY A PID WITH THIS ARGUMENT"
        fi
        ;;
    *)
        help_usage
        #echo $"Usage: glideinmonitor- {start|stop|restart|status|reload}"
        exit $RET_NOT_IMPLEMENTED
esac

exit $RETVAL 
