#!/bin/bash

#	FarSync X21 driver for Linux (generic HDLC interface version)
#
#	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-hdlc-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
SHOWPROGRESS=$2

# Gather the FarSync parameters
FSOPTS=""

if [ -n "$MEDIA" ]
then
	FSOPTS="$FSOPTS media $MEDIA"
fi
if [ -n "$media" ]
then
	FSOPTS="$FSOPTS media $media"
fi

if [ -n "$CLOCK" ]
then
	FSOPTS="$FSOPTS clock $CLOCK"
fi
if [ -n "$clock" ]
then
	FSOPTS="$FSOPTS clock $clock"
fi

if [ -n "$EXTENDED" ]
then
	FSOPTS="$FSOPTS extended $EXTENDED"
fi
if [ -n "$extended" ]
then
	FSOPTS="$FSOPTS extended $extended"
fi

if [ -n "$CARRIER" ]
then
	FSOPTS="$FSOPTS carrier $CARRIER"
fi
if [ -n "$carrier" ]
then
	FSOPTS="$FSOPTS carrier $carrier"
fi

if [ -n "$PHASE" ]
then
	FSOPTS="$FSOPTS phase $PHASE"
fi
if [ -n "$phase" ]
then
	FSOPTS="$FSOPTS phase $phase"
fi

if [ -n "$PROTO" ]
then
	FSOPTS="$FSOPTS proto $PROTO"
fi
if [ -n "$proto" ]
then
	FSOPTS="$FSOPTS proto $proto"
fi

# Additional protocol specific parameters
if [ "X$PROTO" = "Xcisco" ]
then
	# Cisco HDLC keepalive interval and timeout
	if [ -n "$INTERVAL" ]
	then
		FSOPTS="$FSOPTS interval $INTERVAL"
	fi
	if [ -n "$interval" ]
	then
		FSOPTS="$FSOPTS interval $interval"
	fi

	if [ -n "$TIMEOUT" ]
	then
		FSOPTS="$FSOPTS timeout $TIMEOUT"
	fi
	if [ -n "$timeout" ]
	then
		FSOPTS="$FSOPTS timeout $timeout"
	fi

elif  [ "X$PROTO" = "Xfr" ]
then
	# LMI type and DCE operation
	if [ -n "$LMI" ]
	then
		FSOPTS="$FSOPTS lmi $LMI"
	fi
	if [ -n "$lmi" ]
	then
		FSOPTS="$FSOPTS lmi $lmi"
	fi

	if [ "X$DCE" = "Xy" -o "X$DCE" = "XY" -o "X$DCE" = "Xyes" -o "X$DCE" = "XDCE" ]
	then
		FSOPTS="$FSOPTS dce"
	fi

	# Frame relay timing and counters
	if [ -n "$T391" ]
	then
		FSOPTS="$FSOPTS t391 $T391"
	fi
	if [ -n "$t391" ]
	then
		FSOPTS="$FSOPTS t391 $t391"
	fi

	if [ -n "$T392" ]
	then
		FSOPTS="$FSOPTS t392 $T392"
	fi
	if [ -n "$t392" ]
	then
		FSOPTS="$FSOPTS t392 $t392"
	fi

	if [ -n "$N391" ]
	then
		FSOPTS="$FSOPTS n391 $N391"
	fi
	if [ -n "$n391" ]
	then
		FSOPTS="$FSOPTS n391 $n391"
	fi

	if [ -n "$N392" ]
	then
		FSOPTS="$FSOPTS n392 $N392"
	fi
	if [ -n "$n392" ]
	then
		FSOPTS="$FSOPTS n392 $n392"
	fi

	if [ -n "$N393" ]
	then
		FSOPTS="$FSOPTS n393 $N393"
	fi
	if [ -n "$n393" ]
	then
		FSOPTS="$FSOPTS n393 $n393"
	fi
fi

if [ -n "$MODE" ]
then
	FSOPTS="$FSOPTS mode $MODE"
fi
if [ -n "$mode" ]
then
	FSOPTS="$FSOPTS mode $mode"
fi

if [ -n "$DATARATE" ]
then
	FSOPTS="$FSOPTS datarate $DATARATE"
fi
if [ -n "$datarate" ]
then
	FSOPTS="$FSOPTS datarate $datarate"
fi

if [ -n "$NUMSLOTS" ]
then
	FSOPTS="$FSOPTS numslots $NUMSLOTS"
fi
if [ -n "$numslots" ]
then
	FSOPTS="$FSOPTS numslots $numslots"
fi

if [ -n "$STARTSLOT" ]
then
	FSOPTS="$FSOPTS startslot $STARTSLOT"
fi
if [ -n "$startslot" ]
then
	FSOPTS="$FSOPTS startslot $startslot"
fi

if [ -n "$STRUCTURE" ]
then
	FSOPTS="$FSOPTS structure $STRUCTURE"
fi
if [ -n "$structure" ]
then
	FSOPTS="$FSOPTS structure $structure"
fi

if [ -n "$INTERFACE" ]
then
	FSOPTS="$FSOPTS interface $INTERFACE"
fi
if [ -n "$interface" ]
then
	FSOPTS="$FSOPTS interface $interface"
fi

if [ -n "$CODING" ]
then
	FSOPTS="$FSOPTS coding $CODING"
fi
if [ -n "$coding" ]
then
	FSOPTS="$FSOPTS coding $coding"
fi

if [ -n "$LBO" ]
then
	FSOPTS="$FSOPTS lbo $LBO"
fi
if [ -n "$lbo" ]
then
	FSOPTS="$FSOPTS lbo $lbo"
fi

if [ -n "$EQUALIZER" ]
then
	FSOPTS="$FSOPTS equalizer $EQUALIZER"
fi
if [ -n "$equalizer" ]
then
	FSOPTS="$FSOPTS equalizer $equalizer"
fi

if [ -n "$LOOP" ]
then
	FSOPTS="$FSOPTS loop $LOOP"
fi
if [ -n "$loop" ]
then
	FSOPTS="$FSOPTS loop $loop"
fi

if [ -n "$RANGE" ]
then
	FSOPTS="$FSOPTS range $RANGE"
fi
if [ -n "$range" ]
then
	FSOPTS="$FSOPTS range $range"
fi

if [ -n "$TXBUFFER" ]
then
	FSOPTS="$FSOPTS txbuffer $TXBUFFER"
fi
if [ -n "$txbuffer" ]
then
	FSOPTS="$FSOPTS txbuffer $txbuffer"
fi

if [ -n "$RXBUFFER" ]
then
	FSOPTS="$FSOPTS rxbuffer $RXBUFFER"
fi
if [ -n "$rxbuffer" ]
then
	FSOPTS="$FSOPTS rxbuffer $rxbuffer"
fi

if [ -n "$IDLECODE" ]
then
	FSOPTS="$FSOPTS idlecode $IDLECODE"
fi
if [ -n "$idlecode" ]
then
	FSOPTS="$FSOPTS idlecode $idlecode"
fi

if [ -n "$TERMTYPE" ]
then
	FSOPTS="$FSOPTS termType $TERMTYPE"
fi
if [ -n "$termtype" ]
then
	FSOPTS="$FSOPTS termType $termtype"
fi

if [ -n "$ENCAP" ]
then
	FSOPTS="$FSOPTS encap $ENCAP"
fi
if [ -n "$encap" ]
then
	FSOPTS="$FSOPTS encap $encap"
fi

if [ -n "$ANNEX" ]
then
	FSOPTS="$FSOPTS annex $ANNEX"
fi
if [ -n "$annex" ]
then
	FSOPTS="$FSOPTS annex $annex"
fi

if [ -n "$TESTMODE" ]
then
	FSOPTS="$FSOPTS testMode $TESTMODE"
fi
if [ -n "$testmode" ]
then
	FSOPTS="$FSOPTS testMode $testmode"
fi

if [ -n "$BACKOFF" ]
then
	FSOPTS="$FSOPTS backoff $BACKOFF"
fi
if [ -n "$backoff" ]
then
	FSOPTS="$FSOPTS backoff $backoff"
fi

if [ -n "$VPI" ]
then
	FSOPTS="$FSOPTS vpi $VPI"
fi
if [ -n "$vpi" ]
then
	FSOPTS="$FSOPTS vpi $vpi"
fi

if [ -n "$VCI" ]
then
	FSOPTS="$FSOPTS vci $VCI"
fi
if [ -n "$vci" ]
then
	FSOPTS="$FSOPTS vci $vci"
fi

if [ -n "$SNRTH" ]
then
	FSOPTS="$FSOPTS snrth $SNRTH"
fi
if [ -n "$snrth" ]
then
	FSOPTS="$FSOPTS snrth $snrth"
fi

if [ -n "$LPATH" ]
then
	FSOPTS="$FSOPTS lpath $LPATH"
fi
if [ -n "$lpath" ]
then
	FSOPTS="$FSOPTS lpath $lpath"
fi

if [ -n "$NUMBUFFERS" ]
then
	FSOPTS="$FSOPTS numbuffers $NUMBUFFERS"
fi
if [ -n "$numbuffers" ]
then
	FSOPTS="$FSOPTS numbuffers $numbuffers"
fi

if [ -n "$NUMRXBUFFERS" ]
then
	FSOPTS="$FSOPTS numrxbuffers $NUMRXBUFFERS"
fi
if [ -n "$numrxbuffers" ]
then
	FSOPTS="$FSOPTS numrxbuffers $numrxbuffers"
fi

if [ -n "$NUMTXBUFFERS" ]
then
	FSOPTS="$FSOPTS numtxbuffers $NUMTXBUFFERS"
fi
if [ -n "$numtxbuffers" ]
then
	FSOPTS="$FSOPTS numtxbuffers $numtxbuffers"
fi

if [ -n "$SIZEBUFFERS" ]
then
	FSOPTS="$FSOPTS sizebuffers $SIZEBUFFERS"
fi
if [ -n "$sizebuffers" ]
then
	FSOPTS="$FSOPTS sizebuffers $sizebuffers"
fi

if [ -n "$SIZERXBUFFERS" ]
then
	FSOPTS="$FSOPTS sizerxbuffers $SIZERXBUFFERS"
fi
if [ -n "$sizerxbuffers" ]
then
	FSOPTS="$FSOPTS sizerxbuffers $sizerxbuffers"
fi

if [ -n "$SIZETXBUFFERS" ]
then
	FSOPTS="$FSOPTS sizetxbuffers $SIZETXBUFFERS"
fi
if [ -n "$sizetxbuffers" ]
then
	FSOPTS="$FSOPTS sizetxbuffers $sizetxbuffers"
fi

if [ -n "$TERMINATION" ]
then
	FSOPTS="$FSOPTS termination $TERMINATION"
fi
if [ -n "$termination" ]
then
	FSOPTS="$FSOPTS termination $termination"
fi

if [ -n "$NRZICLOCKING" ]
then
	FSOPTS="$FSOPTS nrziclocking $NRZICLOCKING"
fi
if [ -n "$nrziclocking" ]
then
	FSOPTS="$FSOPTS nrziclocking $nrziclocking"
fi

if [ -n "$LOWLATENCY" ]
then
	FSOPTS="$FSOPTS lowlatency $LOWLATENCY"
fi
if [ -n "$lowlatency" ]
then
	FSOPTS="$FSOPTS lowlatency $lowlatency"
fi

if [ -n "$SYNTH" ]
then
	FSOPTS="$FSOPTS synth $SYNTH"
fi
if [ -n "$synth" ]
then
	FSOPTS="$FSOPTS synth $synth"
fi

if [ -n "$LINEMODE" ]
then
	FSOPTS="$FSOPTS linemode $LINEMODE"
fi
if [ -n "$linemode" ]
then
	FSOPTS="$FSOPTS linemode $linemode"
fi

# Set FarSync parameters
if [ -n "$FSOPTS" ]
then
	farutil $DEVICE set $FSOPTS
        if [ $? -gt 0 ]
        then
            exit $?
        fi
fi

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

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

# For HDLC/CISCO/PPP we set an IP address on this interface as it is being used
# directly.  Frame Relay and X.25 will have no address set here and addresses
# will be set on the subsidary PVCs.
if [ -n "$IPADDR" ]
then
	# See how these misspellings get enshrined.
	# HDLC/CISCO/PPP are all p2p links.
	if [ -n "$POINTOPOINT" ]
	then
		IFOPTS="$IFOPTS pointopoint $POINTOPOINT netmask $NETMASK"
	else
		IFOPTS="$IFOPTS netmask $NETMASK"
	fi

	ifconfig $DEVICE $PREOPTS $IPADDR $IFOPTS
else
	ifconfig $DEVICE $PREOPTS 0.0.0.0 $IFOPTS
fi
if [ "$SHOWPROGRESS" == "show" ]
then
    /sbin/fardsl $DEVICE
fi

# 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 "$IPADDR" -a \( -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


# Bring up IPX if requested and our protocol supports it
if [ "$IPX" = "yes" -a "$PROTO" = "ppp" ]
then
	/etc/sysconfig/network-scripts/ifup-ipx $DEVICE
fi

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

#
#	Revision History
#	$Log: ifup-hdlc-rh,v $
#	Revision 1.3  2009/12/17 12:10:07  kevinc
#	Make the release version 1.09.05 - build 143
#	
#	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
#	
#
