#!/bin/sh
#
# besclient:    Starts the BigFix Client
#
# chkconfig: 2345 99 99
# description: Starts and stops the BigFix Client daemon
# processname: BESClient

export SYSTEMCTL_SKIP_REDIRECT=1

# Source function library.
. /etc/init.d/functions

prog=BESClient
service=besclient

test -x /opt/BESClient/bin/$prog || exit 0

start() {
	/bin/echo -n $"Starting BigFix: $prog: "
	# For systemd services the system locale is set by systemd.
	# For legacy SysV services, we get it from /etc/locale.conf
	hasSystemd=0
	if [ -e /etc/systemd  ] && [ -e /etc/os-release ]; then
		. /etc/os-release
		if [ -n "$VERSION_ID" ]; then
			ver=`echo $VERSION_ID | sed 's/\..*$//' | sed 's/,.*$//'`
			if [ -n "$ver" ] && [ -n "$ID" ]; then
				# rhel >= 7 with systemd. Containers don't support systemd
				[ "$ID" = "rhel" ] && [ "$ver" -ge 7 ] && systemdPid=`pidof systemd` && [ -n "$systemdPid" ] && hasSystemd=1
				# centos/ol >= 7 with systemd
				[ "$ID" = "centos" ] || [ "$ID" = "ol" ] && [ "$ver" -ge 7 ] && hasSystemd=1
				# amazon linux >= 2 with systemd
				[ "$ID" = "amzn" ] && [ "$ver" -ge 2 ] && hasSystemd=1
				# rocky linux supports systemd
				[ "$ID" = "rocky" ] && hasSystemd=1
				# fedora >= 34
				[ "$ID" = "fedora" ] && [ "$ver" -ge 34 ] && hasSystemd=1
			fi
		fi
	fi
	if [ "$hasSystemd" -eq 0 ]
	then
		[ -f  /etc/locale.conf ] && . /etc/locale.conf && export LANG LC_ALL LC_CTYPE LC_MESSAGES
	fi
	export XAUTHORITY=/root/.Xauthority
	export DISPLAY=:0
	export BESClientActionMastheadPath=/etc/opt/BESClient/actionsite.afxm
	if [ ! -f $BESClientActionMastheadPath ]; then
		failure "Missing masthead $BESClientActionMastheadPath."
		/bin/echo
		return 1
	fi
	# export BES_LIBPATH=/opt/BESClient/bin
	config=/var/opt/BESClient/besclient.config
	if [ ! -f $config ]; then
		if [ ! -f $config.default ]; then
			failure "Missing config file $config."
			/bin/echo
			return 1
		fi
	fi
	# limit access to client created files
	umask 077
	daemon /opt/BESClient/bin/$prog
	ret=$?
	[ $ret -eq 0 ] && /bin/touch /var/lock/subsys/$service
	/bin/echo
	return $ret
}

stop() {
	/bin/echo -n $"Stopping BigFix: $prog: "
	# Find pid.
	pid=`pgrep -o -f /opt/BESClient/bin/$prog`
	if [ -n "$pid" ]; then
		args=`cat /proc/$pid/cmdline`
	fi
	if [ -z "$pid" ] || [ -z "$args" ] || [ "$args" != "/opt/BESClient/bin/$prog" ]; then
		ret=1
		RC=0
	else    
		delay=120 
		/bin/kill -TERM $pid >/dev/null 2<&1
		ret=$? 
		sleep 0.2
		count=0
		while checkpid $pid && [ "$count" -lt "$delay" ];
		do sleep 5; count=$(($count+5));
		done 
		if [ "$count" -ge "$delay" ]; then 
			kill -KILL $pid >/dev/null 2<&1
			ret=$?
		fi
		sleep 0.2
		checkpid $pid
		RC=$?
	fi
	[ "$RC" -eq 0 ] && failure $"BESClient shutdown" || success $"BESClient shutdown"
		RC=$((! $RC))
	[ $ret -eq 0 ] && rm -f /var/lock/subsys/$service
	/bin/echo
	return $ret
}

restart() {
	stop
	start
}

reload() {
	restart
}

status_BESClient() {
	status /opt/BESClient/bin/$prog
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	condrestart)
		if [ -f /var/lock/subsys/$service ]; then
			restart
		fi
		;;
	reload|restart)
		restart
		;;
	status)
		status_BESClient
		;;
	*)
		/bin/echo $"Usage: besclient {start|stop|status|restart|reload|condrestart}"
		exit 1
esac

exit $?
