#!/bin/sh
# TAMU main installation script
# Written by David Safford
# Anybody (including peter :-) can use this however they see fit.
# The neat part of this method is the use of /etc/system_config
# and doconfig to tailor and complete the installation.

PATH=/bin:/sbin:/usr/bin:.
export PATH

ask() {
	until [ $# = '0' ];
	do
		echo -n "Install package $1 size $2? (y/n) "
		read yn
		if [ "$yn" = 'y' ]; then
			PKGS="$PKGS $1.pkg"
		fi
		shift 2
	done
}

PKGS="--exclude TOC"
NEXT=1
CHGDISK=n

while [ 0 ]; do
	echo "Linux Maintenance Diskette"
	echo " "
	echo '1 - Install a full distribution set'
	echo '2 - Exit to linux shell'
	echo " "
	echo -n "enter choice ($NEXT): "
	read ans;
	if [ ! "$ans" ]; then
		ans=$NEXT
	fi
	if [ $ans = 1 ]; then
		echo " "
		while [ 0 ]; do 
			echo "What floppy drive will be used for installation? "
			echo '1)  Drive A: 5 1/4 inch'
			echo '2)  Drive A: 3 1/2 inch'
			echo '3)  Drive B: 5 1/4 inch'
			echo '4)  Drive B: 3 1/2 inch'
			echo " "
			echo -n "Enter choice (2): "
			read answer; 
			if [ ! "$answer" ]; then
				answer=2
			fi
			if [ "$answer" =  1 ]; then
				INSTDEV=/dev/fd0h1200;
				INSTSIZE=1200;
			elif [ "$answer" =  2 ]; then
				INSTDEV=/dev/fd0H1440;
				INSTSIZE=1440;
			elif [ "$answer" =  3 ]; then
				INSTDEV=/dev/fd1h1200;
				INSTSIZE=1200;
			elif [ "$answer" =  4 ]; then
				INSTDEV=/dev/fd1H1440;
				INSTSIZE=1440;
			else	
				continue;
			fi
			break;
		done 

		while [ 0 ]; do
		    echo -e "partition\tsize\tstatus"
		    DEFPART=
		    DEFSIZE=40000
		    for P in hda1 hda2 hda3 hda4 hdb1 hdb2 hdb3 hdb4 sda1 sda2 sda3 sda4 sdb1 sdb2 sdb3 sdb4
		    do
			I=`fdisk -i /dev/$P 2>/dev/null`
			if [ ! "$I" ]; then
				continue;
			fi
			set $I
			o1=$1
			o2=$2
			o3=$3
			if [ $o3 != "Empty" ]; then
				echo -e "$o1\t$o2\t$o3"
			elif [ ! "$DEFPART" ]; then
				echo -e "$o1\t$o2\t$o3"
			fi
			if [ $o3 = "Empty" -o $o3 = "Linux" ]; then
				if [ $o2 -gt $DEFSIZE ]; then
					DEFPART=$o1
					DEFSIZE=$o2
				fi
			fi
		    done
		    if [ ! "$DEFPART" ]; then
			echo "No default partition found, run fdisk."
			DEFFDISK=y
		    else
		    	DEFSIZE=`fdisk -s $DEFPART`
		    	echo Default: $DEFPART $DEFSIZE
			DEFFDISK=n
		    fi
		    echo "If none of these partitions is suitable, you will"
		    echo "need to run fdisk manually."
		    echo -n "Do you need to run fdisk now? ($DEFFDISK) "
		    read ans
		    if [ "$ans" ]; then
			if [ $ans != n ];then
		                echo "running FDISK:"
		                echo "What is the target root drive? (hda)"
		                read ROOTDEVICE
		                if [ $ROOTDEVICE ]; then
		                        fdisk /dev/$ROOTDEVICE
		                else
		                        fdisk /dev/hda
		                fi
				continue;
		   	fi
		    fi 
		    echo -n "Which partition will be used for linux? ($DEFPART) "
		    read ROOTPART
		    if [ ! "$ROOTPART" ]; then
			ROOTPART=$DEFPART
		    fi
		    ans=`fdisk -i $ROOTPART`
		    set $ans
		    PARTSIZE=$2
		    if [ $3 != "Linux" ]; then
			echo "Creating partition $ROOTPART"
			fdisk -f $ROOTPART 131 >& /dev/null
			/sbin/sync
			/sbin/sync
			/sbin/sync
			echo "Partition created."
			# just to be sure 
			ans=`fdisk -i $ROOTPART`
			set $ans
			PARTSIZE=$2
		    fi
		    break
		done
		saveifs=$IFS
		IFS=1234567890
		set $ROOTPART
		ROOTDEV=$1 
		IFS=$saveifs

		if [ $PARTSIZE -lt 84000 ]; then
			DEFSWAP=n
		else
			DEFSWAP=y
		fi
		echo -n "Setup swapping (for 4MB ram or less)? ($DEFSWAP) "
		read ans
		if [ ! "$ans" ]; then
			ans=y
		fi
		if [ $ans = y ]; then
			DOSWAP="yes"
		else
			DOSWAP="no"
		fi

		echo -n "Install all packages? (y/n) "
		read DOA;
		if [ "$DOA" ]; then
			if [ "$DOA" != y ]; then
			    echo "Checking the Table Of Contents of the set..."
		            echo -n "Remove the boot floppy, insert diskette 1, and press enter "
			    read yn
			    CHGDISK=y
		            dd bs=98 skip=1 count=106 if=$INSTDEV 2>/dev/null |tar xvf - TOC 2>/dev/null
			    PKGS=
			    PARMS=

			    while read name size 
			    do
				    PARMS="$PARMS $name $size"
			    done < TOC

			    ask $PARMS
			fi
		fi	

		echo -n "Do you want BOOTACTV (boot time partition selector) installed? (y): "
		read BOOTACTV;
		if [ ! "$BOOTACTV" ];then
			BOOTACTV=y
		fi
		if [ $BOOTACTV != y ]; then
			echo -n "Where do you want lilo installed? ($ROOTDEV): "
			read ans
			if [ $ans ]; then
				BOOTDEV=$ans
			else
				BOOTDEV=$ROOTDEV
			fi
		else
			BOOTACTV=y
			echo -n "Where do you want lilo installed? ($ROOTPART): "
			read ans
			if [ $ans ]; then
				BOOTDEV=$ans
			else
				BOOTDEV=$ROOTPART
			fi
			
		fi
		echo -n "What is this machine's hostname? (tamu) "
		read MYNAME
		if [ ! "$MYNAME" ]; then
			MYNAME=tamu
		fi
		echo -n "Do you want networking configured? (y): "
		read ans
		if [ ! "$ans" ]; then
			ans=y
		fi
		if [ $ans = y ]; then
			DONET="yes"
			echo -n "What domain is the machine in (tamu.edu) "
			read DOMAIN
			if [ ! "$DOMAIN" ]; then
				DOMAIN=tamu.edu
			fi
			echo -n "What is this host's IP address (192.9.200.1) "
			read MYADDR
			if [ ! "$MYADDR" ]; then
				MYADDR=192.9.200.1
			fi

			saveifs=$IFS
			IFS=.
			set $MYADDR
			o1=$1
			o2=$2
			o3=$3
			o4=$4
			IFS=$saveifs
			if [ $o1 -lt 128 ]; then
			        NM="255.0.0.0"
			elif [ $o1 -lt 192 ]; then
			        NM="255.255.0.0"
			else
			        NM="255.255.255.0"
			fi

			echo -n "What is the netmask ($NM) "
			read NETMASK
			if [ ! "$NETMASK" ]; then
				NETMASK=$NM
			fi

			case "$NETMASK" in
			        255.0.0.0) NA="$o1.0.0.0"; BA="$o1.255.255.255"; DG="$01.254.254.254";;
			        255.255.0.0) NA="$o1.$o2.0.0"; BA="$o1.$o2.255.255"; DG="$o1.$o2.254.254";;
			        255.255.255.0) NA="$o1.$o2.$o3.0"; BA="$o1.$o2.$o3.255"; DG="$o1.$o2.$o3.254";;
			        *) NA="$MYADDR"; BA="$MYADDR"; DG="$MYADDR";;
			esac

			echo -n "What is the network's address ($NA) "
			read MYNET
			if [ ! "$MYNET" ]; then
				MYNET=$NA
			fi
			echo -n "what is the broadcast address ($BA) "
			read BROADCAST
			if [ ! "$BROADCAST" ]; then
				BROADCAST=$BA
			fi
			echo -n "What is the default gateway ($DG) "
			read GATEWAY
			if [ ! "$GATEWAY" ]; then
				GATEWAY=$DG
			fi
			echo -n "What is the nameserver address ($DG) "
			read NAMESERVER
			if [ ! "$NAMESERVER" ]; then
				NAMESERVER=$DG
			fi
		else
			DONET="no"
		fi
		echo -e "\n"
		echo "Whew... that's it for the questions."
		echo "Confirm installation of linux on $ROOTPART size $PARTSIZE (1k blocks)"
		echo -n "WARNING - This will delete all files on $ROOTPART: Continue (n)? "
		read ans;
		if [ ! "$ans" ]; then
			ans=n
		fi
		if [ $ans != y ]; then
			continue;
		fi
		umount $ROOTPART >& /dev/null
		echo "Making root filesystem - this may take several minutes..."
		mke2fs -c $ROOTPART $PARTSIZE
		/sbin/sync
		/sbin/sync
		/sbin/sync
		mount -t ext2 $ROOTPART /mnt
		if [ $DOSWAP = yes ]; then
			echo "Making swap file."
			dd bs=1k count=4096 if=/dev/zero of=/mnt/swapfile
			/sbin/sync
			umount /mnt
			/sbin/sync
			mount -t ext2 $ROOTPART /mnt
			/sbin/sync
			mkswap /mnt/swapfile 4096
			/sbin/sync
			umount /mnt
			/sbin/sync
			mount -t ext2 $ROOTPART /mnt
			/sbin/sync
			swapon /mnt/swapfile
		fi
		if [ "$CHGDISK" = 'n' ]; then
		        echo "Remove the boot floppy now, and "
		fi
		(cd /mnt;label -u -i $INSTDEV |tar --extract --verbose --to-stdout --file - $PKGS |gzip -d|tar xipf -)
		/sbin/sync
		/sbin/sync
		/sbin/sync
		echo "BOOTDEV=$BOOTDEV
ROOTDEV=$ROOTPART
MYNAME=$MYNAME
NAMESERVER=$NAMESERVER
DOMAIN=$DOMAIN
DONET=$DONET
MYADDR=$MYADDR
MYNET=$MYNET
NETMASK=$NETMASK
GATEWAY=$GATEWAY
BROADCAST=$BROADCAST
DOSWAP=$DOSWAP">/mnt/etc/system_config
		(cd /mnt/etc; /sbin/doconfig)

		if [ $BOOTACTV = y ]; then
			/bin/cp /mnt/boot/bootactv.bin $ROOTDEV
		fi
		cd /
		umount /mnt
		/sbin/sync
		echo "restore complete - remove all floppies and reboot"
		NEXT=2
		continue
	elif [ $ans = 2 ]; then
		exit;
	fi
done

