#!/bin/bash
#
# fermiinstall 1.2
# description: 
#	Fermi-install is a program that works with the fermiinstall 
#	  rpm.  This script will configure your lilo.conf to use the
#	  floppy image installed with the rpm.  It will then reboot
#	  your machine and you will be able to install Fermi Linux 
#	  over the network, without having to use a floppy or CD.
#	  It also has allowances to use a kickstart script which is
#	  able to be configured ahead of time if wanted.
#
#**********************************************
# variables
#**********************************************
FORCE="no"
KICK="no"
SERVER="linux"
GENERIC="yes"
IP="131.225.255.255"
GATEWAY="131.225.255.200"
NETMASK="255.255.255.0"
NAMESERVER="131.225.8.120"
RELEASE="612"
SOURCE="/root/fermiinstall"
KICKFILE="$SOURCE/ks.generic"
DRIVE="$(df /boot | grep /dev | cut -d' ' -f1 | cut -d'/' -f3)"

#**********************************************
# print out the help for fermi-install
#**********************************************

printHelp(){
	echo " "
	echo "fermi-install [-h] [-f] [-k]"
	echo "   [-i <IP Address>] "
	echo "   [-g <gateway>] "
	echo "   [-m <netmask>]"
	echo "   [-n <nameserver>]"
	echo "   [-s <install server>] "
	echo "   [-r <release>]"
	echo "   [-F <file>]"
	echo " "
	echo " -h Help  Display this help file"
	echo " "
	echo " **warning** *use the following two options with caution*"
	echo " -f Force     Non-interactive, doesnt ask for verifcation"
	echo " -k [file]    Use kickstart file"
	echo " **warning** *use the above two options with caution* "
	echo " "
	echo "The options below only work if the -k option is used "
	echo " -i IP         IP Address for you machine - default DHCP"
	echo " -g gateway    Gateway IP for machine - default (131.225.255.200)"
	echo " -m netmask    netmask for machine    - default (255.255.255.0)"
	echo " -n nameserver nameserver for machine - default (131.225.8.120)"
	echo " -s server     install server         - default (linux)"
	echo " -r release    Fermi Linux release    - default (612)"
	echo " -F file       modified kickstart file- default (/root/fermiinstall/ks.generic)"
	echo " "
}

#**********************************************
# build kickstart file
#**********************************************

buildKickstart(){
	echo "lang en_US" > $SOURCE/ks.cfg
	if [ "$GENERIC" = "yes" ] ; then
		echo "network --bootproto dhcp" >> $SOURCE/ks.cfg
	else
		echo "network --bootproto static --ip $IP --netmask $NETMASK --gateway $GATEWAY --nameserver $NAMESERVER" >> $SOURCE/ks.cfg
	fi
	echo "nfs --server $SERVER.fnal.gov --dir /export/linux/$RELEASE/i386" >> $SOURCE/ks.cfg
	cat $KICKFILE >> $SOURCE/ks.cfg
}

#**********************************************
# move the floppy files to where they belong
#**********************************************

moveFiles(){
	cp -f $SOURCE/vmlinuz /boot/vmlinuz-install
	cp -f $SOURCE/initrd.img /boot/initrd-install.img
	if [ "$KICK" = "yes" ] ; then
		cp -f $SOURCE/ks.cfg /boot/ks.cfg
	fi
}

#**********************************************
# Edit lilo.conf
#**********************************************

editLilo(){
	cp /etc/lilo.conf /etc/lilo.conf.install.save
	
	if [ "$(grep 'image=/boot/vmlinuz-install' /etc/lilo.conf)" = "" ] ; then
		appLilo
	else
		csplit -f /tmp/lilo /etc/lilo.conf /"image=/boot/vmlinuz-install"/
		cp -f /tmp/lilo00 /etc/lilo.conf
		appLilo
		if [ "$(grep 'boot/ks.cfg' /tmp/lilo01)" = "" ] ; then
			csplit -f /tmp/lilo2  /tmp/lilo01 /"network text"/+1
			cat /tmp/lilo201 >> /etc/lilo.conf
		else
			csplit -f /tmp/lilo2  /tmp/lilo01 /"boot/ks.cfg"/+1
			cat /tmp/lilo201 >> /etc/lilo.conf
		fi
	fi
}

#**********************************************
# append to lilo.conf
#**********************************************

appLilo(){
		echo "		" >> /etc/lilo.conf
		echo "image=/boot/vmlinuz-install" >> /etc/lilo.conf
		echo "        label=install" >> /etc/lilo.conf
		echo "        initrd=/boot/initrd-install.img" >> /etc/lilo.conf
		if [ "$KICK" = "yes" ] ; then
			echo "        append=\"ks=hd:$DRIVE/boot/ks.cfg\"" >> /etc/lilo.conf
		else
			echo "        append=\"network text\"" >> /etc/lilo.conf
		fi
		echo "		" >> /etc/lilo.conf
}

#**********************************************
# run lilo - set it so that it will install next reboot
#**********************************************

runLilo(){
	/sbin/lilo
	if [ "$FORCE" = "no" ] ; then
		echo "Do you want the Fermi Install to start next time you reboot? [ y | N ]"
		read answer
		case $answer in y | Y | yes | Yes )
			/sbin/lilo -R install
		;;
		* )
			echo "When you are ready to install, type "
			echo "/sbin/lilo -R install ; /sbin/reboot"
			exit 5;
		;;
		esac
	else
		/sbin/lilo -R install
	fi	
}

#**********************************************
# Reboot the machine
#**********************************************

runReboot(){
	if [ "$FORCE" = "no" ] ; then
		echo "Do you want to reboot right now?[ y | N ]"
		echo "This will start the installtion on your machine."
		read answer
		case $answer in y | Y | yes | Yes )
			echo "Rebooting and starting install."
		;;
		* )
			echo "When you are ready to install, type /sbin/reboot"
			exit 5;
		;;
		esac
	fi	
	/sbin/reboot
}

while test $# != 0
do
	case $1 in
		-i) test $# -lt 2 && { printHelp ; exit 1 ; }
			IP="$2" ; GENERIC="no" ;shift 2 ;;
		-m)  test $# -lt 2 && { printHelp ; exit 1 ; }
			NETMASK="$2" ; GENERIC="no" ;shift 2 ;;
		-g)  test $# -lt 2 && { printHelp ; exit 1 ; }
			GATEWAY="$2" ; GENERIC="no" ;shift 2 ;;
		-n)  test $# -lt 2 && { printHelp ; exit 1 ; }
			NAMESERVER="$2" ; GENERIC="no" ;shift 2 ;;
		-s)  test $# -lt 2 && { printHelp ; exit 1 ; }
			SERVER="$2" ;shift 2 ;;
		-r)  test $# -lt 2 && { printHelp ; exit 1 ; }
			RELEASE="$2" ;shift 2 ;;
		-F)  test $# -lt 2 && { printHelp ; exit 1 ; }
			KICKFILE="$2" ;shift 2 ;;
		-k) KICK="yes" ;shift 1 ;;
		-f) FORCE="yes" ;shift 1 ;;
		-h | -help | --help | --h) printHelp ; exit 0 ;;
		*) printHelp ; break
	esac
done
	
if [ "$FORCE" = "no" ] ; then
	echo " "
	echo "You are about to configure you system so that it installs Fermi Linux $RELEASE"
	if [ "$KICK" = "yes" ] ; then
		echo "using a kickstart script.  Your computer will be set up in the following way."
		echo " "
		if [ "$GENERIC" = "yes" ] ; then
			echo " IP Setup:       DHCP "		
		else
			echo " IP Address:     $IP "		
			echo " netmask:        $NETMASK "		
			echo " gateway:        $GATEWAY "		
			echo " nameserver:     $NAMESERVER "		
		fi
		echo " Install Server: $SERVER.fnal.gov"
		echo " Fermi Linux:    $RELEASE"
		echo " "
	fi
	echo "Do you want to continue? [ y | N ]"
	read answer
	case $answer in y | Y | yes | Yes )
		echo "Proceding with reconfiguration."
	;;
	* )
		echo "Very Well (fermi-install -h can give you any info you need)"
		exit 2;
	;;
	esac
fi	

if [ "$KICK" = "yes" ] ; then
	buildKickstart
fi

moveFiles
editLilo
runLilo
runReboot

exit 0
	
