#!/bin/sh
#
# BESWebReportsServer:    Starts the BigFix WebReports Server
#
# chkconfig: 2345 99 99
# description: Starts and stops the BigFix WebReports daemon
# processname: BESWebReportsServer

export SYSTEMCTL_SKIP_REDIRECT=1

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

prog=BESWebReportsServer
service=beswebreports

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

start() {
	SKIP_CLIENT_RESTART="${SKIP_CLIENT_RESTART:-noskip$1}"
	
	/bin/echo -n $"Starting BigFix: $prog: "

	hasSystemd=0
	if [ -e /etc/systemd ] && [ -f /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
			fi
		fi
	fi
	if [ "$hasSystemd" -eq 0 ] && [ -f /etc/locale.conf ]; then
		. /etc/locale.conf
		export LANG LC_ALL LC_CTYPE LC_MESSAGES
	fi

	clientActionMastheadPath=/etc/opt/BESClient/actionsite.afxm
	webreportsActionMastheadPath=/etc/opt/BESWebReportsServer/actionsite.afxm
	if [ -f $clientActionMastheadPath ]; then
		cp -f $clientActionMastheadPath $webreportsActionMastheadPath
	fi
	export BESClientActionMastheadPath=$webreportsActionMastheadPath
	export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/BESWebReportsServer/bin:/var/opt/BESWebReportsServer/BESReportsData/BESExe
	export BES_LIBPATH=/opt/BESWebReportsServer/bin
	DB2INSTANCE=db2inst1
	export DB2INSTANCE
	export DISPLAY=:0

	CLIENT_RESTART_NEEDED=0
	reportsConfigPath=/var/opt/BESWebReportsServer/beswebreports.config
	reportsConfigObfPath=/var/opt/BESWebReportsServer/beswebreports.obf
	if [ ! -f $reportsConfigPath ]; then
		if [ ! -f $reportsConfigPath.default ]; then
			failure "Missing config file $reportsConfigPath."
			/bin/echo
			return 1
		fi
		cat $reportsConfigPath.default > $reportsConfigPath
		CLIENT_RESTART_NEEDED=1
	fi

	stackSizeHardLimitKB=$(ulimit -Hs)
	if [ "$stackSizeHardLimitKB" != "unlimited" ] && [ $stackSizeHardLimitKB -lt 10240 ]; then
		failure "Stack size hard limit ( ulimit -Hs ) too low = $stackSizeHardLimitKB."
		/bin/echo
		return 1
	fi

	stackSizeSoftLimitKB=$(ulimit -Ss)
	if [ "$stackSizeSoftLimitKB" = "unlimited" ] || [ $stackSizeSoftLimitKB -lt 10240 ]; then
		ulimit -Ss 10240
	fi

	mkdir -p /var/log/BESWebReports
	if [ -f /var/opt/BESWebReportsServer/.user ]; then
		blkidcommand=/sbin/blkid
		if [ -f $blkidcommand ] ; then
			$blkidcommand >& /dev/null
		fi
		user=$(cat /var/opt/BESWebReportsServer/.user)
		xhost +local:$user &> /dev/null
		chown $user:$user $webreportsActionMastheadPath &> /dev/null
		blkidOut=$(su - $user -c $blkidcommand)
		initUUID=""
		if [ "$blkidOut" = "" ]; then
			initUUID="--initUUID"
		fi
		/opt/BESWebReportsServer/bin/BESWebReportsServer --initializeHost $initUUID
		chown $user:$user $reportsConfigPath &> /dev/null
		if [ "$initUUID" != "" ]; then
			chown $user:$user /var/opt/BESWebReportsServer/.uuid &> /dev/null
		fi
		if [ -f $reportsConfigObfPath ]; then
			chown $user:$user $reportsConfigObfPath &> /dev/null
		fi

		chown $user:$user /var/log/BESWebReports &> /dev/null

		##### Add ACL to '/var/opt/BESCommon' folder and files for user #####
		setfacl -m "u:$user:rx" /var/opt/BESCommon

		setfacl -m "u:$user:r" \
		/var/opt/BESCommon/BESComm.id \
		/var/opt/BESCommon/BESDeploy.id \
		/var/opt/BESCommon/BESLib.id \
		/var/opt/BESCommon/BESShared.id

		setfacl -m "u:$user:rw" \
		/var/opt/BESCommon/BESHostname.id \
		/var/opt/BESCommon/.HostIDAccess.read \
		/var/opt/BESCommon/.HostIDAccess.write \
		/var/opt/BESCommon/.IDAccess.read \
		/var/opt/BESCommon/.IDAccess.write

		daemon --user=$user /opt/BESWebReportsServer/bin/$prog 2> /dev/null
	else
		daemon /opt/BESWebReportsServer/bin/$prog 2> /dev/null
	fi

	ret=$?
	[ $ret -eq 0 ] && touch /var/lock/subsys/$service
	/bin/echo

	if [[ $CLIENT_RESTART_NEEDED -eq 1 && $SKIP_CLIENT_RESTART == "noskip" ]]; then
		CLIENT_INIT=/etc/init.d/besclient
		if [ -f $CLIENT_INIT ]; then
			( export BES_LIBPATH=/opt/BESClient/bin; $CLIENT_INIT restart )
		fi
	fi
	
	return $ret
}

stop() {
	/bin/echo -n $"Stopping BigFix: $prog: "
	# Find pid.
	pid=`pidof /opt/BESWebReportsServer/bin/$prog`
	if [ -z "$pid" ]; 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 $"BESWebReportsServer shutdown" || success $"BESWebReportsServer shutdown"
		RC=$((! $RC))
	[ $ret -eq 0 ] && rm -f /var/lock/subsys/$service
	/bin/echo
	return $ret
}

restart() {
	stop
	start
}

reload() {
	restart
}

status_WebReports() {
	status /opt/BESWebReportsServer/bin/$prog
}

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

exit $?
