#!/bin/sh
# ============================================================================
# Linux Network Interface Initialization Script.
#
# This script performs TCP/IP-level initialization and should be run after
# all WAN PIPE links are brought up.  It scans a directory set in CONFDIR
# environment variable and performs initalization for each file found there.
# Files in CONFDIR directory should be named after corresponfing Linux
# interfaces, 'flip0'.  Each file should contain the variable definitions in
# form NAME=VALUE:
#
#	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
# 
# Note that this script can be used to initialize *any* Linux network
# interface, not just those created by WAN PIPE product.
# ----------------------------------------------------------------------------
# Copyright (c) 1996, Sangoma Technologies Inc.  All rights reserved.
# ============================================================================
# Rev.#	Date		Who	Details
# -----	---------------	-------	----------------------------------------------
#  2.30	Oct.16,1996	GK	Uses meta-configuration file wanpipe.rc.
#  1.00	Apr.22,1996	GK	Initial version.
# ============================================================================

PATH=/sbin:/usr/sbin:/bin:/usr/bin
WANPIPE_RC=/etc/wanpipe.rc

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

# ----------------------------------------------------------------------------
# Configure Linux network interface (TCP/IP level).
#	$1	interface name
# ----------------------------------------------------------------------------
doconfig() {
	[ "${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.
	OPTIONS=
	if [ "$NETWORK" ]
	then
		OPTIONS="-net $NETWORK"
		[ "$NETMASK" ] && OPTIONS="$OPTIONS netmask $NETMASK"
	elif [ "$POINTOPOINT" ]
	then
		OPTIONS="-host $POINTOPOINT"
	else
		OPTIONS="default"
	fi
	[ "$GATEWAY" ] && OPTIONS="$OPTIONS gw $GATEWAY"
	route add $OPTIONS $1
	return 0
}

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

# Read meta-configuration file.
[ -f "$WANPIPE_RC" ] || {
	error "Configuration file $WANPIPE_RC not found"
	exit 1
}
. $WANPIPE_RC

# Set default configuration.
WANPIPE_VERSION=${WANPIPE_VERSION:=0.0.0}
WANPIPE_HOME=${WANPIPE_HOME:=/usr/lib/wanpipe}
WANPIPE_LOG=${WANPIPE_LOG:=/var/log/wanpipe}
WANPIPE_LOCK=${WANPIPE_LOCK:=/var/lock/wanpipe}
CONFDIR=${WANPIPE_HOME}/ifi

# See how we were called.
case "$1" in
	start)
		[ -d "$CONFDIR" ] || {
			echo "Configuration directory ${CONFDIR} not found!"
			exit 1
		}
		echo -n "Configuring network interfaces (TCP/IP level):"
		cd $CONFDIR
		for i in *
		do [ -f "$i" ] && {
			[ -e "/proc/net/dev" ] && {
				cat /proc/net/dev | grep -wq $i || continue
			}
			echo -n " $i"
			ONBOOT=
			IPADDR=
			NETMASK=
			NETWORK=
			BROADCAST=
			POINTOPOINT=
			GATEWAY=
			. $i
			doconfig $i
		}
		done
		echo ""
		;;

	stop)
		echo "Shutting down network interfaces (TCP/IP level)..."
		[ -d "$CONFDIR" ] && {
			cd $CONFDIR
			for i in *
			do [ -f "$i" ] && {
				[ -e "/proc/net/dev" ] && {
					cat /proc/net/dev | grep -wq $i || continue
				}
				ifconfig $i down
			}
			done
		}
		;;

	*)
		echo "Usage: ifinit {start|stop}"
		exit 1
esac
exit 0
