#!/bin/sh

CONFIG=$1

LOGGER="/bin/logger -t $0"

if [ -f /etc/image_features ] ; then
    . /etc/image_features
    else
    echo
    echo
    echo Fatal error: Could not find 'image_features' file
    echo
    exit 1
fi


. $CONFIG
. /etc/sysconfig/rc.conf
. /bin/network-functions

[ -z "$REALDEVICE" ] && REALDEVICE=`echo ${DEVICE} | sed 's/:.*//g'`

# Link layer configuration
# First check if device is already down
# we must change hwaddress on down'ed device.

DEVICEUP=`/bin/ip addr show $REALDEVICE | grep ",UP"`
if [ -z "$DEVICEUP" ]  ; then

	DEV_CONFIG=

##	# In case of enabled redundancy and if device is in master mode,
##	# we must not change the MAC address of eth0 device
##	if [ "$START_KEEPALIVED" = "true" ] && [ "$REALDEVICE" = "eth0" ] && [ -f /tmp/masterMode ] ; then
##		DEV_CONFIG=
##	else
		if [ -n "$MACADDR" ] ; then
			DEV_CONFIG="address $MACADDR "
		else
			if [ "$SUBPRODUCT_TYPE" != MIDI_IPPBX ] || [ "$HW_TYPE" != HW_TYPE_PC ]; then
				# get original MAC addr from previously created file
				MACADDR=$(/bin/grep $REALDEVICE /tmp/ori_mac_addrs | /bin/cut -f 2 -d '=')

				if [ -n "$MACADDR" ] ; then
					DEV_CONFIG="address $MACADDR "
				fi
		    fi
		fi
##	fi

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

	if [ -n "$DEV_CONFIG" ] ; then
		$LOGGER "/bin/ip link set dev $REALDEVICE $DEV_CONFIG"
		/bin/ip link set dev $REALDEVICE $DEV_CONFIG
	fi

	$LOGGER "/bin/ip link set up dev $REALDEVICE"
	if ! /bin/ip link set up dev $REALDEVICE ; then
		$LOGGER Error: cannot enable interface $REALDEVICE
		exit 1
	fi

fi

# IP layer configuration


if [ -z "$IPADDR" ] ; then
	$LOGGER "Warning: No ip address given for $REALDEVICE. IP configuration aborted (see PPP/dhclient?)"
	exit 0
fi

ABCMaskLen
pfxlen=$?
pfx="$IPADDR/$pfxlen"

if [ "$REALDEVICE" = "lo" ] ; then
	scope=host
else
	scope=global
fi

$LOGGER "/bin/ip address flush dev $REALDEVICE label $DEVICE"
/bin/ip address flush dev $REALDEVICE label $DEVICE > /dev/null 2>&1

$LOGGER "/bin/ip address add $pfx dev $REALDEVICE lable $DEVICE broadcast + scope $scope"
if ! /bin/ip address add $pfx dev $REALDEVICE label $DEVICE broadcast + scope $scope; then
	$LOGGER "Fatal error: failed to add $IPADDR on $REALDEVICE (label $DEVICE)"
	exit 1
fi

# IPv6 configuration
if [ "$IPV6INIT" = "yes" ] ; then
  if [ ! -z "$IPV6ADDR" ] ; then
	$LOGGER "/bin/ip -6 addr flush dev $REALDEVICE"
	/bin/ip -6 addr flush dev $REALDEVICE
    $LOGGER "/bin/ip -6 addr add $IPV6ADDR dev $REALDEVICE"
    /bin/ip -6 addr add $IPV6ADDR dev $REALDEVICE
  fi
fi

if [ -z "$DEVICEUP" ]  ; then

# This is not supported on 2.6 kernels
##	echo 1 > /proc/sys/net/ipv4/conf/$REALDEVICE/hidden
# this could be used instead
## arp_announce=2 and arp_ignore=1/2/3

##	# prevent notifier being called twice/wrong - WAN interface notification will be done by init.d/network if gateway is ready configured
##	if [ "$REALDEVICE" = "eth0" ] ; then
		# notify voice applications (asynchronous)
		/bin/notifier $REALDEVICE up &
##	fi
fi

# send some ARP replies to help switches and routers to learn (asynchronous)
if [ "$DEVICE" != "lo" ] ; then
	/bin/arping -A -c 3 -I $REALDEVICE $IPADDR > /dev/null 2>&1 &
fi
