#! /bin/sh

####### PMES service variables ######
TOMCAT_HOME=/opt/pmes-framework/pmes-service/apache-tomcat
JAVA_SAGA_LOCATION=/opt/pmes-framework/pmes-service/JAVA_SAGA
BESCONFIG_LOCATION=/opt/pmes-framework/pmes-service/conf
CATALINA_OPTS="-Dlog4j.configuration=file:$BESCONFIG_LOCATION/log4j.properties -Dsaga.location=$JAVA_SAGA_LOCATION"
PATH=$PATH:/usr/local/bin

export TOMCAT_HOME JAVA_SAGA_LOCATION BESCONFIG_LOCATION CATALINA_OPTS PATH

####### Load storage clients variables #######
if [ -f /etc/profile.d/storage-clients.sh ]; then
  . /etc/profile.d/storage-clients.sh
fi

### BEGIN INIT INFO
# Provides:          monitoring
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: PMES service Daemon
# Description:       PMES service Daemon
### END INIT INFO

# Author: Roger Rafanell <roger.rafanell@bsc.es>

NAME=pmes-framework
PIDFILE=/var/run/$NAME.pid
SERVICE=/opt/pmes-framework/pmes-service/apache-tomcat
SCRIPTNAME=/etc/init.d/$NAME

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting PMES service"
    sudo -E -u pmes -H $SERVICE/bin/catalina.sh start
    ;;
  stop)
    echo "Shutting down PMES service"
    sudo -E -u pmes -H $SERVICE/bin/catalina.sh stop > /dev/null 2>&1
    sleep 4
    ps -o pid -u pmes | xargs kill -1 > /dev/null 2>&1
    ;;
   restart)
    echo "Restarting PMES service"
    sudo -E -u pmes -H $SERVICE/bin/catalina.sh stop > /dev/null 2>&1
    sleep 4
    ps -o pid -u pmes | xargs kill -1 > /dev/null 2>&1
    sleep 3
    sudo -E -u pmes -H $SERVICE/bin/catalina.sh start
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart}"
    exit 1
    ;;
esac

exit 0

