#!/bin/sh

# Management Console for Gentoo Linux
#
# Christoph Lameter, <christoph@lameter.com> July 15, 2002
#
# (C) 2002 The TelemetryBox Corporation (http://telemetrybox.biz). 

TMP=/tmp/console-$$
if [ ! -d $TMP ]; then
  mkdir $TMP
fi

cat >$TMP/help.txt <<EOF
TelemetryBox/Gentoo 1.3b Help
-----------------------------

Right now you are in the TelemetryBox Console. The console allows you to
administer the box on an operating system level if physical access is
possible and also allows an easy set up of the network configuration which
might be important if something goes wrong.

The TelemetryBox has the ability to bring up an X windows Graphical
Environment using the KDE desktop. The desktop is intended for operating the
box as a workstation. Note that the desktop requires significant computing
resources. It is advisable to only use the desktop on machines that are not
under heavy load and/or that are not mission critical.

The homepage of the TelemetryBox distributrion is http://telemetrybox.biz.

Christoph Lameter <christoph@lameter.com> July 15,2002
EOF

gettext () {
  dialog --title "Static IP Configuration for eth0" --inputbox "$1" 10 75 "$2" 2>$TMP/out || exit 1
}

staticip () {
  gettext "Name of host (do not include domain name)" $(</etc/hostname)
  hostname=$(<$TMP/out)
  gettext "Domain" $(hostname -d)
  domain=$(<$TMP/out)
  gettext "IP Address" $(hostname -i)
  address=$(<$TMP/out)
  gettext "Gateway Address" "${address%.*}.1"
  gateway=$(<$TMP/out) 
  dns=$(grep '^nameserver' /etc/resolv.conf|awk 'ORS=" " { print $2; }')
  gettext "List of DNS Servers separated by space" "$dns"
  dns=$(<$TMP/out)
  gettext "Netmask" 255.255.255.0
  netmask=$(<$TMP/out)
  

  dialog --title "Confirmation of IP Parameters" --yesno "The following IP characteristics will be set up:
Hostname    : $hostname
Domain      : $domain
IP Address  : $address
Gateway     : $gateway
DNS Servers : $dns
Netmask     : $netmask
" 15 60 || exit 1

  cat >/etc/conf.d/net <<EOF
iface_eth0="$address netmask $netmask"
gateway="eth0/$gateway"
EOF

  cat >/etc/resolv.conf <<EOF
domain $domain
search $domain
EOF
  for i in $dns; do
    echo "nameserver $i" >>/etc/resolv.conf
  done
  cat >/etc/hosts <<EOF
127.0.0.1	localhost
$address	$hostname.$domain $hostname
EOF
  echo "$hostname" >/etc/hostname
  hostname $hostname
  /etc/init.d/net.eth0 stop
  sleep 1
  /etc/init.d/net.eth0 start
}

dynamicip() {
  cat >/etc/conf.d/net <<EOF
iface_eth0="dhcp"
EOF
  cat >/etc/hosts <<EOF
127.0.0.1	gentoo.telemetrybox.org localhost gentoo
EOF
  echo testbox >>/etc/hostname
  hostname gentoo
  /etc/init.d/net.eth0 stop
  sleep 2
  /etc/init.d/net.eth0 start
}

install() {
	read X
	dialog --title "Installing" --infobox "$X" 5 60
	while read X; do
		emerge $X </dev/tty || true
	done
}

if [ -d /cdroot ]; then 
        INSTSTR="deploy Installation"
else
	INSTSTR="add Add-ons"
fi

dialog --nocancel --backtitle "The TelemetryBox Corporation presents Gentoo/Linux 1.3b" \
        --title "Gentoo V1.3b Console" \
	--menu "A variety of functions can be performed from the \
Gentoo Console. Gentoo supports virtual screens. The first \
one is used for the console. Others can be used to log in. \
Press ALT-F2 to reach a login prompt. Press ALT-F1 or CRTL-ALT-F1 to \
return to the console. ALT-F7 is the GUI console (if active)" 18 72 7 \
        help "Some Documentation and Help on how to do things" \
	$INSTSTR \
	upgrade	"Upgrade the Software (requires Internet Connectivity)" \
	cfiles  "Upgrade configuration files" \
	xon	"X Graphical User Interface ON" \
	xoff	"X Graphical User Interface OFF" \
	network "Set up static IP address for eth0" \
	showip	"Show IP configuration" \
	tz	"Set Time Zone" \
	xconfig	"Configure X Windows" \
	dhcp	"Revert to DHCP IP configuration for eth0" \
	shutdown "Shutdown" \
	reboot	"Reboot the system after shutdown" \
	top	"Show Running Processes and Operating System Load" \
	drivers	"Device Driver Information" \
	admin	"Go into maintenance (Single User) mode" \
	exit	"Exit Console" \
	    2>$TMP/function

case $(<$TMP/function) in
	help)	dialog --title "TelemetryBox/Gentoo Help" --textbox $TMP/help.txt 23 78;;
	tz)		dialog --title "TimeZone Configuration" --fselect /usr/share/zoneinfo/ 23 78 2>$TMP/tz || exec $0
			ln -sf `cat $TMP/tz` /etc/localtime
			;;
	exit)		rm -rf $TMP;exit 0;;
	shutdown)	rm -rf $TMP;shutdown -h now;;
	reboot)		rm -rf $TMP;shutdown -r now;;
	admin)		rm -rf $TMP;telinit s;;
	xon)		/etc/init.d/xdm start;;
	xoff)		/etc/init.d/xdm stop;;
	upgrade)	emerge rsync;emerge -u world;;
        cfiles)		etc-update;;
        xconfig)        X -configure;mv /root/XF86Config.new /etc/X11/XF86Config-4;;
	top)		top;;
	drivers)	
	 echo -e "List of Loaded Drivers:\n---------------------------" >$TMP/report
	 cat /proc/modules >>$TMP/report
	 echo -e "\nPCI device list\n---------------" >>$TMP/report
       	 lspci >>$TMP/report
	 dialog --title "Device Driver Information" --textbox $TMP/report 23 76
	 rm $TMP/report
	;;
	showip)		echo -e "ifconfig output\n---------------"	>$TMP/report
		ifconfig eth0 >>$TMP/report
		echo -e "\n/etc/resolv.conf\n----------------" >>$TMP/report
		cat /etc/resolv.conf >>$TMP/report
		echo -e "\n/etc/hosts\n-----------" >>$TMP/report
		cat /etc/hosts >>$TMP/report
		echo -e "\nRouting\n-----------" >>$TMP/report
		route -n >>$TMP/report
	        dialog --title "IP Configuration" --textbox $TMP/report 23 78
	 	rm $TMP/report
	;;

	network)	staticip;;
	dhcp)		dynamicip;;
        deploy)	deploy;;
	add)
# Should find all uninstalled components and offer a hierachical list 
	  dialog --title "Adding of additional components" --menu \
	     "The following additional software components can be installed.\
These will be downloaded from the Gentoo network." 18 72 7 \
	  ldap "`head -1 /usr/share/tbox/tasks/ldap`" \
	  network "`head -1 /usr/share/tbox/tasks/network`" \
	  webmin "`head -1 /usr/share/tbox/tasks/webmin`" \
 	  exit "Back to the Console"  2>$TMP/module || exec $0

	MODULE=$(<$TMP/module)
	if [ "$MODULE" = "exit" ]; then
		exec $0
	fi
	emerge rsync
	install </usr/share/tbox/tasks/$MODULE
	dialog --msgbox "Installation of $MODULE finished" 5 60
esac

exec $0
