#!/bin/bash

INTFACEFILE="/etc/network/interfaces"
DHCPSRVFILE="/etc/dhcp3/dhcpd.conf"
DHCPDEVFILE="/etc/default/dhcp3-server"
FIREWALLFILE="/etc/init.d/firewall"
FIREWALLSTART="/etc/init.d/firewall-starter"
SOURCESFILE="/etc/apt/sources.list"
MESSAGES="/var/log/messages"

echo "Clearing all firewall rules"
iptables -F
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

echo "This install assumes you only have 2 NIC's. Any other configuration is too tough for my tiny brain"

echo "To make the next step a bit easier here is a listing of your nics( should show you all your devs and the drivers they are using )"
dmesg | egrep \([0-9a-Z]{2}:\){5}[0-9a-Z]{2}
ifdevs=`dmesg | egrep \([0-9a-Z]{2}:\){5}[0-9a-Z]{2} | egrep -o eth[0-9]`
for dev in $ifdevs
do
	echo "Is $dev your internal or external nic[int|ext]?"
	read intext

	if [ "$intext" == "int" ]
	then
		INTDEV=$dev
	else
		EXTDEV=$dev
	fi
done

echo "External NIC Set to: $EXTDEV"
echo "Internal NIC Set to: $INTDEV"

echo "Checking to see if you have internet connection...."
pingtest=`ping -W 1 -c 1 google.com | egrep -o [0-9]+%`

hasinternet="YES"
if [ "$pingtest" != "0%" ]
then
	hasinternet="NO"
	echo "Unable to contact Google ... You don't appear to have internet"

	echo "Would you like me to try and config DHCP on the external interface[y|n]?"
	read REPLACEINTERFACESFILE 

	if [ "$REPLACEINTERFACESFILE" == "y" ]
	then
		echo "Creating backup of $INTFACEFILE"
		cp $INTFACEFILE "${INTFACEFILE}.bk}"
		echo "auto lo" > $INTFACEFILE
		echo "iface lo inet loopback" >> $INTFACEFILE
		echo "" >> $INTFACEFILE
		echo "auto $EXTDEV" >> $INTFACEFILE
		echo "iface $EXTDEV inet dhcp" >> $INTFACEFILE
		echo "" >> $INTFACEFILE
		deadlink=`grep 'link is not ready' $MESSAGES | wc -l`
		/etc/init.d/networking restart
		deadlinkt=`grep 'link is not ready' $MESSAGES | wc -l`
		
		if [ $deadlink != $deadlinkt ]
		then
			echo "Looks like there might be a disconnected cable. Make sure all ethernet cables are connected"
			echo "Run this script again after you have connected all cables"
			exit 0
		fi

		echo "Checking to see if you have internet connection again...."
		pingtest=`ping -W 1 -c 1 google.com | egrep -o [0-9]+%`

		if [ "$pingtest" != "0%" ]
		then
			echo "Still no internet...uhh try and fix that?"
			exit 0
		else
			echo "Looks like your on the net ... continuing"
		fi
	fi
fi

##### You can assume the external interface is up and running now ######

echo "Removing CDROM from $SOURCESFILE"
sed -e "s/deb cdrom/#deb cdrom/g" $SOURCESFILE > "$SOURCESFILE.tmp"
cp $SOURCESFILE "$SOURCESFILE.bk"
mv "$SOURCESFILE.tmp" $SOURCESFILE

echo "Updating apt package list..."
apt-get update

echo "Fetching and installing dhcp server..."
apt-get -y install dhcp3-server

echo "Fetching/Installing VIM"
apt-get -y install vim

echo "Unpacking the router files and setup stuff"
cp routersetupfiles.tar /
cd /
tar xvf routersetupfiles.tar
rm routersetupfiles.tar

##### Unpacking this tar should put in place all the files needed for the router. 
#####  They just need to be configured now

##### Set the internal nic to serve dhcp #####
cp $DHCPDEVFILE "${DHCPDEVFILE}.bk"
echo "INTERFACES=\"$INTDEV\"" > $DHCPDEVFILE

##### Setup the dhcpd.conf file #####
EXTIP=`ifconfig $EXTDEV | egrep -o addr:\([0-9]{1\,3}[.]\){3}[0-9]{1\,3} | cut -b 6-`
EXTNET=`echo $EXTIP | awk -F'.' '{ printf "%d.%d.%d\n", $1, $2, $3 }'`

## Make sure we use different subnets for the internal network and the external network ##
if [ "$EXTNET" == "192.168.1" ]
then
	INTNET="192.168.0"
else
	INTNET="192.168.1"
fi

INTBCST="${INTNET}.255"
INTMSK="255.255.255.0"
INTIP="${INTNET}.1"

## Or simply there is nothing in the file for the internal interface ##
intdevmode=`egrep 'iface $INTDEV inet [static|dhcp]' $INTFACEFILE | awk -F' ' '{print $4}'`
if [ "$intdevmode" == "" ]
then
	echo "Entering in your internal device to the $INTFACEFILE file"
	sleep 2
	echo "auto $INTDEV" >> $INTFACEFILE
	echo "iface $INTDEV inet static" >> $INTFACEFILE
	echo "	address ${INTNET}.1" >> $INTFACEFILE
	echo "	network ${INTNET}.0" >> $INTFACEFILE
	echo "	netmask $INTMSK" >> $INTFACEFILE
	echo "	broadcast $INTBCST" >> $INTFACEFILE
	echo "" >> $INTFACEFILE
elif [ "$intdevmode" == "dhcp" ]
then
		echo "I'm going to need to modify your $INTFACEFILE"
		cp $INTFACEFILE "${INTFACEFILE}.bk"
		sed "s/iface $INTDEV inet dhcp/iface $INTDEV inet static\n\taddress $INTIP\n\tnetwork ${INTNET}.0\n\tnetmask $INTMSK\n\tbroadcast $INTBCST\n/" $INTFACEFILE > "${INTFACEFILE}.tmp"
		mv "${INTFACEFILE}.tmp" $INTFACEFILE
		
		echo "Restarting network to apply changes"
		/etc/init.d/networking restart
else
	echo "Your internal device is already set to static. "
	echo "I'm not going to modify your $INTFACEFILE"
fi

/etc/init.d/networking restart

#### At this point you can assume that both the internal and external NICS are setup ####

echo "Setting up your dhcp server file[$DHCPSRVFILE]"
sleep 2
sed -e "s/<INTNET>/$INTNET/g" -e "s/<SUBNETMASK>/$INTMSK/" -e "s/<INTIP>/$INTIP/" $DHCPSRVFILE > "$DHCPSRVFILE.tmp"
cp $DHCPSRVFILE "$DHCPSRVFILE.bk"
mv "$DHCPSRVFILE.tmp" $DHCPSRVFILE
/etc/init.d/dhcp3-server restart

## Did the server restart successfully
if [ $? -eq 0 ]
then
	echo "DHCP Server is up and running"
else
	echo "DHCP Server failed to start. Check the /var/log/syslog for details"
fi
sleep 2

## Edit the /etc/init.d/firewall-starter script ##
echo "Configuring the firewall startup scripts..."
sleep 1
sed -e "s/<INTIF>/$INTDEV/g" -e "s/<EXTIF>/$EXTDEV/g" -e "s/<INTIP>/$INTIP/g" -e "s/<INTNET>/$INTNET/g" $FIREWALLFILE  > "$FIREWALLFILE.tmp"
cp $FIREWALLFILE "$FIREWALLFILE.bk"
mv "$FIREWALLFILE.tmp" $FIREWALLFILE
chmod 755 $FIREWALLFILE

$FIREWALLSTART start

