#!/bin/bash

echo "$0 start: $(date)" >> /tmp/sequence

sleep 3

. /tmp/cmdline 

if ifconfig| grep -q 'eth0:avahi'; then
    IP_INTERFACE=eth0:avahi
else
    IP_INTERFACE=eth0
fi

IP_ADDRESS=`ifconfig  $IP_INTERFACE | grep 'inet addr:\|inet Adresse:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
IP_MASK=`ifconfig  $IP_INTERFACE | grep 'inet addr:\|inet Adresse:'| grep -v '127.0.0.1' | cut -d: -f4 | awk '{ print $1}'`
IP_GW=`route -n|grep -m 1 '^0.0.0.0'| awk '{print $2}'`

IP_ADDRESS6=`ifconfig  $IP_INTERFACE | grep 'inet6 addr:\|inet6 Adresse:'| grep -v 'Scope:Link' | grep -v 'Scope:Host' | head -n 1 | sed -e "s,[^:]*.\([^/]*\).*,\\1," | awk '{ print $1}'`

if grep -q "IP_DHCPMODE=" /tmp/cmdline; then
    sed -i -e "s/IP_DHCPMODE=.*$/IP_DHCPMODE=$IP_DHCPMODE/" /tmp/cmdline
else
    echo "export IP_DHCPMODE=$IP_DHCPMODE" >> /tmp/cmdline        
fi

if grep -q "IP_INTERFACE=" /tmp/cmdline; then
    sed -i -e "s/IP_INTERFACE=.*$/IP_INTERFACE=$IP_INTERFACE/" /tmp/cmdline
else 
    echo export IP_INTERFACE=$IP_INTERFACE >>/tmp/cmdline
fi

if grep -q "IP_ADDRESS=" /tmp/cmdline; then
   sed -i -e "s/IP_ADDRESS=.*$/IP_ADDRESS=$IP_ADDRESS/" /tmp/cmdline
else
   echo export IP_ADDRESS=$IP_ADDRESS >> /tmp/cmdline
fi

if grep -q "IP_MASK=" /tmp/cmdline; then
    sed -i -e "s/IP_MASK=.*$/IP_MASK=$IP_MASK/"  /tmp/cmdline
else 
    echo export IP_MASK=$IP_MASK >> /tmp/cmdline
fi
if grep -q "IP_GW=" /tmp/cmdline; then
    sed -i -e "s/IP_GW=.*$/IP_GW=$IP_GW/"  /tmp/cmdline
else
    echo export IP_GW=$IP_GW >> /tmp/cmdline
fi

echo $IP_ADDRESS > /tmp/ipaddress
echo $IP_ADDRESS6 > /tmp/ipaddress6

#sbc prepare
killall udpconfd
udpconfd &

#killall snfsserver
#snfsserver ${IP_ADDRESS} 3215&
snfsserver 0.0.0.0 3215 &

sed -i "s/serverlisten.*/serverlisten\t\t${IP_ADDRESS} 127.0.0.1/" /etc/nhttpd/nhttpd.conf
service nhttpd.omg restart

# Set time
which checkntp && checkntp -s

echo "$0 stop: $(date)" >> /tmp/sequence
