#!/bin/sh 
#
### BEGIN INIT INFO
# Provides:          xorp
# Required-Start:    $network $local_fs
# Required-Stop:     
# Should-Start:      $named
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: eXtensible Open Router Platform
# Description:       XORP is the eXtensible Open Router Platform. It
#                    implements a number of network protocols such as BGP,
#                    OSPF, RIP/RIPng, IGMP/MLD and PIM-SM.  
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/xorp_rtrmgr
DAEMON_WRAPPER=/usr/bin/daemon

NAME=xorp
PIDFILE=/var/run/$NAME.pid 
DESC="eXtensible Open Router Platform"

test -x $DAEMON || exit 0
test -x $DAEMON_WRAPPER || exit 0

. /lib/lsb/init-functions

RUN="no"
DAEMON_OPTS=""
# time to wait for daemons death, in seconds
# don't set it too low or you might not let xorp die gracefully
DIETIME=10
LOGFILE=/var/log/xorp/router.log
LOGFILE_ERR=/var/log/xorp/router.err.log
# Include xorp defaults if available
if [ -f /etc/default/xorp ] ; then
	. /etc/default/xorp
fi

if [ "x$RUN" != "xyes" ] ; then
    log_failure_msg "XORP disabled, please adjust the configuration at /etc/xorp/config.boot"
    log_failure_msg "to your needs and then set RUN to 'yes' in /etc/default/xorp to "
    log_failure_msg "enable the router manager daemon"
    exit 1
fi

set -e

running_pid()
{
# Check if a given process pid's cmdline matches a given name
    pid=$1
    name=$2
    [ -z "$pid" ] && return 1 
    [ ! -d /proc/$pid ] &&  return 1
    cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
    # Is this the expected child?
    [ "$cmd" != "$name" ] &&  return 1
    return 0
}

running_proc()
{
# Check if the process is running looking at /proc
# (works for all users)

    # No pidfile, probably no daemon present
    [ ! -f "$PIDFILE" ] && return 1
    pid=`cat $PIDFILE`

    running_pid $pid $DAEMON_WRAPPER || return 1
    
    # The daemon exists, check also that the parent process (the wrapper) has a
    # child which is $DAEMON
    pidchild=`ps -eo ppid,pid |grep ^$pid | awk '{print $2}'| head -1`
    running_pid $pidchild $DAEMON || return 1

    return 0
}

running_daemon()
{
# Check if the process is running using 'daemon'
# (only works for root)
        $DAEMON_WRAPPER --running --pidfile $PIDFILE \
            --name=$NAME 
        return $?
}

running()
{
# Check if the process is running
# Use one function or other depending if we are root or not
    if [ "`id -u`" != "0" ] ; then
        running_proc
        return $?
    fi
    running_daemon
    return  $?
}

start_xorp() {
# Start the process using the wrapper
        $DAEMON_WRAPPER  --pidfile $PIDFILE \
            --name=$NAME \
            --stdout=$LOGFILE \
            --stderr=$LOGFILE_ERR \
            -- $DAEMON  $DAEMON_OPTS
        errcode=$?
	return $errcode
}

stop_xorp () {
# Stop the process using the wrapper
        $DAEMON_WRAPPER --stop --pidfile $PIDFILE \
            --name=$NAME \
        errcode=$
	return $errcode
}


force_stop() {
# Force the process to die killing it manually
	[ ! -e "$PIDFILE" ] && return
	if running ; then
		kill -15 $pid
	# Is it really dead?
		sleep "$DIETIME"s
		if running ; then
			kill -9 $pid
			sleep "$DIETIME"s
			if running ; then
				echo "Cannot kill $NAME (pid=$pid)!"
				exit 1
			fi
		fi
	fi
	rm -f $PIDFILE
}


case "$1" in
  start)
	log_daemon_msg "Starting $DESC " "$NAME"
        if running ;  then
            log_progress_msg "apparently already running"
            log_end_msg 0
            exit 0
        fi
        if start_xorp && running ;  then
            log_end_msg 0
        else
            log_end_msg 1
        fi
	;;
  stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        if running ; then
            stop_xorp
            log_end_msg $?
        else
            log_progress_msg "apparently not running"
            log_end_msg 0
            exit 0
        fi
        ;;
  reload)
        log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
        log_warning_msg "cannot re-read the config file (use restart)."
        ;;
  force-stop)
        # First try to stop gracefully
        $0 stop
        if running; then
            log_daemon_msg "Stopping (force) $DESC" "$NAME"
            force_stop
            log_end_msg $?
        fi
	;;
  restart|force-reload)
        log_daemon_msg "Restarting $DESC" "$NAME"
        stop_xorp
        sleep $DIETIME
        start_xorp
        running
        log_end_msg $?
	;;
  status)
        log_daemon_msg "Checking status of $DESC" "$NAME"
        if running ;  then
            log_progress_msg "running"
            log_end_msg 0
        else
            log_progress_msg "apparently not running"
            log_end_msg 1
            exit 1
        fi
        ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
