#!/bin/sh

# default values defined in specs
IPADDRESS=127.0.0.1
NETMASK=255.255.255.248
GW=0.0.0.0
DNS1=172.26.23.3
DNS2=0.0.0.0
METACHANNEL=239.0.2.10:22222
BOOTCAST_TIMEOUT=30
LOOP_BACK_IP=127.0.0.1
NULL_IP=0.0.0.0

TOKENS_DIR="/etc/infosvc/tokens"
CFG_IP_ETH0_ADDR="cfg.ip.eth0.addr"
CFG_IP_ETH0_NETMASK="cfg.ip.eth0.mask"
CFG_IP_GATEWAY="cfg.ip.gateway"
CFG_IP_DNS1="cfg.ip.dns1"
CFG_IP_DNS2="cfg.ip.dns2"
CFG_IP_METACHANNEL="cfg.ip.metachannel"

setupIgmp()
{
	test -d /proc/sys/net/ipv4/conf/eth0 && echo "2" > /proc/sys/net/ipv4/conf/eth0/force_igmp_version
	test -d /proc/sys/net/ipv4/conf/default && echo "2" > /proc/sys/net/ipv4/conf/default/force_igmp_version
	test -d /proc/sys/net/ipv4/conf/all && echo "2" > /proc/sys/net/ipv4/conf/all/force_igmp_version
	test -d /proc/sys/net/ipv4/conf/lo && echo "2" > /proc/sys/net/ipv4/conf/lo/force_igmp_version
}

exportStaticIPConfiguration() 
{
	setupIgmp
	/bin/setup_opch_loader -L -i $IPADDRESS -n $NETMASK -g $GW -p $DNS1 -s $DNS2 -m $METACHANNEL -o $BOOTCAST_TIMEOUT /dev/info0 /dev/info1
}

exportDynamicIPConfiguration() 
{
	/bin/setup_opch_loader -L -i 0.0.0.0 -o $BOOTCAST_TIMEOUT /dev/info0 /dev/info1
}

if [ -e $TOKENS_DIR/$CFG_IP_ETH0_ADDR ]; then
    IPADDRESS=`cat $TOKENS_DIR/$CFG_IP_ETH0_ADDR`
    if [ $IPADDRESS = $NULL_IP ]; then
	IPADDRESS=$LOOP_BACK_IP
    fi
fi

if [ -e $TOKENS_DIR/$CFG_IP_ETH0_NETMASK ]; then
    NETMASK=`cat $TOKENS_DIR/$CFG_IP_ETH0_NETMASK`
fi

if [ -e $TOKENS_DIR/$CFG_IP_GATEWAY ]; then
    GW=`cat $TOKENS_DIR/$CFG_IP_GATEWAY`
fi

if [ -e $TOKENS_DIR/$CFG_IP_DNS1 ]; then
    DNS1=`cat $TOKENS_DIR/$CFG_IP_DNS1`
fi

if [ -e $TOKENS_DIR/$CFG_IP_DNS2 ]; then
    DNS2=`cat $TOKENS_DIR/$CFG_IP_DNS2`
fi

if [ -e $TOKENS_DIR/$CFG_IP_METACHANNEL ]; then
    METACHANNEL=`cat $TOKENS_DIR/$CFG_IP_METACHANNEL`
fi

# firmware always in static mode
exportStaticIPConfiguration;

################################################################
# Actualizar HOSTNAME y remontar los recursos de red en los cambios de info de red
################################################################
if [ -e  /home/telefonica/store/cfg.nombreequipo ]; then 
    hostname -F /home/telefonica/store/cfg.nombreequipo
fi 

if [ -e  /home/telefonica/localpages/mediacenter/mountexports ]; then 
    sh /home/telefonica/localpages/mediacenter/mountexports
fi 
