#!/bin/sh
# Copyright 2020 Citrix Systems, Inc.
#
# This script starts and stops Citrix Logging Service

# The settings for chkconfig:
# chkconfig is used on Redhat and Fedora instead of update-rc.d to update init levels
# the default values of 20 and 80 are being used for start and stop priority.
# chkconfig <run levels> <start priority> <stop priority>
# chkconfig: 2345 20 80
# description: Provides Logging service required for the Workspace App for Linux.

### LSB init script information required by debian
### BEGIN INIT INFO
# Provides:          ctxcwalogd
# Required-Start:
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Citrix Logging Service
### END INIT INFO

# Set a sane path
export PATH=/bin:/usr/bin:/usr/sbin:/sbin

# The DAEMON variable gets updated at install time if it is not installed in the default location
DAEMON=/opt/Citrix/ICAClient/util/ctxcwalogd
CitrixUser=citrixlog
RUNDIR=/var/run/ctxcwalogd
PIDFILE=$RUNDIR/ctxcwalogd.pid

ECHO_CMD_NO_NL=printf

INITLIB="NOT_FOUND"

# Init functions locations for different systems
LSB_INIT_FUNCTIONS=/lib/lsb/init-functions

# If /lib/lsb/init-functions is present use the LSB init fuctions.
if [ -r "$LSB_INIT_FUNCTIONS" ]; then
	. "$LSB_INIT_FUNCTIONS"
	INITLIB="$LSB_INIT_FUNCTIONS"
fi


CTXLOGD_PID=""

get_pidof_ctxcwalogd()
{
	# Get the pid (or list of pids) of the ctxcwalogd daemon
	# The LSB function pidofproc is too inconsistent across distrobutions at present to be used safely.

	# Identify the ctxcwalogd daemon with 'ps' using the citrixuser(citrixlog)
	local processName=`basename $DAEMON`
    if ls -l `which ps 2>/dev/null` | grep busybox 1>/dev/null ; then                                                      
		CTXLOGD_PID=`ps  | grep " $DAEMON" | grep -v grep | cut -d ' ' -f 2`
    elif PID_CMD="`which ps 2>/dev/null`" ; test -x "$PID_CMD" ; then
		CTXLOGD_PID="`"$PID_CMD" axo pid,cmd,user | grep "[0-9] $processName[ ]*$CitrixUser" | sed -e "s/^[ ]*//g" -e "s, $processName,,g" -e "s, $CitrixUser,,g"`"
	elif PID_CMD="/bin/ps" ; test -x "$PID_CMD" ; then
		CTXLOGD_PID="`"$PID_CMD" axo pid,cmd,user | grep "[0-9] $processName[ ]*$CitrixUser" | sed -e "s/^[ ]*//g" -e "s, $processName,,g" -e "s, $CitrixUser,,g"`"
	# pidof finds all ctxcwalogd binaries running ignoring the path, note not scripts by default.
	elif PID_CMD="`which pidof 2>/dev/null`" ; test -x "$PID_CMD" ; then
		CTXLOGD_PID="`"$PID_CMD" "$DAEMON"`"
	elif PID_CMD="/sbin/pidof" ; test -x "$PID_CMD" ; then
		CTXLOGD_PID="`"$PID_CMD" "$DAEMON"`"
	else
		return 1
	fi

	return 0
}


ctxcwalogd_start () {
		get_pidof_ctxcwalogd

		# Check if any instances of the ctxcwalogd daemon were found
		if [ -z "$CTXLOGD_PID" ]
		then
			# Create the $RUNDIR directory for ctxcwalogd
			mkdir -m 700 -p "$RUNDIR"

			# Starting the ctxcwalogd daemon

			"$ECHO_CMD_NO_NL" " * Starting Citrix Log daemon"

			if start-stop-daemon --start --chuid $CitrixUser --background --make-pidfile --pidfile $PIDFILE --exec $DAEMON
			then
				"$ECHO_CMD_NO_NL" " [ OK ]\n"
			elif /sbin/runuser $CitrixUser -s "$DAEMON";then
				"$ECHO_CMD_NO_NL" " [ OK ]\n"
			else
				"$ECHO_CMD_NO_NL" " [ fail ]\n"
				exit
			fi

			# Create the lock required by Redhat based distros if possible
			mkdir -p /var/lock/subsys
			touch /var/lock/subsys/`basename "$DAEMON"`

		else
			"$ECHO_CMD_NO_NL" "Citrix Log daemon is already running.\n"
		fi
}


ctxcwalogd_stop () {

	# Get the pid of the ctxcwalogd daemon
	if get_pidof_ctxcwalogd
	then
		# If a pid has been found
		if [ -n "$CTXLOGD_PID" ]
		then

			# Stop the ctxcwalogd daemon manually
			"$ECHO_CMD_NO_NL" " * Stopping Citrix Log daemon"

			# kill the daemon process using the pid value
			if kill "$CTXLOGD_PID" > /dev/null 2>&1
			then
				# Remove the lock required by Redhat based distros
				rm -f /var/lock/subsys/`basename "$DAEMON"`

				"$ECHO_CMD_NO_NL" " [ OK ]\n"
			else
				"$ECHO_CMD_NO_NL" " [fail]\n"
			fi
		else
			"$ECHO_CMD_NO_NL" "Citrix Log daemon has already stopped.\n"
		fi
	else
		"$ECHO_CMD_NO_NL" " * Stopping Citrix Log daemon [fail]\nCould not find the pid of the ctxcwalogd daemon.\n"
	fi
}

# parse arguments
case "$1" in
    start|stop)
        ctxcwalogd_${1}
        ;;

    restart|reload|force-reload)
        ctxcwalogd_stop
        ctxcwalogd_start
        ;;

    *)
        echo "usage: `basename "$0"` { start | stop | restart | reload | force-reload }" >&2
        exit 1
        ;;
esac

exit 0

