#! /bin/sh
### BEGIN INIT INFO
# Provides:          xymon-client
# Required-Start:    $network $syslog $named $local_fs $remote_fs
# Required-Stop:     $network $syslog $named $local_fs $remote_fs
# Should-Start:      
# Should-Stop:       
# Default-Start:        3 5
# Default-Stop:         0 1 2 6
# Short-Description:    xymon network monitoring
# Description:          xymon is a network monitoring tool that allows
#  you to monitor hosts and services. This client reports local
#  system statistics (cpu-, memory-, disk-utilisation etc)
#  to the Xymon server.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/lib/xymon/client/runclient.sh
NAME=xymon-client
DESC=xymon-client

test -x $DAEMON || exit 0

CMD="$1"

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active    checks whether a service is activated by symlinks
#      rc_splash arg    sets the boot splash screen to arg (if active)
. /etc/rc.status

# First reset status of this service
rc_reset

# Include xymon-client defaults if available
DMNOPTS=""
if [ -f /etc/default/xymon-client ] ; then
	. /etc/default/xymon-client
else
	echo "Installation failure - missing /etc/default/xymon-client"
	exit 1
fi

if [ "$XYMONSERVERS" = "" ]; then
	echo "Please configure XYMONSERVERS in /etc/default/xymon-client"
	exit 1
fi

set $XYMONSERVERS
if [ $# -eq 1 ]; then
	echo "XYMSRV=\"$XYMONSERVERS\"" >/var/run/xymonclient-runtime.cfg
else
	echo "XYMSRV=\"0.0.0.0\"" >/var/run/xymonclient-runtime.cfg
fi

if [ "$CLIENTHOSTNAME" != "" ]; then
	DMNOPTS="${DMNOPTS} --hostname=${CLIENTHOSTNAME}"
fi
if [ "$CLIENTOS" != "" ]; then
	DMNOPTS="${DMNOPTS} --os=${CLIENTOS}"
fi

set -e

case "$CMD" in
    start)
        echo -n "Starting $DESC: "
        su -c "$DAEMON $DMNOPTS start" - xymon
        echo "$NAME."
        ;;
    stop)
        echo -n "Stopping $DESC: "
        su -c "$DAEMON stop" - xymon
        echo "$NAME."
        ;;
    restart)
        echo -n "Restarting $DESC: "
        su -c "$DAEMON stop" - xymon
        su -c "$DAEMON $DMNOPTS start" - xymon
        echo "$NAME."
        ;;
    reload)
        echo -n "Restarting $DESC (no reload support)"
        su -c "$DAEMON stop" - xymon
        su -c "$DAEMON $DMNOPTS start" - xymon
        rc_status
        ;;
    status)
        echo -n "Checking for $DESC: "
        checkproc $DAEMON
        rc_status -v
        ;;
    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|reload|status}" >&2
        exit 1
        ;;
esac

exit 0