#!/bin/sh
#
# Start/stop the dwun daemon
#

# Source function library.
. /etc/rc.d/init.d/functions

RESTART_TIMEOUT_SECS=10
PIDFILE=/var/run/dwun.pid

case "$1" in
  start)
    echo -n "Starting dwun: "
    daemon dwun
    echo
    touch /var/lock/subsys/dwun
    ;;
  stop)
    echo -n "Stopping dwun: "
    killproc -HUP dwun
    echo
    rm -f /var/lock/subsys/dwun
    ;;
  restart)
    # dwun waits for commandon (e.g. pppd) to die before exiting. May take
    # more than 1 second.
    $0 stop
    c=0;
    while [ -e "$PIDFILE" ]; do
      sleep 1
        if [ "$c" -eq "$RESTART_TIMEOUT_SECS" ]; then
	  echo "Restarting dwun failed"
          echo Remove \'"$PIDFILE"\' if dwun is no longer running
        exit 1
      fi
    done
    $0 start
    ;;
  status)
    status dwun
    ;;
  *)
    echo "Usage: dwun {start|stop|restart|status}"
    exit 1
    ;;
esac
exit 0
