# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo/src/livecd-tools/net-setup,v 1.15 2005/04/13 13:19:58 wolf31o2 Exp $

#!/bin/bash

if [ `whoami` != "root" ]; then
	echo "net-setup: must be root to continue"
	exit 1
fi

if [ -z ${1} ]; then
	echo "net-setup: please specify a network interface"
	exit 1
fi

iface=${1}

[ ! -d /tmp/setup.opts ] && mkdir /tmp/setup.opts
cd /tmp/setup.opts

config_wireless() {
	cd /tmp/setup.opts
	dialog --title "SSID" --inputbox "Please enter your SSID, or leave blank for selecting the nearest open network" 20 50 2> ${iface}.SSID
	SSID=`cat ${iface}.SSID`
	if [ -n "${SSID}" ]; then
		dialog --title "WEP (Part 1)" --menu "Does your network use encryption?" 20 60 7 1 "Yes" 2 "No" 2> ${iface}.WEP
		WEP=`cat ${iface}.WEP`
		case ${WEP} in
			1)
				dialog --title "WEP (Part 2)" --menu "Are you entering your WEP key in HEX or ASCII?" 20 60 7 1 "HEX" 2 "ASCII" 2> ${iface}.WEPTYPE
				WEP_TYPE=`cat ${iface}.WEPTYPE`
				case ${WEP_TYPE} in
					1)
						dialog --title "WEP (Part 3)" --inputbox "Please enter your WEP key in the form of XXXX-XXXX-XX for 64-bit or XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XX for 128-bit" 20 50 2> ${iface}.WEPKEY
						WEP_KEY=`cat ${iface}.WEPKEY`
						if [ -n "${WEP_KEY}" -a -x /usr/sbin/iwconfig ]; then
							/usr/sbin/iwconfig ${iface} essid \"${SSID}\"
							/usr/sbin/iwconfig ${iface} key "${WEP_KEY}"
						fi
							;;
					2)
						dialog --title "WEP (Part 3)" --inputbox "Please enter your WEP key in ASCII form.  This should be 5 or 13 characters for either 64-bit or 128-bit encryption, repectively" 20 50 2> ${iface}.WEPKEY
						WEP_KEY=`cat ${iface}.WEPKEY`
						if [ -n "${WEP_KEY}" -a -x /usr/sbin/iwconfig ]; then
							/usr/sbin/iwconfig ${iface} essid \"${SSID}\"
							/usr/sbin/iwconfig ${iface} key "s:${WEP_KEY}"
						fi
						;;
				esac
				;;
			2)
				/usr/sbin/iwconfig ${iface} essid \"${SSID}\"
				/usr/sbin/iwconfig ${iface} key off
				;;
		esac
	fi
}

config_ip() {
	cd /tmp/setup.opts
	dialog --title "TCP/IP setup" --menu "You can use DHCP to automatically configure a network interface or you can specify an IP and related settings manually. Choose one option:" 20 60 7 1 "Use DHCP to auto-detect my network settings" 2 "Specify an IP address manually" 2> ${iface}.DHCP
	DHCP=`cat ${iface}.DHCP`
	case ${DHCP} in
		1)
			/sbin/dhcpcd -t 10 -h $(hostname) ${iface} &
			;;
		2)
			dialog --title "IP address" --inputbox "Please enter an IP address for ${iface}:" 20 50 "192.168.1.1" 2> ${iface}.IP
			IP=`cat ${iface}.IP`
			BC_TEMP="`echo $IP|cut -d . -f 1`.`echo $IP|cut -d . -f 2`.`echo $IP|cut -d . -f 3`.255"
			dialog --title "Broadcast address" --inputbox "Please enter a Broadcast address for ${iface}:" 20 50 "${BC_TEMP}" 2> ${iface}.BC
			BROADCAST=`cat ${iface}.BC`
			dialog --title "Network mask" --inputbox "Please enter a Network Mask for ${iface}:" 20 50 "255.255.255.0" 2> ${iface}.NM
			NETMASK=`cat ${iface}.NM`
			dialog --title "Gateway" --inputbox "Please enter a Gateway for ${iface} (hit enter for none:)" 20 50 2> ${iface}.GW
			GATEWAY=`cat ${iface}.GW`
			dialog --title "DNS server" --inputbox "Please enter a name server to use (hit enter for none:)" 20 50 2> ${iface}.DNS
			DNS=`cat ${iface}.DNS`
			/sbin/ifconfig ${iface} ${IP} broadcast ${BROADCAST} netmask ${NETMASK}
			if [ -n "${GATEWAY}" ]; then
				/sbin/route add default gw ${GATEWAY} dev ${iface} netmask 0.0.0.0 metric 1
			fi
			if [ -n "${DNS}" ]; then
				echo "nameserver ${DNS}" > /etc/resolv.conf
			else
				: > /etc/resolv.conf
			fi
			;;
	esac
}

dialog --title "Network setup" --menu "This script is designed to setup both wired and wireless network settings.  All questions below apply to the ${iface} interface only.  Choose one option:" 20 60 7 1 "My network is wireless" 2 "My network is wired" 2> ${iface}.WIRED_WIRELESS
WIRED_WIRELESS=`cat ${iface}.WIRED_WIRELESS`
case ${WIRED_WIRELESS} in
	1)
		config_wireless
		config_ip
		;;
	2)
		config_ip
		;;
	*)
		exit 0
		;;
esac


echo "Type \"ifconfig\" to make sure the interface was configured correctly."

# vim: ts=4
