#!/bin/sh
# if we are already running on a static ip address. Allow it or the gateway or the netmask to be changed
# to reflect the new values.
# This script requires the ipaddress, the netmask, and the gateway

if [ $# -lt 3 ]
then
   echo Not enough args! This script requires the ipaddress, the netmask, and the gateway.
   exit
fi 

#default interface is eth0
if [ "$4" = "" ]
then
    INTERFACE="eth0"
else
    INTERFACE=$4
fi

if [ "$INTERFACE" = "eth0" ]
then
    sed -e 's/IPADDR0=\".*\"/IPADDR0=\"'"$1"'\"/' /etc/rc.d/rc.prefs > /tmp/rc.prefs.1
    mv /tmp/rc.prefs.1 /etc/rc.d/rc.prefs
    sed -e 's/NETMASK0=\".*\"/NETMASK0=\"'"$2"'\"/' /etc/rc.d/rc.prefs > /tmp/rc.prefs.1
    mv /tmp/rc.prefs.1 /etc/rc.d/rc.prefs
    sed -e 's/GATEWAY0=\".*\"/GATEWAY0=\"'"$3"'\"/' /etc/rc.d/rc.prefs > /tmp/rc.prefs.1
    mv /tmp/rc.prefs.1 /etc/rc.d/rc.prefs
elif [ "$INTERFACE" = "wlan0" ]
then
    sed -e 's/IPADDR1=\".*\"/IPADDR1=\"'"$1"'\"/' /etc/rc.d/rc.prefs > /tmp/rc.prefs.1
    mv /tmp/rc.prefs.1 /etc/rc.d/rc.prefs
    sed -e 's/NETMASK1=\".*\"/NETMASK1=\"'"$2"'\"/' /etc/rc.d/rc.prefs > /tmp/rc.prefs.1
    mv /tmp/rc.prefs.1 /etc/rc.d/rc.prefs
    sed -e 's/GATEWAY1=\".*\"/GATEWAY1=\"'"$3"'\"/' /etc/rc.d/rc.prefs > /tmp/rc.prefs.1
    mv /tmp/rc.prefs.1 /etc/rc.d/rc.prefs
else
    echo arg 1 must be eth0 or wlan0
    exit
fi

#save the rc.prefs to our mirrored directory so that it will be restored on startup.
mkdir -p /opt/fastec/settings/etc/rc.d
cp /etc/rc.d/rc.prefs /opt/fastec/settings/etc/rc.d/rc.prefs

#restart interface
/opt/fastec/scripts/interface_start $INTERFACE $1 $2 $3 

