#!/bin/bash

# $Log: netconfig,v $
# Revision 1.3  2001/07/12 12:02:26  amn
# Changed from /etc/init.d/network to /etc/network/interfaces for Debian 2.2.
#
# Revision 1.2  1998/02/05 12:21:07  amn
# Clears /etc/resolv.conf when changing to "not connected to network".
#
# Revision 1.1  1997/10/21 19:12:13  root
# Initial revision
#

#set -x

Root=""
Base_Archive_Dir=""
readonly TARG=/tmp/
readonly Arch=`( cd /etc; echo architecture.*) | sed -e 's/architecture.//'`

Arch2=""

if [ "$Arch" = "m68k" ]; then
	Arch2=`cat /proc/hardware | ( read line; set -- $line; 
		if [ "$1" = "Model:" ]; then echo $2 ;return 0; fi)`
	case "$Arch2" in
	Atari|Amiga)
		;;
	*)
		echo "Your m68k architecture is not supported yet"
		read foo
		exit 0
	esac
fi

readonly Arch2

readonly revext=`( cd /etc; echo revision_ext.*) | sed -e 's/revision_ext.//'`

readonly bold='[1m'
readonly clear='[H[J'
readonly norm='[0;10m'

umask 022
BACKTITLE="FS/Debian/Linux Network Configuration"
export Swap
CreatedBootFloppy=
InstalledLILO=
readonly TempFile="/tmp/`echo $0|sed -e 's/^.*\///'`.$$"
readonly InstallationRootDevice="`block_device / 2>/dev/null`"

# Ignore interrupt in scripts, catch it in programs started by scripts.
trap true 2 3
# When the system halts, clear any menus that may be displayed on the first
# console.
trap "rm -f $TempFile; echo $clear; exit 0" 1 15
cd /

count_words ()
{
	echo "$#"
}

first ()
{
	echo "$1"
}

second ()
{
	echo "$2"
}

third ()
{
	echo "$3"
}

fourth ()
{
	echo "$4"
}

last ()
{
	eval echo $"$#"
}

not ()
{
	if [ "$1" -eq 0 -o "$1" = "" ]; then
		echo 1
	else
		echo 0
	fi
}

write_it_down ()
{
	local text="$1"
	local reply
	echo ""
	echo "$bold\
$text

You might want to write down the error messages before
you continue. Please press ENTER when you are ready.$norm"
	read reply
	return $?
}

match_find ()
{
	local files="$1"
	local name="$2"

	find "$1" | ( while read line; do
			    case $line in
			    */$2)
				    echo $line
				    ;;
			    esac 
		    done)
}

unreachable ()
{
	write_it_down "This line should never have been reached.
This indicates a bug in this program."
	exit 1
}

get_required_parameters () {
	host="`inputBox \
"Every Debian system has a name - even if it is not on a
network. This name is called its \"hostname\". You should now
specify a hostname for your new system.

If your system is going to be on a network, you should talk to the
administrator of the network before you choose a hostname. If not,
you may choose any name you like.

The hostname must not contain dots, and you must not append the domain
name here.

If you can't think of a hostname for your new system, you may press
<ENTER> to use the default hostname of \"new-fs\"." \
	"Please enter your Host name" new-fs`"
	if [ $? -ne 0 ]; then return 255; fi

	if [ -z "$host" ]; then
		host="new-fs";
	elif [ `count_words $host` -ne 1 ]; then
		infoBox "Problem" "The host name must be one word."
		return 1
	else
		local repl=`echo $host | sed -e 's/\./ /'`
		if [ `count_words $repl` -ne 1 ]; then
			infoBox "Problem" "The host name must not contain dots."
			return 1
		fi
	fi

	return 0
}

is_ip_number () {
	local i1 i2 i3 i4

	ipa=`echo $1 | sed 's/\./ /g'`

	i1=`first $ipa`
	i2=`second $ipa`
	i3=`third $ipa`
	i4=`fourth $ipa`

	for i in "$i1" "$i2" "$i3" "$i4"; do
		case $i in
		[0-9]|[0-9][0-9]|[0-9][0-9][0-9])
			;;
		*)
			return 1
		esac
		if [ $i -lt 0 -o $i -gt 255 ]; then
			return 1
		fi
	done
	return 0
}

get_net_parameters () {
	local I1 I2 I3 I4 R1 R2 R3 R4 N1 N2 N3 N4 S1 S2 S3 S4 ipa netm

	while true; do
		if [ "$domain" = none ]; then domain=""; fi
		domain="`inputBox \
"As with individual systems, every network has a name.
This name is called the domain name. Please enter your
Domain Name." "Please enter your Domain name" "$domain"`"
		if [ $? -ne 0 ]; then return 255; fi
		if [ -n "$domain" ]; then break; fi
	done

	hostname="$host.$domain"
	yesNoBox "The full name of your system is $hostname . Correct?" \
		"Confirm"
	if [ $? -ne 0 ]; then return 255; fi

	while true; do
		ipaddr="`inputBox \
			"What is the IP address of system \"$host\"?" \
			"Please Enter IP Address" \
			"$ipaddr"`"
		if [ $? -ne 0 ]; then return 255; fi
		if is_ip_number $ipaddr ; then break; fi
		msgBox "You entered an invalid IP address.
Ask your network administrator." "Error"
	done

	ipa=`echo $ipaddr | sed 's/\./ /g'`
	I1=`first $ipa`
	I2=`second $ipa`
	I3=`third $ipa`
	I4=`fourth $ipa`

	while true; do
		netmask="`inputBox "What is your netmask?" \
			"Please Enter Netmask" \
			"$netmask"`"
		if [ $? -ne 0 ]; then return 255; fi
		if is_ip_number $netmask ; then break; fi
		msgBox "You entered an invalid netmask.
Ask your network administrator." "Error"
	done

	netm=`echo $netmask | sed 's/\./ /g'`
	N1=`first $netm`
	N2=`second $netm`
	N3=`third $netm`
	N4=`fourth $netm`

	let "R1=$I1 & $N1"
	let "R2=$I2 & $N2"
	let "R3=$I3 & $N3"
	let "R4=$I4 & $N4"

	network="$R1.$R2.$R3.$R4"

	let "R1=~$N1 & 255 | $I1"
	let "R2=~$N2 & 255 | $I2"
	let "R3=~$N3 & 255 | $I3"
	let "R4=~$N4 & 255 | $I4"
	let "S1=$N1 & $I1"
	let "S2=$N2 & $I2"
	let "S3=$N3 & $I3"
	let "S4=$N4 & $I4"

	broadcast="`menu \
		 "What is your IP broadcast address?
Unless your network has very specific needs choose the first option." \
		 "Choose Broadcast Address"  \
		 "$R1.$R2.$R3.$R4" "Last bits set to one" \
		 "$S1.$S2.$S3.$S4" "Last bits set to zero" \
		 "another" "choose another broadcast address"`"
	if [ $? -ne 0 ]; then return 255; fi
	if [ "$broadcast" = another ]; then
		while true; do
			[ -n "$broadcast" ] || $broadcast=$R1.$R2.$R3.$R4
			broadcast="`inputBox \
				"What is your IP broadcast address?" \
				"Please Enter Broadcast Address"`"
			if [ $? -ne 0 ]; then return 255; fi
			if is_ip_number $broadcast ; then break; fi
			msgBox "You entered an invalid broadcast address.
Ask your network administrator." "Error"
		done
	fi

	yesNoBox \
"A gateway system is one that connects your network to
other networks such as your company wide-area net or the
Internet. Is there a gateway system on your network?" "Is there a Gateway?"
	if [ $? -eq 0 ]; then
		let "R1=$I1 & $N1"
	        let "R2=$I2 & $N2"
	        let "R3=$I3 & $N3"
		while true; do
			[ -n "$gateway" ] || gateway="$R1.$R2.$R3.1"
			gateway="`inputBox "What is your IP gateway address?" \
				"Please Enter Gateway Address" "$gateway"`"
			if [ $? -ne 0 ]; then return 255; fi
			if is_ip_number $gateway ; then break; fi
			msgBox "You entered an invalid gateway address.
Ask your network administrator." "Error"
		done
	fi

	local choice
	choice="`menu \
"Where is the DNS (Domain Name Service) server?" \
"Locate DNS Server" \
	"1" "System \"$host\" will be its own DNS server." \
	"2" "Another system will be the DNS server for system \"$host\"." \
	"3" "There will be no DNS server."`"
	if [ $? -ne 0 ]; then return 255; fi

	case $choice in
	1)
		nameserver="127.0.0.1"
		;;
	2)
# XXX verify nameserver ip addresses
		nameserver="`inputBox \
"Please enter the IP addresses (not host names) of up to 5 name
servers, separated by spaces. Do not use commas. The input line
will scroll to allow you to enter a long line. The servers will
be queried in the order in which you enter them." \
 "Please Enter Name Server Address" "$gateway"`"
		;;
	3|*)
		nameserver="none"
		;;
	esac

	yesNoBox \
"IP Address:           $ipaddr
Netmask:              $netmask
Network Address:      $network
Broadcast Address:    $broadcast
Gateway Address:      $gateway
Nameserver Addresses: $nameserver

Correct?" "Please Confirm"
	if [ $? -ne 0 ]; then return 1; fi

	return 0
}

write_network_configuration ()
{
	# This uses the following variables
	#
	# $broadcast
	# $domain
	# $gateway
	# $host
	# $hostname
	# $ipaddr
	# $nameserver
	# $netmask
	# $network
	# $netinterface

	for i in ${TARG}etc ${TARG}etc/network ; do
		[ -d $i ] || mkdir $i;
	done

	echo $host >${TARG}etc/hostname
	
	echo "localnet	$network" > ${TARG}etc/networks

	if [ "$domain" != "none" ]; then
		cat >${TARG}etc/resolv.conf << EOF
search $domain
EOF
		if [ "$nameserver" != "none" ]
		then
			for i in $nameserver; do
				echo "nameserver	$i" >> ${TARG}etc/resolv.conf
			done
		fi
	else
		cat >${TARG}etc/resolv.conf << EOF
# search <domain>
# nameserver <IP address of DNS>
EOF
	fi


	cat > ${TARG}etc/network/interfaces << EOF
# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)

# The loopback interface
iface lo inet loopback

EOF
	chmod 755 ${TARG}etc/network/interfaces

	if [ "$netinterface" != none ]; then
		cat >> ${TARG}etc/network/interfaces << EOF
# A real static interface board (typically Ethernet eth0).
iface $netinterface inet static
    address $ipaddr
    netmask $netmask
    broadcast $broadcast
EOF
            if [ "$gateway" != "" ]; then
		cat >> ${TARG}etc/network/interfaces << EOF
    gateway $gateway
EOF
            fi
	fi

	case "$netinterface" in
	eth0|tr0)
		cat > ${TARG}etc/hosts << EOF
127.0.0.1	localhost
$ipaddr		$hostname	$host
EOF
		;;
	none)
		cat > ${TARG}etc/hosts << EOF
127.0.0.1	$host	localhost
EOF
		;;
	slip|ppp|plip)
		cat > ${TARG}etc/hosts << EOF
127.0.0.1	localhost
$ipaddr		$hostname	$host
EOF
		msgBox \
"This installation program does not create a complete SLIP, PPP or
PLIP configuration. For SLIP and PPP you need additional packages like
dip or ppp. For PPP you have to create the configuration entries in
/etc/network/interfaces." "Information"
		;;
	pcmcia)
		cat > ${TARG}etc/hosts << EOF
127.0.0.1	localhost
$ipaddr		$hostname	$host
EOF
		for i in ${TARG}etc/pcmcia ; do
			[ -d $i ] || mkdir $i;
		done

		cat >> ${TARG}etc/pcmcia/network.opts << EOF

# Automatically configured during Debian installation
IPADDR="$ipaddr"
NETMASK="$netmask"
NETWORK="$network"
BROADCAST="$broadcast"
GATEWAY="$gateway"
DOMAIN="$domain"
EOF
		if [ "$nameserver" != "none" ]; then
			cat >> ${TARG}etc/pcmcia/network.opts << EOF
DNSSRVS="$nameserver"
EOF
		fi
		;;
	*)
		unreachable
	esac

	return 0
}

configure_network () {
	local broadcast gateway host hostname netinterface ipaddr nameserver \
	 netmask network

	while true; do
		get_required_parameters
		local status=$?
		if [ $status -eq 255 ]; then return 255; fi
		if [ $status -eq 0 ]; then break; fi
	done

	yesNoBox \
"Is your system connected to a network?

If you decide to use a network you must ask your 
network administrator for an IP address and the 
network parameters !" "Use a Network?"
	if [ $? -ne 0 ]; then
		broadcast="127.0.0.255"
		domain="none"
		gateway="none"
		hostname="$host"
		ipaddr="127.0.0.1"
		nameserver="none"
		netmask="255.255.255.0"
		network="127.0.0.0"
		netinterface=none
	else
		while true; do
			[ -n "$ipaddr" ] || ipaddr="192.168.1.1"
			[ -n "$netmask" ] || netmask="255.255.255.0"
			get_net_parameters
			local status=$?
			if [ $status -eq 255 ]; then return 255; fi
			if [ $status -eq 0 ]; then break; fi
		done

		netinterface="`menu \
			"Choose the type of your primary network interface.
If you have more than one network interface, choose the one that you will
need for installing Debian (via NFS or FTP)" \
			"Choose network interface" \
			"eth0" "Ethernet or Fast Ethernet" \
			"tr0" "Token Ring" \
			"ppp" "PPP" \
			"slip" "SLIP" \
			"plip" "PLIP" \
			"pcmcia" "PCMCIA Ethernet or Token Ring"`"
		if [ $? -ne 0 ]; then return 255 ;fi		

	fi

	write_network_configuration

#	if [ -x /sbin/ifconfig ]; then
#		${TARG}etc/network/interfaces
#		(cd /etc; rm -rf resolv.conf; \
#			ln -s ${TARG}etc/resolv.conf . )
#	fi

	return 0
}

# Shell interface to "dialog"
# Bruce Perens, November 1995
# This is free software under the GNU General Public License.

# Global options
#	The variable $BACKTITLE specifies the back title.
#	The variable $DIALOG_OPTIONS, initialized here to --clear, provides
#	options you want on the command line of each dialog invocation.
#	The variable $DIALOG_TEST can be set to "echo" to see the calls
#	to dialog without executing them.

DIALOG_OPTIONS=""

# Make any dialogue box, with default settings and backtitle from
# $BACKTITLE in the environment.
#
# dialog --type arg arg ...
#
dialogBox () {
	local type="$1"
	shift
	local title=""
	local backtitle=""

	local text="$1"
	shift

	if [ $# -ge 1 ]; then
		title="$1"
		shift
	fi

	if [ -n "$BACKTITLE" ]; then
		backtitle="$BACKTITLE"
	fi

	$DIALOG_TEST dialog $DIALOG_OPTIONS --title "$title" --backtitle \
	 "$backtitle" "$type" "$text" 0 0 "$@" 2>&1 1>/dev/tty
	local result=$?
	return $result
}

# Display a file.
#
# fileBox filename [title]
#
fileBox () {
	dialogBox --textbox "$1" "$2"
}

# textBox takes presents its standard input in a dialog box. This
# is useful for "here documents" and pipes.
#
# textBox [title]
#
textBox () {
	cat >$TempFile

	if [ $? -ne 0 ]; then
		echo "Can't make temporary file for dialog box." 1>&2
		return 255
	fi

	# Note that dialog needs stdin to be the terminal, so I redirect here.
	< /dev/tty dialogBox --textbox $TempFile "$1"
	local result=$?
	rm -f $TempFile
	return $result
}

msgBox () {
	dialogBox --msgbox "$1" "$2"
}

infoBox () {
	dialogBox --infobox "$1" "$2"
}

yesNoBox () {
	dialogBox --yesno "$1" "$2"
	return $?
}

inputBox () {
	dialogBox --inputbox "$1" "$2" "$3"
	return $?
}

# menu text title tag1 item1 ...
menu () {
	local text="$1"
	shift
	local title="$1"
	shift
	dialogBox --menu "$text" "$title" 0 "$@"
	return $?
}

# menu text title tag1 item1 status1 ...
checklist () {
	local text="$1"
	shift
	local title="$1"
	shift
	dialogBox --checklist "$text" "$title" 0 "$@"
	return $?
}

# menu text title tag1 item1 status1 ...
radiolist () {
	local text="$1"
	shift
	local title="$1"
	shift
	dialogBox --radiolist "$text" "$title" 0 "$@"
	return $?
}

find_root () {
	local root_device
	root_device="`mount | fgrep "on / " | cut -d' ' -f1`"
	if [ $? -ne 0 ]; then return 1; fi
	echo $root_device_device
	return 0
}

interactive_shell() {
	cd /
	echo \
"You are running \"sh\", a Bourne-shell clone. The root filesystem is a
RAM disk or floppy. The hard disk filesystems are mounted on \"${TARG}\".
Use the \"exit\" command to return to the installation menu.
"
	/bin/sh
	cd /
}

main_menu ()
{
	title="FS/Debian/Linux Network Settings"

		# Ignore interrupt in scripts, catch it in programs started by scripts.
		trap true 2 3
		infoBox \
"The installation program is determining the current state
of your system and the next installation step that should
be performed." \
		 "Please Wait"
		if [ $? -ne 0 ]; then return 255; fi

		local next_action=true
		local previous_action=true
		local previous1_action=true
		local alternate_action=true
		local alternate1_action=true

		local found_root
		found_root=`find_root`
		if [ $? -ne 0 ]; then
			echo 
			msgBox \
"I had trouble checking the choice of root device. I'll assume that
the root fs is still \""$Root"\". Press enter to continue." "Problem"
		elif [ -z "$Root" ]; then
			Root="$found_root"
		elif [ "$Root" != "$found_root" ]; then
			msgBox \
"The root device has changed unexpectedly, from \""$Root"\" to
\""$found_root"\". Press enter to continue." "Root File System has Changed"
			Root="$found_root"
		fi

		echo "menu \\" > $TempFile

		if [ ! -f ${TARG}etc/hostname ]; then
			# The network has not been configured.
			next_action=configure_network
			cat >> $TempFile << EOF
"You've configured the base system but not the network.
Please select \\"Next\\" to configure the network." "$title" \\
			"Next" "Configure the Network" \\
EOF
		fi
		
cat >>$TempFile << EOF
		" " " " \\
		"N" "Configure the Network" \\
		"S" "Execute a Shell"
EOF

		local action
###		action="`. $TempFile`"
		action="N"
		if [ $? -ne 0 -o -z "$action" ]; then continue; fi
		case $action in
		Next) $next_action;;
		Alternate) $alternate_action;;
		Alternate1) $alternate1_action;;
		Previous) $previous_action;;
		Previous1) $previous1_action;;
		N) configure_network;;
		S) interactive_shell;;
		*) unreachable ;;
		esac

}


# Start of main.

echo "























"

main_menu
