#!/bin/sh

# We only do anything in here if the interface is set to manual config 
# in /etc/network/interfaces
if [ "$METHOD" == "manual" ]; then
    exec 1>/dev/ttyAMA0
    exec 2>/dev/ttyAMA0

    # Get the network settings from the netsettings.sh script
    source /app/bin/netsettings.sh

    # Make sure we have a hostname
    if [ "$NET_HOSTNAME" == "" ]; then
        NET_HOSTNAME="CANVU"
    fi
    hostname $NET_HOSTNAME 

    # Are we using static or DHCP for the network?
    if [ "$NET_MODE" == "dhcp" ]; then
        echo S01ifconfig: DHCP Mode

        # Bring up the interface    
        ifconfig $IFACE up
        
        # Start DHCP
        udhcpc -R -n -t10 -T3 -p /var/run/udhcpc.$IFACE.pid -i $IFACE -x hostname:$NET_HOSTNAME
    else
        # Static mode
        echo S01ifconfig: Static Mode

        # Bring up the interface    
        ifconfig $IFACE up
        # Set IP and subnet
        ifconfig $IFACE $NET_STATIC_IP netmask $NET_STATIC_SUBNET
        # Add the default route
        route add default gw $NET_STATIC_GATEWAY $IFACE
        
        # Setup DNS nameservers
        echo "nameserver $NET_STATIC_DNS1" > /etc/resolv.conf
        if [ "$NET_STATIC_DNS1" != "" ]; then  
            echo "nameserver $NET_STATIC_DNS2" >> /etc/resolv.conf
        fi
    fi        
fi
