#!/bin/bash

MYIP=`/sbin/ifconfig eth0 | awk '/inet (addr:)?[0-9]/ { print gensub("^.*inet (addr:)?([0-9.]*) .*","\\\\2",1) }'`
NEW_IP_ADDR=`echo $1 | egrep '([0-9]{1,3}\.){3}[0-9]{1,3}'`
if [ -z "$NEW_IP_ADDR" -o "$MYIP" = "$NEW_IP_ADDR" ] 
then 
  exit 1
fi
IP_NIBBLE_1=`echo $NEW_IP_ADDR | cut -d'.' -f1`
IP_NIBBLE_2=`echo $NEW_IP_ADDR | cut -d'.' -f2`
IP_NIBBLE_3=`echo $NEW_IP_ADDR | cut -d'.' -f3`
IP_NIBBLE_4=`echo $NEW_IP_ADDR | cut -d'.' -f4`

if [ $IP_NIBBLE_1 -gt 255 -o $IP_NIBBLE_2 -gt 255 -o $IP_NIBBLE_3 -gt 255 -o $IP_NIBBLE_4 -gt 254 ]
then
  exit 1
fi
MY_LOCAL_IP="${IP_NIBBLE_1}.${IP_NIBBLE_2}.${IP_NIBBLE_3}.${IP_NIBBLE_4}"
MY_LOCAL_NET="${IP_NIBBLE_1}.${IP_NIBBLE_2}.${IP_NIBBLE_3}.0"
MY_LOCAL_BCAST="${IP_NIBBLE_1}.${IP_NIBBLE_2}.${IP_NIBBLE_3}.255"

ifconfig eth0 $MY_LOCAL_IP

cat > /etc/network/interfaces <<EOF



# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0 eth0:1

iface eth0 inet static
address $MY_LOCAL_IP
netmask 255.255.255.0
network $MY_LOCAL_NET
broadcast $MY_LOCAL_BCAST

iface eth0:1 inet dhcp

# The primary network interface
#auto eth0 
#
#iface eth0 inet dhcp

EOF

/opt/euro-beta/e3g_restart
