#! /bin/sh
# /etc/init.d/hello: start the hello world! daemon.

PATH=/bin:/usr/bin:/sbin:/usr/sbin

pidfile=/var/run/hello.pid
binpath=/usr/bin/hello

test -x $binpath || exit 0

# Options for the daemon
OPTIONS="-s"

running()
{
    # No pidfile, probably no daemon present
    #
    if [ ! -f $pidfile ]
    then
	return 1
    fi

    pid=`cat $pidfile`

    # No pid, probably no daemon present
    #
    if [ -z "$pid" ]
    then
	return 1
    fi

    if [ ! -d /proc/$pid ]
    then
	return 1
    fi

    cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1`

    # No daemon?
    #
    if [ "$cmd" != "$binpath" ]
    then
	return 1
    fi

    return 0
}

case "$1" in
  start)
    echo -n "Starting hello world daemon: hello"
    start-stop-daemon --background --make-pidfile --pidfile $pidfile --start --quiet --exec $binpath -- -s
    echo "."
    ;;
  stop)
    echo -n "Stopping hello world daemon: hello"
    start-stop-daemon --stop --quiet --exec $binpath --pidfile $pidfile
    echo "."
    ;;
  reload|force-reload)
    echo -n "Reloading hello world daemon: hello"
    start-stop-daemon --stop --quiet --signal 1 --exec $binpath --pidfile $pidfile
    echo "."
    ;;
  restart)
    echo -n "Restarting hello world daemon: hello"
    start-stop-daemon --stop --quiet --exec $binpath --pidfile $pidfile
    sleep 1
    start-stop-daemon --start --quiet --exec $binpath -- $OPTIONS
    echo "."
    ;;
  reload-or-restart)
    if running
    then
	echo -n "Reloading hello world daemon: hello"
	start-stop-daemon --stop --quiet --signal 1 --exec $binpath --pidfile $pidfile
    else
    	echo -n "Restarting hello world daemon: hello"
	start-stop-daemon --start --quiet --exec $binpath -- $SYSLOGD
    fi
    echo "."
    ;;
  *)
    echo "Usage: /etc/init.d/hello {start|stop|reload|restart|force-reload|reload-or-restart}"
    exit 1
esac

exit 0
