#!/bin/bash

# This script creates the enties needed for a static IP address
# This includes the IP address, netmask, gateway address, and
# the nameserver values.
#
# First prompt the user for some these values and save the results
# Richard Carlson
# rcarlson@internet2.edu
# November 1, 2006
#

# bring in NPT functions for later use
ENV="env -i PATH=/lib/init:/bin:/sbin:/usr/bin"
. /lib/init/nptoolkit-functions.sh

echo "Welcome to the NPToolkit server Static IP configuration program.  This"
echo "program will prompt you for the necessary information for your site."
echo ""

echo -n "Use Static IP addresses or DHCP [static] :"
read method
if test "$method" == "dhcp" || test "$method" == "d" ||
    test "$method" == "DHCP" || test "$method" == "D" ; then
	echo "."
	echo "Configuring system for DHCP addresses"
	mount_NPT_drives
	select_NPT_drive
	if [ "DRVS" == "" ] ; then
		echo "Information: No drive contain customized files"
	else
		for DRV in $DRVS ; do
			/bin/rm -f $DRV/NPTools/static.ip
			/bin/rm -f $DRV/NPTools/resolv.conf
			echo "Removed static IP config files from '$DRV'"
		done
	fi
	NETDEVICES="$(awk -F: '/eth.:|tr.:/{print $1}' /proc/net/dev 2>/dev/null)"
	for DEVICE in $NETDEVICES
	do
		echo -n "Network device $DEVICE detected, DHCP broadcasting for IP."
		trap 2 3 11
		ifconfig $DEVICE up >/dev/null 2>&1 ; pump -i $DEVICE >/dev/null 2>&1 &
		trap "" 2 3 11
		sleep 1
		echo " (Backgrounding)"
	done
	# OK, the interface is listening for DHCP requests and the static files have been removed
	# so just exit.
	exit
fi

echo -n "Enter your IP address [192.168.1.99]  : "
read yourIP
if test "$yourIP" == ""; then
	yourIP="192.168.1.99"
fi

echo -n "Enter your Netmask value [255.255.255.0]  : "
read yourMSK
if test "$yourMSK" == ""; then
	yourMSK="255.255.255.0"
fi

echo -n "Enter your gateway's IP address [192.168.1.1]  : "
read yourGW
if test "$yourGW" == ""; then
	yourGW="192.168.1.1"
fi

echo -n "Enter the network interface identifer [eth0]  : "
read yourIF
if test "$yourIF" == ""; then
	yourIF="eth0"
fi

cnt=0
until [ "$cnt" = "3" ]  ; do
    echo -n "Enter your nameserver's IP address (3 max, blank line to exit early)  : "
    read yourNS
    if test "$yourNS" == ""; then
	    break;
    fi
    echo "nameserver $yourNS" >> /etc/resolv.conf
    echo ""
    cnt=`expr $cnt + 1`
done
    
echo "IP_ADDR $yourIP" > /tmp/static.ip
echo "NETMASK $yourMSK" >> /tmp/static.ip
echo "GATEWAY $yourGW" >> /tmp/static.ip
echo "INTERFACE $yourIF" >> /tmp/static.ip

# All the questions have been asked, now it's time to put them
# to use.  Run sed a number of times and replace the variable names
# with the info the user supplied.

mount_NPT_drives
select_NPT_drive
if [ "$DRVS" == "" ] ; then
	echo "Error: No drive available for customized files"
else
	cnt=0
	for DRV in $DRVS ; do
		save_NPT_file $DRV /tmp/static.ip static.ip
		save_NPT_file $DRV /etc/resolv.conf resolv.conf
		if [ $cnt == 1 ] ; then
			echo "Warning: multiple drives are being configured"
		fi
		echo "Static IP customization files are being saved to '$DRV'"
		cnt=`expr $cnt + 1`
	done
fi

echo -n "Do you want to start this interface? [Yes] :"
read answer
if test "$answer" == ""; then
    answer="yes"
fi
if [[ "$answer" = "yes" || "$answer" = "Yes" || "$answer" = "y" || "$answer" = "Y" ]] ; then
    /etc/init.d/staticIP start
fi

/bin/rm -f /tmp/static.ip

