#!/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}'`

if test "$IP_DHCPMODE_6" = "STATIC"; then
    IP_ADDRESS_6=`get_eep IP_ADDRESS_6`
    if [ -z "${IP_ADDRESS_6}" ]; then
        IP_ADDRESS_6=`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}'`
    fi
else
    IP_ADDRESS_6=`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}'`
fi

IP_MASK_6=`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}'`
IP_GW_6=`route -A inet6 | grep -m 1 'UG'| awk '{print $2}'`

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

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

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

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


################################################ handle second inerface

if ifconfig| grep -q 'eth1:avahi'; then
    IP2_INTERFACE=eth1:avahi
else
    IP2_INTERFACE=eth1
fi

if ifconfig| grep -q 'eth1' ; then
    IP2_ADDRESS=`ifconfig  $IP2_INTERFACE | grep 'inet addr:\|inet Adresse:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
    IP2_MASK=`ifconfig  $IP2_INTERFACE | grep 'inet addr:\|inet Adresse:'| grep -v '127.0.0.1' | cut -d: -f4 | awk '{ print $1}'`
    IP2_GW=`route -n|grep -m 1 '^0.0.0.0'| awk '{print $2}'`

    if test "$IP2_DHCPMODE_6" = "STATIC"; then
        IP2_ADDRESS_6=`get_eep IP2_ADDRESS_6`
        if [ -z "${IP2_ADDRESS_6}" ]; then
            IP2_ADDRESS_6=`ifconfig  $IP2_INTERFACE | grep 'inet6 addr:\|inet6 Adresse:'| grep -v 'Scope:Link' | grep -v 'Scope:Host' | head -n 1 | sed -e "s,[^:]*.\([^/]*\).*,\\1," | awk '{ print $1}'`
        fi
    else
        IP2_ADDRESS_6=`ifconfig  $IP2_INTERFACE | grep 'inet6 addr:\|inet6 Adresse:'| grep -v 'Scope:Link' | grep -v 'Scope:Host' | head -n 1 | sed -e "s,[^:]*.\([^/]*\).*,\\1," | awk '{ print $1}'`
    fi

    IP2_MASK_6=`ifconfig  $IP2_INTERFACE | grep 'inet6 addr:\|inet6 Adresse:'| grep -v 'Scope:Link' | grep -v 'Scope:Host' | head -n 1 | sed -e "s,[^:]*.[^/]*/\([^ ]*\).*,\\1," | awk '{ print $1}'`
    IP2_GW_6=`route -A inet6 | grep -m 1 'UG'| awk '{print $2}'`

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

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

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

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

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

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

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

    if grep -q "IP2_GW_6=" /tmp/cmdline; then
        sed -i -e "s/IP2_GW_6=.*$/IP2_GW_6=$IP2_GW_6/"  /tmp/cmdline
    else
        echo export IP2_GW_6=$IP2_GW_6 >> /tmp/cmdline
    fi
else
    echo "second IP interface not available or configured"    
fi

################################################
echo $IP_ADDRESS > /tmp/ipaddress
echo $IP_ADDRESS_6 > /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
