#!/bin/sh

# $Progeny: configure-network,v 1.16 2002/02/15 22:21:21 branden Exp $

# XXX: This unfortunate duplication is made necessary by udhcpc scrubbing
# the environment.
LOGFILE=/var/log/installer.log

ifile=/etc/network/interfaces

clear_routing_table () {
	# clear routing table
	while :; do
		route del default 2> /dev/null
		[ $? -ne 0 ] && break
	done;
}

[ "$#" -eq 1 ] || exit 1

case "$1" in
	deconfig)
		clear_routing_table
		ifconfig $interface 0.0.0.0
		echo "Interface $interface $1" >> $LOGFILE
		;;
	bound|renew)
		clear_routing_table
		ifconfig $interface $ip netmask $subnet broadcast $broadcast
		if [ -n "$router" ]; then
			for GATEWAY in $router; do
				route add default gw $GATEWAY
			done
		fi

		rm -f /etc/resolv.conf
		if [ -n "$domain" ]; then
			echo "domain $domain" > /etc/resolv.conf
		fi
		# XXX: resolv.conf(5) says only up to 3 nameservers can be used
		for NAMESERVER in $dns; do
			echo "nameserver $NAMESERVER" >> /etc/resolv.conf
		done

		echo "debian" > /etc/hostname

		rm -f $ifile
		cat >> $ifile << EOF
auto lo eth0
iface lo inet loopback
EOF
		if [ -n "$PGI_STATIC" ]; then
			cat >> $ifile << EOF
iface eth0 inet static
	address $ip
	netmask $subnet
	broadcast $broadcast
	gateway $router
EOF
		else
			echo "iface eth0 inet dhcp" >> $ifile
		fi

		rm -f /etc/hosts
		cat >> /etc/hosts << EOF
127.0.0.1 localhost
$ip debian
EOF

		cat >> $LOGFILE << EOF
Interface $interface $1
           IP $ip
      netmask $subnet
    broadcast $broadcast
    router(s) $router
       domain $domain
nameserver(s) $dns
EOF
		;;
	*)
		exit 2
		;;
esac

exit 0
