#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          isatapd
# Required-Start:    $network $remote_fs
# Required-Stop:     $remote_fs
# Should-Start:      $named
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: isatapd creates and maintains an ISATAP client tunnel (RFC 5214) in Linux
# Description:       isatapd creates and maintains an ISATAP client tunnel (RFC 5214) in Linux.
### END INIT INFO

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

DAEMON=/usr/sbin/isatapd
DESC="ISATAP client"

PIDFILE=/var/run/isatapd.pid

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

# Default options, these can be overriden by the information
# at /etc/default/isatapd
MTU=1280		# IPv6 MTU
DAEMON_OPTS=""          # Additional options given to the server
ISATAP_ROUTERS="isatap"

# Include defaults if available
if [ -f /etc/default/isatapd ] ; then
    . /etc/default/isatapd
fi

start_server() {
        start-stop-daemon --start --quiet --pidfile $PIDFILE \
                --exec $DAEMON -- --daemon --pid $PIDFILE \
                --mtu $MTU $DAEMON_OPTS $ISATAP_ROUTERS || return $?
}

stop_server() {
        start-stop-daemon --stop --quiet --pidfile $PIDFILE --retry 2 --oknodo || return $?
}

case "$1" in
  start)
        log_daemon_msg "Starting $DESC" "isatapd"
        if start_server ; then
            log_end_msg 0
        else
            log_end_msg $?
        fi
        ;;
  stop)
        log_daemon_msg "Stopping $DESC" "isatapd"
        if stop_server ; then
            log_end_msg 0
        else
            log_end_msg $?
        fi
        ;;
  restart|force-reload)
        log_daemon_msg "Restarting $DESC" "isatapd"
        stop_server
        if start_server ; then
            log_end_msg 0
        else
            log_end_msg $?
        fi
        ;;
  status)
        status_of_proc -p $PIDFILE $DAEMON isatapd && exit 0 || exit $?
        ;;
  reload)
        log_daemon_msg "Reloading $DESC" "isatapd"
        if start-stop-daemon --stop --signal 1 --pidfile $PIDFILE > /dev/null; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
  *)
        N=/etc/init.d/isatapd
        echo "Usage: $N {start|stop|restart|force-reload|reload|status}" >&2
        exit 1
        ;;
esac

exit 0
