#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          bicsuite-client
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      $network $named $time
# Should-Stop:       $network $named $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop the bicsuite Enterprise Job Scheduling agents
# Description:       Controls the bicsuite agent processes
### END INIT INFO
#
. /opt/bicsuite/etc/SETTINGS
set -e
set -u

if [ -f /lib/lsb/init-functions ]; then
	. /lib/lsb/init-functions
	LOG_WARNING_MSG=log_warning_msg
	LOG_FAILURE_MSG=log_failure_msg
	ECHO_SUCCESS=echo
	ECHO_FAILURE=echo
else
	LOG_WARNING_MSG=echo
	LOG_FAILURE_MSG=echo
	ECHO_SUCCESS=echo
	ECHO_FAILURE=echo
fi

#
# There were some issues with the length of full qualified hostnames (too long)
# This is why we now use the abbreviated hostname instead
# In order to preserve the compatibility with previously created jobservers
# we check both variants here
HOSTNAME=`hostname -s | tr [a-z.-] [A-Z__]`
ALTHOSTNAME=`hostname | tr [a-z.-] [A-Z__]`

SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)

# Safeguard (relative paths, core dumps..)
cd /
umask 077

case "${1:-''}" in
  'start')
	echo -n "Starting bicsuite Enterprise Job Scheduling jobservers"
	if [ -d /opt/bicsuite/taskfiles/$HOSTNAME ]; then
		cd /opt/bicsuite/taskfiles/$HOSTNAME
		for USR in *; do
			if grep $USR /etc/passwd >/dev/null 2>&1; then
				cd $USR;
				for SVR in *; do
					su - $USR -c "
						[ -f \$HOME/.bashrc ] && . \$HOME/.bashrc;
						if [ -f \$HOME/etc/$SVR.init ]; then 
							. \$HOME/etc/$SVR.init;
						fi
						$BICSUITEHOME/bin/jobserver-run \$HOME/etc/$SVR.conf \$HOME/log/$SVR.out;
					"
				done
				cd ..;
			fi
		done
	fi
	if [ -d /opt/bicsuite/taskfiles/$ALTHOSTNAME ]; then
		cd /opt/bicsuite/taskfiles/$ALTHOSTNAME
		for USR in *; do
			if grep $USR /etc/passwd >/dev/null 2>&1; then
				cd $USR;
				for SVR in *; do
					su - $USR -c "
						[ -f \$HOME/.bashrc ] && . \$HOME/.bashrc;
						if [ -f \$HOME/etc/$SVR.init ]; then 
							. \$HOME/etc/$SVR.init;
						fi
						$BICSUITEHOME/bin/jobserver-run \$HOME/etc/$SVR.conf \$HOME/log/$SVR.out;
					"
				done
				cd ..;
			fi
		done
	fi
	$ECHO_SUCCESS
	;;

  'stop')
	echo -n "Stopping bicsuite Enterprise Job Scheduling jobservers"
	if [ -d /opt/bicsuite/taskfiles/$HOSTNAME ]; then
		cd /opt/bicsuite/taskfiles/$HOSTNAME
		for USR in *; do
			if grep $USR /etc/passwd >/dev/null 2>&1; then
				cd $USR;
				for SVR in *; do
					PNAME="~$USR/log/$SVR.out"
					PNAME=`eval "ls $PNAME"`
					if [ -n "$PNAME" -a -p "$PNAME" ]; then
						if fuser -s $PNAME >/dev/null 2>&1; then
							fuser -k $PNAME >/dev/null 2>&1;
						fi
					fi
				done
				cd ..;
			fi
		done
	fi
	if [ -d /opt/bicsuite/taskfiles/$ALTHOSTNAME ]; then
		cd /opt/bicsuite/taskfiles/$ALTHOSTNAME
		for USR in *; do
			if grep $USR /etc/passwd >/dev/null 2>&1; then
				cd $USR;
				for SVR in *; do
					PNAME="~$USR/log/$SVR.out"
					PNAME=`eval "ls $PNAME"`
					if [ -n "$PNAME" -a -p "$PNAME" ]; then
						if fuser -s $PNAME >/dev/null 2>&1; then
							fuser -k $PNAME >/dev/null 2>&1;
						fi
					fi
				done
				cd ..;
			fi
		done
	fi
	$ECHO_SUCCESS
	;;

  'restart')
	echo "Restarting bicsuite Enterprise Job Scheduling jobservers"
	/etc/init.d/bicsuite-client stop
	/etc/init.d/bicsuite-client start
	;;

  'reload'|'force-reload')
  	$LOG_WARNING_MSG "Reloading jobservers not supported; use 'restart' instead"
	log_end_msg 0
	;;

  'status')
	NOTFOUND=0
	if [ -d /opt/bicsuite/taskfiles/$HOSTNAME ]; then
		cd /opt/bicsuite/taskfiles/$HOSTNAME
		for USR in *; do
			if grep $USR /etc/passwd >/dev/null 2>&1; then
				cd $USR;
				for SVR in *; do
					PNAME="~$USR/log/$SVR.out"
					PNAME=`eval "ls $PNAME"`
					if fuser -s $PNAME >/dev/null 2>&1; then
						echo "GLOBAL.$HOSTNAME.$USR.$SVR is up"
					else
						echo "GLOBAL.$HOSTNAME.$USR.$SVR is down"
					fi
				done
				cd ..;
			fi
		done
	else
		NOTFOUND=`expr $NOTFOUND + 1`
	fi
	if [ -d /opt/bicsuite/taskfiles/$ALTHOSTNAME ]; then
		cd /opt/bicsuite/taskfiles/$ALTHOSTNAME
		for USR in *; do
			if grep $USR /etc/passwd >/dev/null 2>&1; then
				cd $USR;
				for SVR in *; do
					PNAME="~$USR/log/$SVR.out"
					PNAME=`eval "ls $PNAME"`
					if fuser -s $PNAME >/dev/null 2>&1; then
						echo "GLOBAL.$ALTHOSTNAME.$USR.$SVR is up"
					else
						echo "GLOBAL.$ALTHOSTNAME.$USR.$SVR is down"
					fi
				done
				cd ..;
			fi
		done
	else
		NOTFOUND=`expr $NOTFOUND + 1`
	fi
	if [ $NOTFOUND -ge 2 ]; then
		echo "No installed jobservers found"
	fi
  	;;

  *)
	echo "Usage: $SELF start|stop|restart|reload|force-reload|status"
	exit 1
	;;
esac
