#!/bin/sh

# change the default range of UDP and TCP ports to avoid clash with our application ports (4000...)
echo 32768 61000 > /proc/sys/net/ipv4/ip_local_port_range

# SSH support needs server keys. This is a one-time operation.
cd /etc/dropbear
[ -f dropbear_rsa_host_key ] || (dropbearkey -t rsa -f dropbear_rsa_host_key >/dev/null 2>&1 &)
[ -f dropbear_dss_host_key ] || (dropbearkey -t dss -f dropbear_dss_host_key >/dev/null 2>&1 &)
cd /

insmod natsemi

# Want inetd running as soon as possible so don't want to wait for inittab to start it.
# Also, busybox inetd cannot be run in the foreground so it is unsuitable for
# respawning by inittab.

rm -f /var/run/inetd.pid
sh -c "while true; inetd 2>&1 | logger -t inetd; sleep 1; while [ -f /var/run/inetd.pid ]; do sleep 10; done; done" &
rm -f /var/lib/dhcpcd/dhcpcd-eth0.info

insmod ip_tables
insmod iptable_filter
insmod ip_conntrack
insmod ip_nat
insmod iptable_nat
insmod ipt_MASQUERADE

ifconfig lo 127.0.0.1

#COLD Start check
insmod flashenv
eval `grep AppEraseConfig= /dev/flash_env_all`
rmmod flashenv
cold=n
[ -n "$AppEraseConfig" ] && [ $AppEraseConfig = "y" ] && cold=y
if [ $cold = y ]; then
echo "COLD STARTING CONFIGURATION"
rm /SpliceCom/Database.txt
rm -f /tmp/vlan.cfg
/etc/init.d/vlan stop
cp /SpliceCom/def_startEthernet2 /SpliceCom/startEthernet2
cp /SpliceCom/def_localAddress /SpliceCom/localAddress
for f in /SpliceCom/startVpn /SpliceCom/starttc /etc/dhcpd_cust.conf; do
	[ -f $f ] && mv $f $f.old
done
else
echo "NORMAL STARTING"
fi

#Check if we have a startEthernet2  if not the use the default ( otherwise leave alone!)
if [ ! -f /SpliceCom/startEthernet2 ]; then
cp /SpliceCom/def_startEthernet2 /SpliceCom/startEthernet2
fi

if [ ! -f /SpliceCom/localAddress ]; then
cp /SpliceCom/def_localAddress /SpliceCom/localAddress
fi

#Start the ADSL LAN DHCP Client (debug for now)
logger "Starting DHCP Client of eth0"
rm -f /var/run/dhcpcd-eth0.pid

# Get the MAC address to form a hostname for DHCP
MAC=`ifconfig eth0 | grep Ethernet | cut -d " " -f 11`
MAC=`echo $MAC | cut -c 1,2,4,5,7,8,10,11,13,14,16,17`
HOSTNAME=CS5100-$MAC
[ -f /etc/hostname ] && HOSTNAME=`cat /etc/hostname`
hostname $HOSTNAME

cp /etc/hosts /etc/hosts.bak
grep -v localhost /etc/hosts.bak > /etc/hosts
echo "127.0.0.1 localhost $HOSTNAME" >> /etc/hosts

#execute the pre-programmed script to start the appropriate ethernet
. /SpliceCom/localAddress
. /SpliceCom/startEthernet2

sleep 1

ntpd -g

# Start checking for duplicate IP addresses on network
addrmon &

snmp_config &

# Enable IP forwarding - essential for RTP operation
echo 1 > /proc/sys/net/ipv4/ip_forward

# Enable response to broadcast ping
echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

# Enable Power-over-ethernet
insmod i2c
insmod poe

