#!/bin/bash

#	FarSync X21 driver for Linux (generic HDLC interface version)
#
#	PVC interface up script for systems without Red Hat style network config
#	Based heavily on the Red Hat 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-db
#	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

COMPNY_DIR=/etc/farsite
CONFIG_DIR=$COMPNY_DIR/farsync

cd ${CONFIG_DIR}

CONFIG=$1
. $CONFIG

# Create the PVC and attach to the master device
farutil $MASTERDEV create $DLCI
if [ $? == 1 ]
then
  exit 1
fi
# Stop here if this is a SuSE bootstrap and it's been flagged that the SuSE
# config will handle the network part
if [ "foo$2" = "fooboot" -a \( "${ONBOOT}" = "suse" -o "${ONBOOT}" = "SUSE" \) ]
then
	exit 0
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

if [ -f /etc/sysconfig/network ]
then
	# 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
fi

exit 0

#
#	Revision History
#	$Log: ifup-pvc-db,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
#
