#!/bin/sh -e

if [ "$uml" = "dhcp" ]; then
    ifconfig $interface up
    # First try dhclient3, then dhclient
    if ! dhclient3 -lf /tmp/dhclient.leases -sf /etc/rootstrap/dhclient-script; then
        echo "dhclient3 failed, trying dhclient"
        cp /etc/rootstrap/dhclient-script /etc
        dhclient -lf /tmp/dhclient.leases $interface
    fi
else
    ifconfig $interface $uml netmask $netmask

    if [ -n "$gateway" ]; then
	route add -host $gateway dev $interface
        route add default gw $gateway
    fi
fi

CONF=/tmp/rootstrap.netconf
echo "interface=$interface" > $CONF
echo "address=$uml" >> $CONF
echo "netmask=$netmask" >> $CONF
echo "gateway=$gateway" >> $CONF

if test -n "$domain"; then
    echo "domain $domain" > /etc/resolv.conf
fi

if test -n "$nameserver"; then
    echo "nameserver $nameserver" >> /etc/resolv.conf
fi

if [ -z "$hostname" ]; then
  hostname=rootstrap
fi
hostname "$hostname"
echo "127.0.0.1  localhost $hostname.$domain $hostname" > /etc/hosts
