#! /bin/sh
#
# hobbit-client   This shell script takes care of starting and stopping
#                 the hobbit client.
#
# chkconfig: 2345 80 20
# description: hobbit 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 Hobbit server.

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

test -x $DAEMON || exit 0

CMD="$1"

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

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

set $HOBBITSERVERS
if [ $# -eq 1 ]; then
	echo "BBDISP=\"$HOBBITSERVERS\"" >/var/run/hobbitclient-runtime.cfg
	echo "BBDISPLAYS=\"\"" >>/var/run/hobbitclient-runtime.cfg
else
	echo "BBDISP=\"0.0.0.0\"" >/var/run/hobbitclient-runtime.cfg
	echo "BBDISPLAYS=\"$HOBBITSERVERS\"" >>/var/run/hobbitclient-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" - hobbit
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	su -c "$DAEMON stop" - hobbit
	echo "$NAME."
	;;
  restart)
	echo -n "Restarting $DESC: "
	su -c "$DAEMON stop" - hobbit
	su -c "$DAEMON $DMNOPTS start" - hobbit
	echo "$NAME."
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart}" >&2
	echo "Usage: $N {start|stop|restart}" >&2
	exit 1
	;;
esac

exit 0

