#! /bin/sh

set -e

install_init()
{
	#Examine system bootstrap files.
        if [ -d /etc/rc0.d ]
        then RC_DIR=/etc
        elif [ -d /etc/rc.d/rc0.d ]
        then RC_DIR=/etc/rc.d
	else return 0
        fi

	PROD=wanrouter
	START_SCRIPT=S20wanrouter
	STOP_SCRIPT=K10wanrouter
	PROD_INIT=/usr/sbin/wanrouter

	# Install start scripts.
        [ -d $RC_DIR/rc2.d ] && ln -sf $PROD_INIT $RC_DIR/rc2.d/$START_SCRIPT
        [ -d $RC_DIR/rc3.d ] && ln -sf $PROD_INIT $RC_DIR/rc3.d/$START_SCRIPT
        [ -d $RC_DIR/rc5.d ] && ln -sf $PROD_INIT $RC_DIR/rc4.d/$START_SCRIPT
        [ -d $RC_DIR/rc5.d ] && ln -sf $PROD_INIT $RC_DIR/rc5.d/$START_SCRIPT
 
        # Install stop scripts.
        [ -d $RC_DIR/rc0.d ] && ln -sf $PROD_INIT $RC_DIR/rc0.d/$STOP_SCRIPT
        [ -d $RC_DIR/rc1.d ] && ln -sf $PROD_INIT $RC_DIR/rc1.d/$STOP_SCRIPT
        [ -d $RC_DIR/rc6.d ] && ln -sf $PROD_INIT $RC_DIR/rc6.d/$STOP_SCRIPT
        [ -d $RC_DIR/init.d ] && ln -sf $PROD_INIT $RC_DIR/init.d/$PROD
 
        return 0
}

if [ -d "/usr/local/wanrouter" ]; then
	cat <<EOM

*** Previous installation of Wanpipe detected.
    Please use /usr/sbin/wancfg instead of /usr/local/wanrouter/wancfg
    for Wanpipe configuration.
    The new configuration files will be saved in /etc/wanpipe
    and /etc/wanpipe/interfaces directories.

EOM
else
	echo 'no old wanpipe detected' > /dev/null
fi

cat <<EOM

*** Sangoma Wanpipe was successfully installed.
    Run wancfg command to configure wanpipe.
    Refer to /usr/share/doc/wanpipe for documentation.

    Docs: README-2.config
          README-3.operation
	  README-4.debugging

    Hardware Probe: /usr/sbin/wanrouter hwprobe
    Wanpipe Config: /usr/sbin/wancfg
    Wanpipe Start : /usr/sbin/wanrouter start

EOM

install_init;

#check dependancies for the new modules
eval "depmod -a > /dev/null 2> /dev/null &"
echo

eval "modprobe wanrouter"
if [ $? -eq 0 ]; then
	modprobe -r wanrouter 2>> /dev/null
else
	echo "Failed to load wanpipe modules!"
	echo
	echo "Make sure you are installing correct RPMS for your system!"
	echo
	echo "Otherwise call Sangoma Tech Support"
fi

exit 0;
