#!/bin/sh
# $Id: connect.sh,v 1.11 1998/09/21 16:50:17 nathanw Exp $
# Script to get the network up and running from the ramdisk install.

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


# Run ifconfig -a and get rid of everything but the name of a real interface
# device. 
intfs=`ifconfig -l | sed -e 's/lo.//g;s/sl.//g;s/ppp.//g;s/strip.//g;s/tun.//g'`

if [ "intfs" = "" ]; then
	echo "No network interfaces found.  If you have a network connection,"
	echo "it wasn't recognized by the kernel.  If you don't have a network"
	echo "connection, you should be using a different installation disk."
	echo "Type 'halt' now to halt your machine."
	exit 1
fi

# Reverse the order of the interfaces so that for cards which detect
# as PnP and ISA, we try the PnP one first. We still wedge the install
# if we get around to trying the ISA-probed version, but this fixes
# the common case of a single 3c509B card.
newints=""
for f in $intfs; do
	newints="$f $newints"
done
intfs="$newints"

echo ""
ifaddr=
while [ "$ifaddr" = "" ]; do
	echo -n "What is this machine's IP address? "
	read ifaddr
done

params=`/etc/athena/netparams -e "$ifaddr"`
if [ $? -eq 1 ]; then
	echo "Our database doesn't have the network configuration information \
		for that address"
	echo "You will need to enter the information manually."
	echo "Contact your network administrator if you do not know \
		these parameters."

	# Perform IFS magic to seperate IP address into four pieces.
	OLDIFS="$IFS"
	IFS=.
	set -- $ifaddr
	ifA=$1 ifB=$2 ifC=$3 ifD=$4
	IFS="$OLDIFS"

	echo -n "What is your netmask? [0xffffff00] "
	getresp 0xffffff00; ifnetmask="$resp"
	if [ "$ifnetmask" = "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 "What is your broadcast address? [$proto_bcast] "
	getresp "$proto_bcast"; ifbroadcast="$resp"
	echo -n "What is your gateway address? [$proto_gateway] "
	getresp "$proto_gateway"; ifgateway="$resp"
	on_mitnet=no
else
	set -- $params
	ifnetmask=$1
	ifbroadcast=$3
	ifgateway=$4
	on_mitnet=yes
fi

echo "Netmask: $ifnetmask"
echo "Broadcast address: $ifbroadcast"
echo "Default gateway: $ifgateway"


echo -n "Testing network interfaces... "

ifconfig lo0 inet 127.0.0.1

# Loop through all above interfaces, attempting to ping the gateway address
# on each of them. This will fail miserably if we didn't figure out the
# right gateway...

for i in $intfs ; do
	intf=$i

	# Media types and options.
	medias=`ifconfig -m $intf | sed -n -e 's/.*supported media: //p'`

	if [ "$medias" = manual ]; then
		# No media selection mechanisim. 
		m=
		ifconfig $intf $ifaddr netmask $ifnetmask \
			broadcast $ifbroadcast
		ping -nqc 1 $ifgateway > /dev/null
		pingstat=$?
       		ifconfig $intf down delete
		if [ $pingstat = 0 ]; then
			guess=1
			break 2
		fi
	else		
		for m in $medias; do
			ifconfig $intf $ifaddr netmask $ifnetmask \
				broadcast $ifbroadcast media $m
			ping -nqc 1 $ifgateway > /dev/null
			pingstat=$?
			ifconfig $intf down delete
			if [ $pingstat = 0 ]; then
				guess=1
				break 2
			fi
		done
	fi
	route -q delete $ifgateway
done

if [ "$guess" = "1" ]; then
	if [ "$m" = "" ]; then
		echo "success on interface $intf (default media)"
	else
		echo "success on interface $intf with media $m"
	fi
	intfdef="[$intf] "
	mediadef="[$m] "
else
	intf=""
	ifflags=""
	echo "failed."
fi

resp="xxx"
while ! (expr " $intfs" : ".* $resp" > /dev/null); do
	echo "Ethernet interfaces: $intfs"
	echo -n "Use what ethernet interface? $intfdef"
	getresp "$intf"
done
intf="$resp"

medias=`ifconfig -m $intf | sed -n -e 's/.*supported media: //p'`
if [ "$medias" = manual ]; then
	media=""
else 
	echo "Media types avaliable for $intf: $medias"
	echo -n "Use what media? $mediadef"
	getresp "$m"
	media="$resp"
fi
# Reset

if [ "$m" = "" ]; then
	ifconfig $intf $ifaddr netmask $ifnetmask broadcast $ifbroadcast 
else
	ifconfig $intf $ifaddr netmask $ifnetmask broadcast $ifbroadcast \
		media $m
fi
route add default $ifgateway

# Write some variables to /tmp/vars
cat >> /tmp/vars << EOM
intf="$intf"
ifaddr="$ifaddr"
ifnetmask="$ifnetmask"
ifbroadcast="$ifbroadcast"
ifgateway="$ifgateway"
ifmedia="$media"
EOM

exit 0
