#!/bin/sh
#
# chkconfig: 2345 20 80
# description: Atempo Live Navigator agent start up script
#
### BEGIN INIT INFO
# Provides:          hnagent
# Required-Start:    $syslog $network $local_fs
# Required-Stop:     $syslog $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Atempo Live Navigator agent start up script
# Description: Endpoint Data Protection for Remote Offices, Desktops, Laptops and File Servers
### END INIT INFO

# Name of the daemon
# MUST NOT be the same name as the script: the
# stop command would kill the script too
DAEMON_BASE=HNagent
INSTALL_PATH=/opt/Atempo/HN
DAEMON=$INSTALL_PATH/bin/$DAEMON_BASE
DAEMON_OPTIONS=""
DAEMON_TITLE="Atempo Live Navigator"
STOP_SLEEP_SECS=5
subsys=hnagent

DIR=$INSTALL_PATH/lib
UNAME=`uname -s`
case "$UNAME" in
AIX)    LIBPATH="$DIR:$LIBPATH"                     ; export LIBPATH ;;
Darwin) DYLD_LIBRARY_PATH="$DIR:$DYLD_LIBRARY_PATH" ; export DYLD_LIBRARY_PATH ;;
*)      LD_LIBRARY_PATH="$DIR:$LD_LIBRARY_PATH"     ; export LD_LIBRARY_PATH ;;
esac

# On FreeBSD 7, the following lines enable the startup script. If these lines are deleted, it won't work.
# PROVIDE: Atempo Live Navigator
# REQUIRE: NETWORKING

if echo "\c" | grep "c">/dev/null ; then
	ECHOMODE=Bsd
else
	ECHOMODE=Sys5
fi

echo_no_cr() {
	if [ "$ECHOMODE" = Bsd ] ; then
		echo -n "$*"
	else
		echo "$*\c"
	fi
}

ISREDHATLIKE=1
# Source function library.
if [ -f /etc/init.d/functions ] ; then
	. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
	. /etc/rc.d/init.d/functions
else
	ISREDHATLIKE=0
fi

ISSUSE=1
if [ -f /etc/rc.status ] ; then
	. /etc/rc.status
else
	ISSUSE=0
fi

get_locale_of_keyword()
{
	LOC=`locale | grep "${1}=" | cut -d= -f2 | sed 's/"//g'`
	if [ "$LOC" ]; then
		echo `expr substr ${LOC} 1 3`
	fi
}

set_locale()
{
	# LC_ALL - Overrides individual LC_* settings: if LC_ALL is set, none of the below LC_* have any effect.
	LOCALE3=$(get_locale_of_keyword "LC_ALL")
	if [ "fr_" = "${LOCALE3}" ]; then
		locale=fr
		return
	fi

	# LC_IDENTIFICATION - Metadata about the locale information.
	LOCALE3=$(get_locale_of_keyword "LC_IDENTIFICATION")
	if [ "fr_" = "${LOCALE3}" ]; then
		locale=fr
		return
	fi

	# LC_MESSAGES - What language should be used for system messages.
	LOCALE3=$(get_locale_of_keyword "LC_MESSAGES")
	if [ "fr_" = "${LOCALE3}" ]; then
		locale=fr
		return
	fi
}

start_agent()
{
	check_agent_running >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		return
	fi

	if [ "$locale" = "fr" ]; then
		echo_no_cr "Démarrage de $DAEMON_TITLE"
	else
		echo_no_cr "Starting $DAEMON_TITLE"
	fi

	if [ $ISSUSE -eq 1 ] ; then
		startproc -f "$DAEMON" $DAEMON_OPTIONS
	else
		"$DAEMON" $DAEMON_OPTIONS &
	fi

	if [ $ISREDHATLIKE -eq 1 ]; then
		echo_success
		echo
	elif [ $ISSUSE -eq 1 ]; then
		rc_status -v
	else
		echo
	fi
}

stop_agent()
{
	check_agent_running >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		return
	fi

	if [ "$locale" = "fr" ]; then
		echo_no_cr "Arrêt de $DAEMON_TITLE"
	else
		echo_no_cr "Stopping $DAEMON_TITLE"
	fi

	AGENT_LEFT_MEMORY=0
	AGENT_PIDS=`pidof $DAEMON_BASE`
	echo_no_cr "."
	kill $AGENT_PIDS
	echo_no_cr "."
	sleep $STOP_SLEEP_SECS
	echo_no_cr "."

	check_agent_running >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		AGENT_LEFT_MEMORY=1
	fi

	if [ $AGENT_LEFT_MEMORY -ne 1 ] ; then
		# 181 tries = 15 minutes
		nb_of_retries_left=181
		while [ $nb_of_retries_left != 0 ] ; do
			nb_of_retries_left=`expr $nb_of_retries_left - 1`
			check_agent_running >/dev/null 2>&1
			if [ $? -ne 0 ]; then
				echo_no_cr "."
				sleep $STOP_SLEEP_SECS
				continue
			else
				AGENT_LEFT_MEMORY=1
				break
			fi
			echo_no_cr "."
		done
	fi

	if [ $AGENT_LEFT_MEMORY -ne 1 ] ; then
		if [ "$locale" = "fr" ]; then
			echo "$DAEMON_TITLE ne s’est pas arrêté à temps, passage en procédure d’urgence..."
		else
			echo "$DAEMON_TITLE did not stop in time, switching to emergency procedure..."
		fi

		kill -9 $AGENT_PIDS
	
		check_agent_running >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			if [ "$locale" = "fr" ]; then
				echo "La procédure d’urgence à échoué, $DAEMON_TITLE est toujours en cours d’exécution"
			else
				echo "Emergency procedure failed, $DAEMON_TITLE is still alive"
			fi
		else
			if [ "$locale" = "fr" ]; then
				echo "$DAEMON_TITLE: La procédure d’urgence est terminée"
			else
				echo "$DAEMON_TITLE: Emergency procedure complete"
			fi
			AGENT_LEFT_MEMORY=1
		fi
	fi

	if [ $AGENT_LEFT_MEMORY -ne 1 ] ; then
		if [ $ISREDHATLIKE -eq 1 ]; then
			echo_failure
			echo
		elif [ $ISSUSE -eq 1 ]; then
			rc_failed 1
		else
			echo
		fi
	else
		if [ $ISREDHATLIKE -eq 1 ]; then
			echo_success
			echo
		elif [ $ISSUSE -eq 1 ]; then
			rc_status -v
		else
			echo
		fi
	fi
}

check_agent_running()
{
	PIDS=`pidof $DAEMON_BASE`
	if [ -z "$PIDS" ]; then
		if [ "$locale" = "fr" ]; then
			echo_no_cr "$DAEMON_BASE est arrêté"
		else
			echo_no_cr "$DAEMON_BASE is stopped"
		fi
		echo
		return 0
	fi

	if [ "$locale" = "fr" ]; then
		echo_no_cr "$DAEMON_BASE (pid $PIDS) est en cours d’exécution..."
	else
		echo_no_cr "$DAEMON_BASE (pid $PIDS) is running..."
	fi
	echo
	return 1
}

locale=C # POSIX is default value
set_locale

case "$1" in
  start)
	start_agent
        [ -d /var/lock/subsys ] || mkdir -p /var/lock/subsys
        touch /var/lock/subsys/"$subsys"
	;;
  stop)
	stop_agent
        rm -f /var/lock/subsys/"$subsys"
	;;
  restart)
	stop_agent
	start_agent
	;;
  status)
	check_agent_running
	;;
  *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 2
	;;
esac

