#!/bin/bash

#	FarSync X21 driver for Linux (generic HDLC interface version)
#
#	PVC interface up script for Red Hat like network config systems.
#	Based heavily on other similar system scripts.
#
#	Copyright (C) 2001 FarSite Communications Ltd.
#	www.farsite.co.uk
#
#	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.
#
#	File:	ifup-pvc-rh
#	Author:	R.J.Dunlop	<bob.dunlop@farsite.co.uk>
#
#	Revision history at end of file
#

PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin

cd /etc/sysconfig/network-scripts
. network-functions

CONFIG=$1
source_config

# Create the PVC and attach to the master device
farutil $MASTERDEV create $DLCI
if [ $? == 1 ]
then
  exit 1
fi


# Now gather ifconfig parameters
IFOPTS=""
PREOPTS=""

if [ -n "$MTU" ]
then
	PREOPTS="$PREOPTS mtu $MTU"
fi

if [ -n "$POINTOPOINT" ]
then
	IFOPTS="$IFOPTS pointopoint $POINTOPOINT"
else
	IFOPTS="$IFOPTS pointopoint"
fi

ifconfig $DEVICE $PREOPTS $IPADDR $IFOPTS


# If user has defined network or netmask values assume they refer to the network
# at the end of the p2p link. Difficult to see a use for defining a route to a
# local network on this sort of link.
if [ -n "$NETMASK" -o -n "$NETWORK" ]
then
	#Try to fill in any missing bits
	if [ -z "$NETMASK" ]
	then
		eval `/bin/ipcalc --netmask ${NETWORK}`

	elif [ -z "$NETWORK" ]
	then
		if [ -n "$POINTOPOINT" ]
		then
			eval `/bin/ipcalc --network ${POINTOPOINT} ${NETMASK}`
		else
			eval `/bin/ipcalc --network ${IPADDR} ${NETMASK}`
		fi
	fi

	# Add a route to the remote network if one isn't already defined
	if route -n | sed "s/ .*//" | grep ${NETWORK} > /dev/null
	then
		: Route to the network already exists
	else
		if [ -n "$POINTOPOINT" ]
		then
			route add -net ${NETWORK} netmask ${NETMASK} gw ${POINTOPOINT} dev ${DEVICE}
		else
			route add -net ${NETWORK} netmask ${NETMASK} dev ${DEVICE}
		fi
	fi
fi

# Get global networking parameters
. /etc/sysconfig/network

# If we're identified as the default gateway device add the route.
# Note we're slightly less aggressive than the standard Ethernet interface in
# that if no device is set we don't assume we're it.
if [ "${GATEWAYDEV}" = "${DEVICE}" ]
then
	if [ -n "${GATEWAY}" ]
	then
		route add default gw ${GATEWAY} ${DEVICE}
	else
		route add default ${DEVICE}
	fi
fi


# Chain to standard post up work
exec /etc/sysconfig/network-scripts/ifup-post $CONFIG $2

#
#	Revision History
#	$Log: ifup-pvc-rh,v $
#	Revision 1.2  2009/07/10 08:42:42  kevinc
#	Merging 1.09.04 and async branches
#	
#	Revision 1.1.1.1  2009/05/06 12:45:30  kevinc
#	
#
