getresp() {
	read resp
	if [ "X$resp" = "X" ]; then
		resp=$1
	fi
}

getrepeat() {
	resp=""
	while [ -z "$resp" ]; do
		echo -n "$1"
		read resp
	done
}

getnetparams() {
	ifaddr=$1

	# Split IP address into components.
	OLDIFS="$IFS"
	IFS=.
	set -- $ifaddr
	ifA=$1 ifB=$2 ifC=$3 ifD=$4
	IFS="$OLDIFS"

	. /etc/athena/mitnetconf
	if [ -z "$proto_bcast" -a "$ifA" != 18 ]; then
		echo -n "Enter netmask [0xffffff00]: "
		getresp 0xffffff00; newnetmask="$resp";
		if "$netmask" = "0xffff0000"; then
			proto_bcast="${ifA}.${ifB}.255.255"
			proto_gateway="${ifA}.${ifB}.0.1"
		else
			proto_bcast="${ifA}.${ifB}.{$ifC}.255"
			proto_gateway="${ifA}.${ifB}.${ifC}.1"
		fi
		echo -n "Enter broadcast address [$proto_bcast]: "
		getresp "$proto_bcast"; newbcast="$resp"
		echo -n "Enter gateway address [$proto_gateway]: "
		getresp "$proto_gateway"; newgate="$resp"
	else
		if [ -z "$proto_bcast" ]; then
			proto_bcast="${ifA}.${ifB}.255.255"
		fi
		if [ -z "$proto_netmask" ]; then
			proto_netmask=0xffff0000
		fi
		if [ -z "$proto_gateway" ]; then
			proto_gateway=${ifA}.${ifB}.0.1"
		fi
		newmask="$proto_netmask"
		newbcast="$proto_bcast"
		newgate="$proto_gate"
	fi
}

newconfigs() {
	echo $newhost > /etc/myname
	echo "$newip	$newname $newname.$newdomain" >> /etc/hosts
	echo "$newgate" > /mnt/etc/mygate
	echo "inet $newname.$newdomain $newmask $newbcast $newflags" \
		> /etc/hostname.$newif
}

# Pick up existing parameters
oldgate=`cat /etc/mygate`
oldname=`cat /etc/myname`

# Are we on the net?
on_the_net=no;
ping -nqc 1 -w 1 $oldgate > /dev/null && on_the_net=yes;

if [ "$on_the_net" = yes ]; then
	# Find current primary interface.
	oldifs=`ls /etc/hostname.* | sed 's/.*\.\([^ ]*\).*$/\1/'`
	echo "Current network interfaces configured: $oldifs"
	if [ "`echo $oldifs | wc -w`" -eq 1 ]; then
		echo -n "Enter new interface [$oldifs]: "
		getresp "$oldifs"; newif="$resp"
	else
		getrepeat "Enter new interface: "; newif="$resp"
	fi
	if [ -f /etc/hostname.$newif ]; then
		oldflags=`cat /etc/hostname.$newif | awk '{print $5 $6 $7;}'`
		echo -n "Enter new flags [$oldflags]: "
		getresp "$oldflags"; newflags="$resp"
	else
		getrepeat "Enter new interface: "; newflags="$resp"
	fi

	# Get new hostname from user.
	echo ""
	echo "Current hostname is $oldname.'
	getrepeat "Enter new hostname: "; newname="$resp"

	# Resolve hostname to IP address
	newip=`nslookup $newname | sed -n 's/Address: *\(.*\)$/\1/p' | tail -1`
	if [ -z "$newip" ]; then
		echo ""
		echo "I can't look up the address of $newname.  Either your"
		echo "current network configuration is faulty, or $newname"
		echo "does not have a DNS entry.  If you think you entered"
		echo "the name wrong, press ^C now and try again.  Otherwise,"
		echo "enter the IP address of $newname and we will continue."
		echo ""
		getrepeat "Enter new IP address: "; newip="$resp"
	else
		# Canonicalize entered name.
		full=`nslookup $newip | sed -n 's/Name: *\(.*\)$/\1/p'`
		if [ -n "$full" ]; then
			full=`echo $full | tr "[A-Z]" "[a-z]"`
			canon=`echo $full | sed 's/^\([^\.]*\)\..*/\1/'`
			newdomain=`echo $full | sed 's/^[^\.]*\.\(.*\)/\1/'`
			if [ "$newname" != "$canon" ]; then
				echo "$newname canonicalized to $canon."
				newname="$canon"
			fi
			echo "Domain is $newdomain."
		else
			echo "Can't canonicalize name, assuming domain mit.edu"
			newdomain=mit.edu
		fi
	fi

	# Get newmask, newbcast, newgate.
	getnetparams $newip

	# Regenerate config files
	newif="$oldif"
	newconfigs

	echo "The next time you reboot your machine, it will come up as"
	echo "$newname."
else
	# Ask for IP address
	getrepeat "Enter new IP address: "; newip="$resp"

	# Determine gateway, broadcast, etc.
	getnetparams $newip

	# Find all interfaces excluding slip, ppp, and loopback.
	intfs=`ifconfig -a | sed '/^    /d;/^lo/d;/^sl/d;/^ppp/d;s/^\([^:]*\):.*$/\1/'`
	if [ "intfs" = "" ]; then
	        echo "No network interfaces found."
	        exit 1
	fi

	# Find interface, flags capable of reaching gateway.
	echo -n "Testing network interfaces... "
	for i in $intfs ; do
		intf=$i

		case "$intf" in ep?*)
			flags1="-link2	link2"
			flags2=" link1	-link1"
			flags3=" link0	-link0"
		;; *)
			flags1="-link2 link2"
			flags2="-link1 link1"
			flags3="-link0 link0"
		;; esac

		for j in $flags1; do
		for k in $flags2; do
		for l in $flags3; do
			ifflags="$l $k $j"
			ifconfig $intf $ifaddr netmask $newmask broadcast \
				$newbcast $ifflags
			ping -nqc 1 -w 1 $newgate > /dev/null \
				&& { guess=1 ; break 4; }
		done; done; done
		ifconfig $intf down delete
		route -q delete $ifgateway
	done
	if [ "$guess" = "1" ]; then
		echo "success on interface $intf with flags: $ifflags"
		ifconfig $intf down delete
		echo "Enter interface [$intf]: "
		getresp "$intf"; newif="$resp"
		echo "Enter flags [$ifflags]: "
		getresp "$ifflags"; newflags="$resp"
	else
		echo "failed."
		# Nuke the following message when the bug is fixed.
		echo ""
		echo "(Note: this version of the NetBSD kernel has trouble"
		echo "switching 3C509 cards to the UTP interface.  If you are"
		echo "using a 3C509 card and UTP, and haven't run the"
		echo "Etherdisk menu program to configure your card for this"
		echo "interface, try doing that and starting over again.)"
		echo ""
		getrepeat "Enter interface: "; newif="$resp"
		echo -n "Enter flags: "; read newflags
	fi

	# Set up the network with given parameters.
	ifconfig $newif $newip netmask $newmask broadcast $newbcast \
	        -link0 -link1 -link2
	ifconfig $newif $newip netmask $newmask broadcast $newbcast $newflags
	ifconfig lo0 inet 127.0.0.1
	route add $newip 127.0.0.1
	route add default $newip

	full=`nslookup $newip | sed -n 's/Name: *\(.*\)$/\1/p'`
	if [ -n "$full" ]; then
		full=`echo $full | tr "[A-Z]" "[a-z]"`
		newname=`echo $full | sed 's/^\([^\.]*\)\..*/\1/'`
		newdomain=`echo $full | sed 's/^[^\.]*\.\(.*\)/\1/'`
		echo "New hostname is $newname."
		echo "New domain is is $newdomain."
	else
		echo ""
		echo "Cannot reverse resolve $newip."
		echo ""
		getrepeat "Enter hostname: "; newname="$resp"
		echo -n "Enter domain name [mit.edu]: "
		getresp mit.edu; newdomain="$resp"
	fi

	# Regenerate config files
	newconfigs
fi

