#! /bin/sh
#
# xymon-client    This shell script takes care of starting and stopping
#                 the xymon client.

### BEGIN INIT INFO
# Provides:          xymon-client
# Required-Start:    $remote_fs $network
# Should-Start:      $all
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Xymon system monitor client
# Description:       Client to feed system data to a remote Xymon server.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="/usr/lib/xymon/client/bin/xymonlaunch"
NAME=xymon-client
DESC="Xymon Client"
PIDFILE="/var/run/xymon/clientlaunch.pid"
XYMONCLIENTHOME="/usr/lib/xymon/client"

test -x $DAEMON || exit 0

. /lib/lsb/init-functions
. /usr/share/xymon/init-common.sh

# Include xymon-client defaults if available
if [ -f /etc/default/xymon-client ] ; then
	. /etc/default/xymon-client
fi
[ -z "$MACHINE" ] && MACHINE="$CLIENTHOSTNAME"
[ -z "$MACHINEDOTS" ] && MACHINEDOTS="`hostname -f`"
export XYMONSERVERS XYMONCLIENTHOME CLIENTHOSTNAME MACHINE MACHINEDOTS

case "$1" in
  start)
	# do not run the client script on the server
	[ -x /usr/lib/xymon/server/bin/xymond ] && exit 0

	create_includefiles

	if test "$TMPFSSIZE" && test -e /proc/mounts && ! grep -q /var/lib/xymon/tmp /proc/mounts; then
		echo "Mounting tmpfs on /var/lib/xymon/tmp"
		rm -f /var/lib/xymon/tmp/*
		mount -t tmpfs -o"size=$TMPFSSIZE,mode=755,uid=$(id -u xymon)" tmpfs /var/lib/xymon/tmp
	fi

	log_daemon_msg "Starting $DESC" "$NAME"
       start-stop-daemon --pidfile "$PIDFILE" --exec $DAEMON --chuid xymon --umask 022 --start \
		-- \
		--config=/etc/xymon/clientlaunch.cfg \
		--log=/var/log/xymon/clientlaunch.log \
		--pidfile=$PIDFILE
	log_end_msg $?
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
       start-stop-daemon --pidfile "$PIDFILE" --exec $DAEMON --pidfile $PIDFILE --stop --retry 5
	log_end_msg $?
	;;
  status)
	if test -s $PIDFILE
	then
		kill -0 `cat $PIDFILE`
		if test $? -eq 0
		then
                        echo "Xymon client running with PID `cat $PIDFILE`"
			exit 0
		else
			echo "Xymon client not running, removing stale PID file"
			rm -f $PIDFILE
			exit 1
		fi
	else
		echo "Xymon client does not appear to be running"
		exit 3
	fi
	;;
  restart)
	if [ -x /usr/lib/xymon/server/bin/xymond ] ; then
		log_action_msg "Xymon server installed. Please restart 'xymon' instead"
		exit 0
	fi
	$0 stop
	sleep 1
	$0 start
	;;
  reload|force-reload)
	[ -x /usr/lib/xymon/server/bin/xymond ] && exit 0
	create_includefiles
	kill -HUP `cat /var/run/xymon/clientlaunch.pid`
	;;
  rotate)
	for PIDFILE in /var/run/xymon/*.pid
	do
		test -e $PIDFILE && kill -HUP `cat $PIDFILE`
	done
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|status|rotate}" >&2
	exit 1
	;;
esac

exit 0

