#!/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
#
### BEGIN INIT INFO
# Provides:          miplv6
# Required-Start:    $syslog
# X-UnitedLinux-Should-Start:
# Required-Stop:     $syslog
# X-UnitedLinux-Should-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Load MIPL Mobile IPv6 module
# Description:       Automatically loads MIPL Mobile IPv6 kernel module and
#	configures it. See http://www.mipl.mediapoli.com/ for more information.
### END INIT INFO
#
# mobile-ip6.  Generated from mobile-ip6.in by configure.

MIPV6_BASE_MODULE=mip6_base
MIPV6_MN_MODULE=mip6_mn
MIPV6_HA_MODULE=mip6_ha
MIPV6_MODULE=

prefix=/usr
exec_prefix=${prefix}
initdir=/etc/init.d
sysconfdir=${prefix}/etc

CONFIG_FILE=/etc/sysconfig/miplv6
MIPDIAG=${exec_prefix}/sbin/mipdiag
SUBSYS=$1
LOCKDIR=/var/lock/subsys

MODPROBE=/sbin/modprobe
LSMOD=/sbin/lsmod

# 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.status ; then
    . /etc/rc.status
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

if [ -r $CONFIG_FILE ] ; then
    . $CONFIG_FILE
else
    echo "Cannot read configuration file: $CONFIG_FILE"
    exit 1
fi

for file in $MIPDIAG $MODPROBE $LSMOD ; do
    if [ ! -x $file ] ; then
	echo "Cannot execute: $file"
	exit 1
    fi
done

if [ "$FUNCTIONALITY" = "mn" ] ; then
    MIPV6_MODULE=$MIPV6_MN_MODULE
elif [ "$FUNCTIONALITY" = "ha" ] ; then
    MIPV6_MODULE=$MIPV6_HA_MODULE
else
    MIPV6_MODULE=$MIPV6_BASE_MODULE
fi

if [ -z "$HOMEADDRESS" -a "$FUNCTIONALITY" = "mn" ] ; then
    echo "Home address not set for mobile node"
    exit 1
fi

MIPV6_MODULE_BASE=MIPV6_MODULE

case "$1" in
    start)
	$LSMOD | grep $MIPV6_MODULE_BASE &>/dev/null && \
	    echo "$MIPV6_MODULE already installed" && exit 1

	INSMOD_ARGS=

	if [ -n "$DEBUGLEVEL" ] ; then
	    INSMOD_ARGS="$INSMOD_ARGS mipv6_debug=$DEBUGLEVEL"
	fi

	$MODPROBE $MIPV6_BASE_MODULE $INSMOD_ARGS &> /dev/null
	status=$?

	if [ "$FUNCTIONALITY" = "mn" -o "$FUNCTIONALITY" = "ha" ] ; then
	    $MODPROBE $MIPV6_MODULE &> /dev/null
	fi

	action "Starting Mobile IPv6:" [ $status -eq 0 ] || exit 1

	touch $LOCKDIR/mobile-ip6

	# Temporary kludge to control get the new sysctls working.
	# Should probably be integrated into mipdiag at some time.

	if [ "$FUNCTIONALITY" = "ha" ] ; then 
	    if [ -n "$MIN_TUNNEL_NR" ] ; then
		echo $MIN_TUNNEL_NR > /proc/sys/net/ipv6/mobility/min_tnls
	    fi

	    if [ -n "$MAX_TUNNEL_NR" ] ; then
		echo $MAX_TUNNEL_NR > /proc/sys/net/ipv6/mobility/max_tnls
	    fi
	fi

	if [ "$FUNCTIONALITY" = "mn" ] ; then
	    [ -n "$HOMEADDRESS" ] && \
	    $MIPDIAG -i $HOMEDEV -h $HOMEADDRESS -H$HOMEAGENT
	fi

	if [ "$FUNCTIONALITY" = "ha" -a -n "$TUNNEL_SITELOCAL" ] ; then
	    $MIPDIAG -t$TUNNEL_SITELOCAL
	fi

	if [ "$FUNCTIONALITY" = "ha" ] ; then 
	    echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
	fi 
	;;

    stop)
	action "Stopping Mobile IPv6:" [ -f $LOCKDIR/mobile-ip6 ] || exit 1

	$MODPROBE -r $MIPV6_MODULE
	if [ "$FUNCTIONALITY" = "mn" -a -n "$HOMEADDRESS" ]; then
	    /sbin/ip addr del $HOMEADDRESS dev $HOMEDEV
	fi

	rm -f $LOCKDIR/mobile-ip6
	exit $?
	;;

    restart)
	$0 stop
	$0 start
	exit $?
	;;

    status)
	$LSMOD | grep $MIPV6_MODULE > /dev/null
	if [ $? -eq 0 ]; then
		echo "$MIPV6_MODULE module is installed..."
		exit 0
	fi
       	# See if lock in $LOCKDIR exists
	if [ -f $LOCKDIR/mobile-ip6 ]; then
		echo "$MIPV6_MODULE module not installed but subsys locked"
		exit 2
	fi

	echo "$MIPV6_MODULE module is not installed"
	exit 3
	;;

    *)
	echo "Usage mobile-ip6 {start|stop|status|restart}"
	exit 1
	;;
esac
