#!/bin/bash
# $Id: stage2,v 1.1 2005-01-30 23:47:35 tim Exp $

export PATH=/usr/sbin:/usr/bin:/sbin:/bin

umount /initrd
umount /devfs

NETWORK_CARDS="
    3c501 3c507 3c509 3c515 3c523 3c527 3c59x 8139cp 8139too 82596
    ac3200 aironet4500_card amd8111e arlan-proc arlan at1700 atp
    b44 de4x5 de600 de620 depca dl2k dmfe eepro100 epic100 es3210
    ewrk3 fealnx forcedeth hamachi hp100 ibmlana lne390 lp486e
    natsemi ne2 ne2k-pci ne3210 ni65 ns83820 pcnet32 r8169 rcpci
    rrunner sb1000 sis900 sk_mca smc-mca smc-ultra32 starfire
    sundance sunhme tg3 tlan typhoon via-rhine winbond-840 yellowfin"

echo_on()
{
    echo 7 4 1 7 >/proc/sys/kernel/printk
}

echo_off()
{
    echo 0 4 1 7 >/proc/sys/kernel/printk
}

load_network_driver()
{
    echo
    echo "Trying to determine the network card"
    echo
    echo "I will stop at the first driver I successfully load"
    echo "If this driver doesn't work you should reboot and then"
    echo "tell me to start at ONE MORE than the one that didn't work"
    echo
    echo "What driver do you want me to start from?"
    echo "type 1<return> to start from the beginning"
    echo "type 0<return> to skip network card detection"
    echo
    echo "type exit for a shell"
    echo
    read f
    if [ "$f" = "" ]; then f=1; fi
    if [ "$f" = "0" ]; then return 0; fi
    if [ "$f" = "exit" ]; then return 2; fi
    j=0
    for i in $NETWORK_CARDS; do
	j=$[ $j + 1 ]
	if [ $j -lt $f ]; then
	    echo "$j) Skipping $i"
	else
	    echo "$j) Trying to install $i"
	    if modprobe $i >/dev/null 2>&1; then
		echo loaded network driver $i successfully
		return 0
	    fi
	fi
    done

    return 1
}

while true; do
    echo_off
    load_network_driver
    rcode=$?
    echo_on
    case ${rcode} in
	0)
	    break
	    ;;
	2)
	    echo
	    echo "Once you have loaded a network card, type"
	    echo "exec /stage2<return>"
	    echo "to continue with the booting process and type '0<return>'"
	    echo "when it asks what driver you want to start from"
	    echo
	    exec /bin/sh
	    ;;
    esac
    echo
    echo "I don't seem to have managed to find a network driver"
    echo
done

while true; do
    echo "Do you want to configure a static IP address (y/n)?"
    read yn
    if [ "$yn" = "n" ]; then
	echo "Defaulting to a dhcp network setup"
	address=192.168.0.1
	netmask=255.255.255.0
	gateway=192.168.0.254
	echo 'netenv_id="dhcp"' >/etc/netenv/mapping
	break;
    fi
    if [ "$yn" = "y" ]; then
	echo "Enter IP address"
	read address
	echo "Enter netmask"
	read netmask
	echo "Enter gateway"
	read gateway
	echo
	echo address=$address
	echo netmask=$netmask
	echo gateway=$gateway
	echo
	echo "OK (y/n)?"
	read yn
	if [ "$yn" = "y" ]; then
	    echo 'netenv_id="static"' >/etc/netenv/mapping
	    break;
	fi
    fi
done

cat <<EOF >/etc/network/interfaces
# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)

# The loopback interface
auto lo
iface lo inet loopback

# The first network card - this entry was created during the Debian installation
auto eth0

mapping eth0
        script /usr/local/sbin/map-scheme
        map dhcp eth0-dhcp
        map static eth0-static
        map nonet eth0-nonet

#This line is deliberately commented to stop eth0 coming up.
#iface eth0-nonet inet dhcp

iface eth0-static inet static
        address $address
        netmask $netmask
        gateway $gateway

iface eth0-dhcp inet dhcp

EOF

umount /proc

exit 0
