#! /bin/sh
#
# init script for UniK OLSR daemon
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/olsrd
NAME=olsrd
DESC=olsrd

test -x $DAEMON || exit 0

# Include unik-olsrd defaults if available
# you can make command line option width $DAEMON_OPTS variable
if [ -f /etc/default/olsrd ] ; then
	. /etc/default/olsrd
fi

set -e

check_start_olsrd_option() {
    if [ ! "$start_olsrd" = "yes" ]; then
	echo "Not starting olsrd daemon, disabled via /etc/default/olsrd"
	return 1
    else
	return 0
    fi
}

case "$1" in
  start)
  if check_start_olsrd_option; then
	echo -n "Starting $DESC: "
	start-stop-daemon --start --quiet --name $DESC \
		--exec $DAEMON -- $DAEMON_OPTS
	echo "$NAME."
  else
      RET=1
  fi
	;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop --quiet --oknodo --name $DESC \
		--exec $DAEMON
	echo "$NAME."
	;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: "
	#start-stop-daemon --stop --quiet --name $DESC \
	start-stop-daemon --stop --quiet --oknodo --name $DESC \
		--exec $DAEMON 
	sleep 1
	if check_start_olsrd_option; then
	  start-stop-daemon --start --quiet --name $DESC \
		--exec $DAEMON -- $DAEMON_OPTS > /dev/null
	  echo "$NAME."
	else
          RET=1
        fi
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
