#!/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:          ctxlogd
# 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/ctxlogd
RUNDIR=/var/run/ctxlogd
PIDFILE=$RUNDIR/ctxlogd.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_ctxlogd()
{
	# Get the pid (or list of pids) of the ctxlogd daemon
	# The LSB function pidofproc is too inconsistent across distrobutions at present to be used safely.

	# Identify the ctxlogd daemon with 'ps' using the exact path
	
    	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 | grep "[0-9] $DAEMON" | sed -e "s/^[ ]*//g" -e "s, $DAEMON,,g"`"
	elif PID_CMD="/bin/ps" ; test -x "$PID_CMD" ; then
		CTXLOGD_PID="`"$PID_CMD" axo pid,cmd | grep "[0-9] $DAEMON" | sed -e "s/^[ ]*//g" -e "s, $DAEMON,,g"`"
	# pidof finds all ctxlogd 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
}


ctxlogd_start () {
		get_pidof_ctxlogd

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

			# Starting the ctxlogd daemon

			# Start the ctxlogd daemon using the LSB init functions
			if [ "$INITLIB" = "$LSB_INIT_FUNCTIONS" ]
			then
				if start_daemon -p $PIDFILE "$DAEMON" 
				then
					log_success_msg "Starting Citrix Log daemon [ OK ]"
				else
					log_failure_msg "Starting Citrix Log daemon [fail]"
				fi

			# Start the ctxlogd daemon manually
			else
				"$ECHO_CMD_NO_NL" " * Starting Citrix Log daemon"

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

					"$ECHO_CMD_NO_NL" " [ OK ]\n"
				else
					"$ECHO_CMD_NO_NL" " [fail]\n"
				fi
			fi
		else
			"$ECHO_CMD_NO_NL" "Citrix Log daemon is already running.\n"
		fi
}


ctxlogd_stop () {

	# Get the pid of the ctxlogd daemon
	if get_pidof_ctxlogd
	then
		# If a pid has been found
		if [ -n "$CTXLOGD_PID" ]
		then
			# If the LSB init functions are present
			if [ "$INITLIB" = "$LSB_INIT_FUNCTIONS" ]
			then
			
				# Killproc requires a pidfile.
				# Dynamicaly create the pidfile.
				"$ECHO_CMD_NO_NL" "$CTXLOGD_PID" > "$PIDFILE"

				killproc -p "$PIDFILE" "$DAEMON"

				RETVAL=$?		
				
				# Remove the pidfile if not removed by killproc
				rm -f "$PIDFILE" > /dev/null 2>&1

				if [ $RETVAL -eq 0 ]
				then
					log_success_msg "Stopping Citrix Log daemon [ OK ]"
				else
					log_failure_msg "Stopping Citrix Log daemon [fail]"
				fi

			# Stop the ctxlogd daemon manually
			else
				"$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
			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 ctxlogd daemon.\n"
	fi
}

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

    restart|reload|force-reload)
        ctxlogd_stop
        ctxlogd_start
        ;;

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

exit 0

