#!/bin/bash
#  /etc/init.d/eeclusterd
#
# chkconfig: 2345 20 80
#
#
# EECluster: An Energy-Efficient software tool for managing HPC Clusters
# (c) Universidad de Oviedo 2016
# 
# Autores: 
# 	Alberto Cocaña Fernández
# 	Raquel Cortina Parajón
# 	José Ranilla Pastor
# 	Luciano Sánchez Ramos
# 
#
# processname: eeclusterd
# config: /usr/share/eeclusterd/conf/configuration.properties
# config: /usr/share/eeclusterd/conf/factories.properties
# config: /usr/share/eeclusterd/conf/jdbc_config.properties
# config: /usr/share/eeclusterd/conf/jdbc_queries.properties
# config: /usr/share/eeclusterd/conf/path.properties
# pidfile: /var/run/eeclusterd/eeclusterd.pid
#
### BEGIN INIT INFO
# Provides:          eeclusterd
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Short-Description: Starts the EECluster service
# Description:       This file is used to start the daemon
#                    and should be placed in /etc/init.d
### END INIT INFO


NAME="eeclusterd"
DESC="EECluster service"

# The path to the folder containing eeclusterd
FILE_PATH="/usr/share/eeclusterd"

# The path to the folder containing the java runtime
JAVA_HOME="/usr/bin/java"


export PATH=$PATH:/sbin:/bin:/usr/bin

# Our classpath 
CLASS_PATH="$FILE_PATH"

# The fully qualified name of the class to execute
CLASS="main.Main"

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

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

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

green='\e[0;32m'
red='\e[0;31m'
NC='\e[0m' # No Color

case "$1" in
start)
	printf "%-50s" "Starting the $DESC..." 

	let count=0
	notSGE=true
	loop=true

	echo "" > $LOG_OUT
	echo "" > $LOG_ERR

	while $loop; do		
		#Try to execute a SGE command (as qhost for example)
		
		var_SGEROOT=$(env | grep SGE_ROOT)
		if [ ${#var_SGEROOT} -le 0 ]; then
			echo "SGE_ROOT is not defined in iter $count" >> $LOG_ERR
			export $(cat $FILE_PATH/conf/paths.properties | grep SGE_ROOT)
		fi

		qhost_path=$(cat $FILE_PATH/conf/paths.properties | grep QHOST)
		qhost_path=${qhost_path/QHOST = /}
		#echo "qhost:-$qhost_path-" >> $LOG_OUT

		$qhost_path > /dev/null 2> "$FILE_PATH/log/qhost.err"
		var_qhost=$(cat "$FILE_PATH/log/qhost.err" | grep "not found")
		rm -f "$FILE_PATH/log/qhost.err"

		if [ ${#var_qhost} -gt 0 ]; then
			#echo ""
			echo "SGE is not running on time $count. Sleep 5 seconds" >> $LOG_ERR
			sleep 5
			let "count++"
			if [ $count -ge 3 ];then
				loop=false
			fi
		else
			notSGE=false
			#echo ""
			echo "SGE is running on time $count" >> $LOG_OUT
			loop=false
		fi
	done

	#If SGE finally is not running, don't start eeclusterd
	if ($notSGE); then
		echo "SGE is not running" >> $LOG_OUT
	else
		PID=`$JAVA_HOME -cp $CLASS_PATH $CLASS >> $LOG_OUT  2>> $LOG_ERR & echo $!`
	fi
	
        if [ -z $PID ]; then
           echo -e "[${red}Fail${NC}]"
        else
            echo $PID > $PIDFILE
            echo -e "[${green}OK${NC}]"
        fi
;;
status)
        printf "%-50s" "Checking $DESC..."
        
	if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
		echo -e "[${red}Not running${NC}]"               
 		echo "Process dead but pidfile exists"
		 
            else
                echo -e "[${green}Running${NC}]"
            fi
        else
            echo -e "[${red}Service not running${NC}]" 
        fi
;;
stop)
        printf "%-50s" "Stopping $DESC..."
        
	PID=`cat $PIDFILE`

        if [ -f $PIDFILE ]; then
            kill -HUP $PID
            echo -e "[${green}OK${NC}]"
            rm -f $PIDFILE
        else
            echo -e "[${red}pidfile not found${NC}]" 
        fi
;;

restart)
  	$0 stop
  	$0 start
;;

*)
        echo "Usage: $0 {status|start|stop|restart}"
        exit 1
esac

