#!/bin/sh
#
# Copyright (c) 1994 Christopher G. Demetriou
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#	This product includes software developed by Christopher G. Demetriou.
# 4. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# 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.
#
#	$Id: connect.sh,v 1.25 1995/12/25 07:23:11 ghudson Exp ghudson $
#	$Source: /mit/netbsd/dev/install/nfsinst/RCS/connect.sh,v $

# Our job is to set up the system's network, connect to an NFS server,
# set the variables $host, $domain, $intf, $ifaddr, $ifnetmask,
# $ifbcast, $ifflags, $ifgateway, $on_mitnet, and $kernel to the
# system's parameters, source /setup on the NFS server to store those
# set up the memory filesystem and store those variable values, and then
# run /install on the NFS server to install the system.

# Read in disk id variables ($kernel, $OSREV, $ATHENAREV).
. ./id

VERSION=1.0A
FSTAB=${FSTABDIR}/fstab

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

echo ""
proto_server=sipb-nfs.mit.edu
echo -n "Which NFS server? [$proto_server] "
read nfs_server
if [ "$nfs_server" = "" ]; then
	nfs_server=$proto_server
fi

proto_filesystem=/u2/lockers/netbsd/release/athena-${athenarev}/i386/install
echo -n "Which filesystem? [$proto_filesystem] "
read nfs_filesystem
if [ "$nfs_filesystem" = "" ]; then
	nfs_filesystem=$proto_filesystem
fi

# 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} /

# Set up the memory filesystem.
. /setup

# POOF! Okay, we're done here, hand off to our friend on the network...
echo "Continuing installation from ${nfs_server}:${nfs_filesystem}/install (/install)"
/install
