#!/bin/sh
#
# Load MIPL Mobile IPv6 module
#
# chkconfig: 345 75 05
# description: Automatically loads MIPL Mobile IPv6 kernel module and \
# configures it. See http://www.mipl.mediapoli.com/ for more information.
# config: /etc/sysconfig/network-mip6.conf
#
# @configure_input@

prefix=@prefix@
exec_prefix=@exec_prefix@
sysconfdir=@sysconfdir@
initdir=@INITDIR@
localstatedir=@localstatedir@

CONFIG_FILE=@NETWORK_MIP6_CONF@/network-mip6.conf
MIPDIAG=@sbindir@/mipdiag
SUBSYS=$1
LOCKDIR=@LOCKDIR@

# Test if 'functions' exists. If not, define our own 'action'

if test -x /etc/rc.d/init.d/functions ; then
    . /etc/rc.d/init.d/functions
elif test -x /etc/rc.config ; then
    . /etc/rc.config
else
    action() {
	msg=$1
	shift
	eval "$*" >/dev/null 2>&1
	ret=$?
	if test $ret = 0 -a "x$*" != "x" ; then
	    msg=$msg" OK"
	else
	    msg=$msg" Failed"
	fi
	logger $msg
	echo $msg
    }
fi

[ -f $CONFIG_FILE ] || exit 0

. $CONFIG_FILE

[ -x $MIPDIAG ] || exit 0
[ -x /sbin/insmod ] || exit 0

if [ -z "$HOMEADDRESS" ] && [ "$FUNCTIONALITY" = "mn" ] ; then
    exit 1;
fi

case "$1" in
    start)
	case "$FUNCTIONALITY" in
	    mn)
		INSMOD_ARGS="mipv6_is_mn=1"
		;;
	    ha)
		INSMOD_ARGS="mipv6_is_ha=1"
		;;
	    cn)
		INSMOD_ARGS=
		;;
        esac
	if [ ! -z "$DEBUGLEVEL" ]; then
	    INSMOD_ARGS="$INSMOD_ARGS mipv6_debug=$DEBUGLEVEL";
	fi

	/sbin/insmod mobile_ip6 $INSMOD_ARGS &> /dev/null
	action "Starting Mobile IPv6:" [ $? -eq 0 ] || exit 1

	touch $LOCKDIR/mobile-ip6

	if [ "$FUNCTIONALITY" = "mn" ] ; then
	    [ ! -z "$HOMEADDRESS" ] && $MIPDIAG -h $HOMEADDRESS;
	    [ ! -z "$HOMEAGENT" ] && $MIPDIAG -H $HOMEAGENT;
	fi

	[ "$FUNCTIONALITY" = "ha" ] && [ ! -z "$TUNNEL_SITELOCAL" ] \
	    && $MIPDIAG -t $TUNNEL_SITELOCAL

	if [ ! -z "$MOBILENODEFILE" ] && [ -f $MOBILENODEFILE ]; then
	    $MIPDIAG -R $MOBILENODEFILE;
	fi
	if [ ! -z "$SAFILE" ] && [ -f $SAFILE ]; then
	    $MIPDIAG -S $SAFILE;
	fi


	;;
    stop)
	action "Stopping Mobile IPv6:" [ -f $LOCKDIR/mobile-ip6 ] || exit 1
	/sbin/rmmod mobile_ip6
	rm -f $LOCKDIR/mobile-ip6
	exit $?
	;;
    restart)
	$0 stop
	$0 start
	exit $?
	;;
    status)
	/sbin/lsmod | grep mobile_ip6 > /dev/null
	if [ $? -eq 0 ]; then
		echo "mobile_ip6 module is installed..."
		exit 0
	fi
       	# See if lock in $LOCKDIR exists
	if [ -f $LOCKDIR/mobile-ip6 ]; then
		echo "mobile_ip6 module not installed but subsys locked"
		exit 2
	fi

	echo "mobile_ip6 module is not installed"
	exit 3
	;;
    *)
	echo "Usage mobile-ip6 {start|stop|status|restart}"
	exit 1
	;;
esac

