#!/bin/sh
# /etc/init.d/tomcat-esup
#

### BEGIN INIT INFO
# Provides:          tomcat-esup
# Required-Start:    
# Required-Stop:     
# Should-Start:      $remote_fs $all
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Tomcat-Esup
# Description:       Tomcat-Esup
### END INIT INFO

# name displayed in output messages of the script
PROG_TOMCAT_NAME_DISPLAY="EsupPortail Tomcat"

# Used only with the restart target : 
# time waiting so that tomcat stops with a classical shutdown 
# after this time, if tomcat is not stopped, we kill it
TIMEOUT=60

# Directory of Tomcat
TOMCAT_DIR=/opt/tomcat-esup

# user_uid who starts effectivly tomcat
TOMCAT_USER=esup

# used with the stop target
# kill tomcat if we can't do a shutdown because of a java.net.ConnectException
KILL_IF_ConnectException=1

. /opt/esup-env/env.sh

# valeur attendue de entName : ent1 ou ent2 ou ent3 ...
entName=$(hostname | cut -c2-)

start () {
    if test `ps -edf | grep "$TOMCAT_DIR/bin/bootstrap.jar" |  grep -v grep | wc -l` -gt 0
    then
	echo "$PROG_TOMCAT_NAME_DISPLAY is already started - call the restart target if you want to restart it"
    else
        echo "Starting $PROG_TOMCAT_NAME_DISPLAY ..."
	# start daemon
	su $TOMCAT_USER -c  "cd $TOMCAT_DIR/bin && ./startup.sh &> /dev/null 2>&1"
    fi
}
 
stop () {
    if test `ps -edf | grep "$TOMCAT_DIR/bin/bootstrap.jar" |  grep -v grep | wc -l` -gt 0
    then
        echo "Stopping $PROG_TOMCAT_NAME_DISPLAY ..."
	stop_log_error=`su $TOMCAT_USER -c "cd $TOMCAT_DIR/bin && ./shutdown.sh" 2>&1`
	#echo $stop_log_error
	#echo ''
	if test $KILL_IF_ConnectException -eq '1' -a `echo $stop_log_error | grep "java.net.ConnectException" | wc -l` -gt 0
	    then
	        echo "problem : java.net.ConnectException - we can't stop $PROG_TOMCAT_NAME_DISPLAY usually"
		    kill
		    fi
    while test `ps -edf | grep "$TOMCAT_DIR/bin/bootstrap.jar" |  grep -v grep | wc -l` -gt 0
    do
	sleep 1
	echo -n '.'
	t=`expr $t + 1`
	if test $t -eq $TIMEOUT
	    then
	        echo ''
		    kill
		    fi
    done
    echo ''
    else
	echo "no process found"
    fi
}
 
restart() {
    t=0
    stop
    echo ''
    start
}

status() {
    if test `ps -edf | grep "$TOMCAT_DIR/bin/bootstrap.jar" | grep -v grep | wc -l` -gt 0
    then
	echo "$PROG_TOMCAT_NAME_DISPLAY is running"
	echo ''
	echo "---process details---"
	details=`ps -edf | grep "$TOMCAT_DIR/bin/bootstrap.jar" | grep -v grep`
	echo $details | awk '{ print "uid: " $1 }'
	echo $details | awk '{ print "pid: " $2 }'
	#echo $details | awk '{ print "PPID: " $3 }'
	echo $details | awk '{ print "cpu utilization: " $4 }'
	echo $details | awk '{ print "start time: " $5 }'
	#echo $details | awk '{ print "TTY: " $6 }'
	echo $details | awk '{ print "cumulative cpu time: " $7 }'
	echo -n 'command: '; echo $details | cut -d' ' -f8-
	echo ''
	echo 'Http Status :' $(curl -L -k --write-out %{http_code} --silent --output /dev/null https://${entName}.univ.fr)
    else
	echo "$PROG_TOMCAT_NAME_DISPLAY is stopped"
    fi
}

kill() {
    if test `ps -edf | grep "$TOMCAT_DIR/bin/bootstrap.jar" | grep -v grep | wc -l` -gt 0
    then
	echo "stop $PROG_TOMCAT_NAME_DISPLAY by kill !"
	ps -edf | grep "$TOMCAT_DIR/bin/bootstrap.jar" | grep -v grep | awk '{print "kill -9 " $2}' | sh
    else
	echo "no process found"
    fi
}
 
case $1 in
        start)
                start
		exit 0
        ;;
        stop)
                stop
		exit 0
        ;;
        restart)
                restart
		exit 0
        ;;
        status)
                status
		exit 0
        ;;
        kill)
                kill
		exit 0
		;;
        *)
 
        echo "Usage: $prog {start|stop|restart|status|kill}"
        exit 1
esac
