#!/bin/sh
# $Id: connect.sh,v 1.26 1996/12/04 06:40:43 ghudson Exp $

# Copyright (c) 1995 by the Student Information Processing Board
#       of the Massachusetts Institute of Technology
#  
# Permission to use, copy, modify, and distribute this software
# and its documentation for any purpose and without fee is
# hereby granted, provided that the above copyright notice
# appear in all copies and that both that copyright notice and
# this permission notice appear in supporting documentation,
# and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
# used in advertising or publicity pertaining to distribution
# of the software without specific, written prior permission.
# M.I.T. and the M.I.T. S.I.P.B. make no representations about
# the suitability of this software for any purpose.  It is
# provided "as is" without express or implied warranty.

# /tmp/vars outputs:
#	kernel		path to kernel to install, usually /srvd/netbsd
#	nfs_server	NFS server name
#	nfs_filesystem	Path to NFS install directory on server
#	ftpsite		FTP site for os tarfile
#	ftpdir		FTP directory for os tarfile
#	systemdir	system directory in AFS with packs
#	osrev		revision of the OS we're installing
#	athenarev	revision of the Athena software we're installing
#	host		unqualified hostname of this system
#	domain		domain name of this system
#	intf		network interface device
#	ifaddr		IP address of network interface
#	ifnetmask	netmask of network interface
#	ifbcast		broadcast address of network interface
#	ifflags		flags of network interface
#	ifgateway	gateway address of subnet

# Read in disk constants ($systemdir, $osrev, $athenarev).
. /vars

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

# 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.  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

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

# 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"

# Set default netmask, broadcast address, and gateway, based on the ip address.
# This is specific to MITnet.
. .mitnetconf

# Rely on the assumption that .mitnefconf will set proto_bcast on any match.
if [ -n "$proto_bcast" ]; then
	if [ -z "$proto_netmask" ]; then proto_netmask=0xffff0000; fi
	if [ -z "$proto_gateway" ]; then proto_gateway="${ifA}.${ifB}.0.1"; fi
	ifnetmask="$proto_netmask"
	ifbcast="$proto_bcast"
	ifgateway="$proto_gateway"
	on_mitnet=yes
else
	echo -n "What is your netmask? [0xffffff00]"
	getresp 0xffffff00; ifnetmask="$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 "What is your broadcast address? [$proto_bcast] "
	getresp "$proto_bcast"; ifbcast="$resp"
	echo -n "What is your gateway address? [$proto_gateway] "
	getresp "$proto_gateway"; ifgateway="$resp"
	on_mitnet=no
fi

echo -n "Testing network interfaces... "

# 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

	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 $ifnetmask broadcast $ifbcast \
			$ifflags
		ping -nqc 1 -w 1 $ifgateway > /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"
	intfdef="[$intf] "
	flagsdef="[$ifflags] "
	ifconfig $intf down delete
else
	intf=""
	ifflags=""
	echo "failed."
	# Nuke the following message when the bug is fixed.
	echo ""
	echo "(Note: this version of the NetBSD kernel has trouble switching"
	echo "3C509 cards to the UTP interface.  If you are using a 3C509 card"
	echo "and UTP, and haven't run the Etherdisk menu program to configure"
	echo "your card for this interface, try doing that and starting over"
	echo "again.)"
	echo ""
fi

resp=""
while [ "$resp" = "" ]; do
	echo -n "Use what ethernet interface? $intfdef"
	getresp "$intf"
done
intf="$resp"

echo -n "Use what flags? $flagsdef"
getresp "$ifflags"
ifflags="$resp"

# Reset flags.
ifconfig $intf $ifaddr netmask $ifnetmask broadcast $ifbcast \
	-link0 -link1 -link2
ifconfig $intf $ifaddr netmask $ifnetmask broadcast $ifbcast $ifflags
ifconfig lo0 inet 127.0.0.1
route add $ifaddr 127.0.0.1
route add default $ifgateway

# Find and set hostname, for NFS servers which check hostnames.
fullname=`/rresolve $ifaddr`
if [ "$fullname" = "" ]; then
	while [ "$fullname" = "" ]; do
		echo -n "What is this machine's fully qualified hostname?"
		echo -n " (e.g. foo.mit.edu)? "
		read fullname
	done
else
	echo "The hostname for this machine is ${fullname}."
fi
host=`echo $fullname | sed -e 's/^\([^\.]*\)\.\(.*\)$/\1/'`
domain=`echo $fullname | sed -e 's/^\([^\.]*\)\.\(.*\)$/\2/'`
hostname $host

mount_nfs -i ${nfs_server}:${nfs_filesystem} /nfs

if [ ! -r /nfs/startafs ]; then
	echo "Failed to mount NFS server.  Send mail to netbsd-help@mit.edu"
	echo "for assistance, or call at 253-7788.  Type 'halt' to halt and"
	echo "reboot your machine."
	exit 1
fi
echo "${nfs_server}:${nfs_filesystem} mounted on /nfs"

# Create temporary filesystem.
mount_mfs -s 2048 swap /tmp

# /os and /srvd are symlinks to /tmp/os and /tmp/srvd; set them up.
ln -s ${systemdir}/os-${osrev} /tmp/os
ln -s ${systemdir}/srvd-${athenarev} /tmp/srvd

# Write initial variable definitions to /tmp/vars.
cp /vars /tmp/vars
cat >> /tmp/vars << EOM
host="$host"
domain="$domain"
intf="$intf"
ifaddr="$ifaddr"
ifnetmask="$ifnetmask"
ifbcast="$ifbcast"
ifflags="$ifflags"
ifgateway="$ifgateway"
EOM

echo "Continuing installation from /nfs/startafs"
exec /nfs/startafs
