#!/bin/sh
# router	WAN Router Initialization Script.
#
# copyright	(c) 1996, Sangoma Technologies Inc.
#
#		This program is free software; you can redistribute it and/or
#		modify it under the terms of the GNU General Public License
#		as published by the Free Software Foundation; either version
#		2 of the License, or (at your option) any later version.
# ============================================================================
# Nov 28, 1997	Jaspreet Singh	Updated for v2.0.1
# Nov 06, 1997	Jaspreet Singh	Updated for v2.0.0
# Jul 28, 1997	Jaspreet Singh	Updated for v1.0.5
# Jul 10, 1997	Jaspreet Singh	Updated for v1.0.4
# Dec 15, 1996	Gene Kozin	Initial version based on Sangoma's WANPIPE(tm)
# ============================================================================

####### FUNCTION DEFINITIONS #################################################

# ----------------------------------------------------------------------------
# Check to see if a value belongs to the list.
# Return:	0 - yes
#		1 - no
# ----------------------------------------------------------------------------
check_list()
{
	[ $# -lt 2 ] && return 1

	val=$1
	shift
	for i in $*
	do [ "$val" = "$i" ] && return 0
	done
	return 1
}

# ----------------------------------------------------------------------------
# Display error message.
# ----------------------------------------------------------------------------
error() {
	echo -e "$SCRIPT: $*!"
	[ -f "$ROUTER_LOG" ] && echo -e "$*!" >> $ROUTER_LOG
	return 0
}

# ----------------------------------------------------------------------------
# Configure network interface.
# This routine performs TCP/IP-level interface configuration.  Interface name
# is given as an argument. It reads a configuration file with the same name in
# and calls ifconfig and route to do the job.
#
# Configuration file is actually a shell script containing only variables:
#
#	ONBOOT=yes			if not 'yes' then skip this file
#	IPADDR=xxx.xxx.xxx.xxx		IP address of this interface
#	NETMASK=xxx.xxx.xxx.xxx		Network mask for this interface
#	NETWORK=xxx.xxx.xxx.xxx		Network address
#	BROADCAST=xxx.xxx.xxx.xxx	Broadcast address
#	POINTOPOINT=xxx.xxx.xxx.xxx	Point-to-point address
#	GATEWAY=xxx.xxx.xxx.xxx		Gateway address
# ----------------------------------------------------------------------------
interface_up()
{
	# Clear variables and read the definitions file in
	ONBOOT=
	IPADDR=
	NETMASK=
	NETWORK=
	BROADCAST=
	POINTOPOINT=
	GATEWAY=
	. $1

	[ "${ONBOOT}" != "yes" -o -z "$IPADDR" ] && return 0

	# Configure interface.
	[ "$IPADDR" ] && {
		# Prepare ifconfig options.
		OPTIONS=
		[ "$NETMASK" ] && OPTIONS="$OPTIONS netmask $NETMASK"
		[ "$BROADCAST" ] && OPTIONS="$OPTIONS broadcast $BROADCAST"
		[ "$POINTOPOINT" ] && OPTIONS="$OPTIONS pointopoint $POINTOPOINT"

		ifconfig $1 $IPADDR $OPTIONS
	}

	# Update routing table.
	[ "$POINTOPOINT" ] && route add -host $POINTOPOINT

	[ "$NETWORK" ] && {
		OPTIONS="-net $NETWORK"
		[ "$NETMASK" ] && OPTIONS="$OPTIONS netmask $NETMASK"
		[ "$GATEWAY" ] && OPTIONS="$OPTIONS gw $GATEWAY"
		route add $OPTIONS $1
	}
	return 0
}

# ----------------------------------------------------------------------------
# Start WAN router.
#	o create log file
#	o check configuration file presence
#	o load WAN drivers (using modprobe)
#	o configure drivers
#	o configure interfaces
# ----------------------------------------------------------------------------
router_up()
{
	echo "Starting WAN Router..."
	echo "`date`: starting WAN router" > $ROUTER_LOG
	[ -f "$ROUTER_CONF" ] || {
		error "Configuration file $ROUTER_CONF not found"
		return 1
	}
	[ "$ROUTER_BOOT" = "NO" -o -z "$WAN_DRIVERS" ] && return 1

	echo -n "Loading WAN drivers: "
	for i in $WAN_DRIVERS
	do
		lsmod | grep -wq $i && continue
		echo -n "$i "
		echo -n "Loading driver $i ... " >> $ROUTER_LOG
		if modprobe $i
		then echo "ok" >> $ROUTER_LOG
		else echo "fail" >> $ROUTER_LOG
		fi
	done
	echo "done."

	# Configure router.
	wanconfig -v -f $ROUTER_CONF >> $ROUTER_LOG

	# Configure network interfaces.
	[ -d "$IFCONF_DIR" ] || {
		error "Directory $IFCONF_DIR not found"
		return 0
	}
	cd $IFCONF_DIR
	INTERFACES=`ls`
	[ -z "$INTERFACES" ] && {
		error "No configuration files found in $IFCONF_DIR"
		return 0
	}
	echo -n "Configuring interfaces: "
	for i in $INTERFACES
	do [ -s $i ] && {
		cat /proc/net/dev | grep -wq $i || continue
		echo -n "$i "
		interface_up $i || {
			error "Failed to configure interface $i"
			continue
		}
	}
	done
	echo "done."
	return 0
}

# ----------------------------------------------------------------------------
# Stop WAN router.
#	o shut down interfaces
#	o unload WAN drivers
#	o remove lock file
# ----------------------------------------------------------------------------
router_down()
{
	echo "Stopping WAN Router..."
	echo -e "\n`date`: stopping WAN router..." >> $ROUTER_LOG

	# Shut down network interfaces.
	[ -d "$IFCONF_DIR" ] && {
		cd $IFCONF_DIR
		INTERFACES=`ls`
		for i in $INTERFACES
		do
			cat /proc/net/dev | grep -wq $i || continue
			ifconfig $i down
		done
	}

	# Unload WAN drivers.
	for i in $WAN_DRIVERS
	do lsmod | grep -wq $i && {
		modprobe -r $i
	}
	done
	rm -f $ROUTER_LOCK 2> /dev/null
}

####### MAIN #################################################################
# set -x

export PATH=/sbin:/bin:/usr/sbin:/usr/bin
ROUTER_VERSION=2.0.1
ROUTER_RC=/etc/router.rc
ROUTER_HOME=/usr/lib/router
IFCONF_DIR=$ROUTER_HOME/interfaces
SCRIPT=router

# Ignore interrupt signals.
trap '' 2

# Read meta-configuration file.
if [ -f $ROUTER_RC ]
then . $ROUTER_RC
else echo "$SCRIPT: WARNING: $ROUTER_RC not found! Using defaults."
fi

# Set default configuration.
ROUTER_BOOT=${ROUTER_BOOT:=YES}
ROUTER_CONF=${ROUTER_CONF:=/etc/router.conf}
ROUTER_LOG=${ROUTER_LOG:=/var/log/router}
ROUTER_LOCK=${ROUTER_LOCK:=/var/lock/router}
WAN_DRIVERS=${WAN_DRIVERS:=""}

# See how we were called.
case "$1" in
	start)	[ -f "$ROUTER_LOCK" ] && {
			echo "Router is already running. Stop it first!"
			exit 1
		}
		router_up && touch $ROUTER_LOCK
		;;
	stop)	router_down
		;;
	*)	echo "WAN Router startup script"
		echo "Usage: $SCRIPT {start|stop}"
		exit 1
esac
exit 0

