#!/bin/sh
#
# chkconfig: 345 80 80
# description: Build the VPN kernel module, if possible.
#
# Inspired by zacheiss' afs-build-kmod
#
# $Id: vpnclient-build-kmod,v 1.3 2004/12/16 17:08:34 jdreed Exp $

if [ -f /etc/redhat-release ]; then
    . /etc/rc.d/init.d/functions
else
    echo_success() {
	echo " ok"
    }
    echo_failure() {
	echo " failed"
    }
    echo_passed() {
	echo " passed"
    }
fi

SOURCEDIR=/opt/cisco-vpnclient/src
PREBUILTDIR=/opt/cisco-vpnclient/modules

case `uname -r` in
2.[56].*)
        VPNMOD=cisco_ipsec.ko
        ;;
2.[34].*)
	VPNMOD=cisco_ipsec
	;;
*)
	echo "Unsupported kernel version"
	exit 0
esac

RETVAL=0

start() {
    echo -n $"Checking for VPN kernel module: "
    # Find the module directory
    if [ -d /lib/modules/preferred ]; then
	MDIR=/lib/modules/preferred/CiscoVPN
    else
	MDIR=/lib/modules/`uname -r`/CiscoVPN
    fi
    # Find a module
    if [ -f "$MDIR/$VPNMOD" ]; then
	echo_success
	echo
    else
	# First try a pre-built module
	if [ -d $PREBUILTDIR/`uname -r` ]; then
	    mkdir -p $MDIR
	    cp $PREBUILTDIR/`uname -r`/$VPNMOD $MDIR
	    if [ -f "$MDIR/$VPNMOD" ]; then
		echo
		echo -n $"-- using pre-built module:"
		echo_success
		echo
		exit 1
	    fi
	fi
	# Then try to build one
	if [ -f "${SOURCEDIR}/Makefile" ]; then
	    echo
	    echo -n $"-- attempting to build module:"
	    $SOURCEDIR/build-module
	    if [ -f "$MDIR/$VPNMOD" ]; then
		echo_passed
		echo
	    else
		echo_failure
		echo
		RETVAL=1
	    fi
	else
	    echo_failure
	    echo
	    RETVAL=1
	fi
    fi
    return $RETVAL
}

stop() {
    echo -n $"Stopping vpnclient-build-kmod: "
    echo_success
    echo
    return $RETVAL
}

case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    restart)
	stop
	start
	;;
    *)
	echo $"Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $RETVAL
