#
# Script to set up and customise users system.

function setupmodem()
{
        echo ' '
	echo 'SYSTEM HARDWARE CONFIGURATION'
	echo ' '
	echo 'This installation phase configures your mouse and modem. '
	echo ' '
	echo 'You can reconfigure your hardware at any time by running'
	echo 'the script /etc/syssetup.'
	echo ' '
	echo -n "Do you have a modem (y/n)? "
	read ans;
	if [ "$ans" = "Y" -o "$ans" = "y" ]; then
		while [ "$MODEM" = "" ]; do
			echo ' '
			echo "1 - Com1 (/dev/cua0)" 
			echo "2 - Com2 (/dev/cua1)" 
			echo "3 - Com3 (/dev/cua2)" 
			echo "4 - Com4 (/dev/cua3)" 
			echo ""
			echo -n "Which com port is your modem on (1/2/3/4)? "
			read ans;
			case $ans in
			1) MODEM=cua0 ;;
			2) MODEM=cua1 ;;
			3) MODEM=cua2 ;;
			4) MODEM=cua3 ;;
			*) echo "invalid com port" ;;
			esac
		done
		(cd dev; ln -sf $MODEM modem)
#		if [ ! -f etc/default/uugetty.modem ]; then
#			cp -a etc/default/uugetty.ttyS1 etc/default/uugetty.modem
#			echo "ALTLOCK=$MODEM" >> etc/default/uugetty.modem;
#		fi
	fi
}

function setupmouse()
{
	echo ' '
	echo -n "Do you have a mouse (y/n)? "
	read ans;
	if [ "$ans" = "Y" -o "$ans" = "y" ]; then
		while [ "$MOUSE" = "" ]; do
			echo ' '
			echo "1 - Logitech BusMouse"
			echo "2 - PS2 Bus Mouse"
			echo "3 - Microsoft Bus Mouse"
			echo "4 - ATI XL Bus Mouse"
			echo "5 - Microsoft Serial Mouse"
			echo "6 - Logitech Serial Mouse"
			echo "7 - MouseSystems Serial Mouse"
			echo "8 - MM Serial Mouse"
			echo ""
			echo -n "What type of mouse do you have (1-8)? "
			read ans;
			case $ans in
			1) MOUSE=bmouselogitec ;;
			2) MOUSE=ps2aux ;;
			3) MOUSE=bmousems ;;
			4) MOUSE=bmouseatixl ;;
			5|6|7|8) MOUSE=serial ;;
			*) echo "invalid mouse type" ;;
			esac
		done
		while [ "$MOUSE" = "serial" ]; do
			echo ' '
			echo "1 - Com1 (/dev/cua0)" 
			echo "2 - Com2 (/dev/cua1)" 
			echo "3 - Com3 (/dev/cua2)" 
			echo "4 - Com4 (/dev/cua3)" 
			echo ""
			echo -n "Which com port is your mouse on (1/2/3/4)? "
			read ans;
			case $ans in
			1) MOUSE=cua0 ;;
			2) MOUSE=cua1 ;;
			3) MOUSE=cua2 ;;
			4) MOUSE=cua3 ;;
			esac
		done
		if [ "$MOUSE" = "$MODEM" ]; then
			echo "warning: mouse device $MOUSE is on the same device as the modem";
		else
			(cd dev; ln -sf $MOUSE mouse )
			return 0;
		fi
	fi
}

setuphostname()
{
	if [ ! -d etc/rc.net ]; then
		return 0;
	fi
	echo -n "Current host name is `cat etc/host`.  Enter new hostname: "
	read ans;
	if [ "$ans" != "" ]; then
		echo $ans > etc/host
		echo "Hostname set.  Remember to edit /etc/hosts for ip stuff".
	fi
	echo "order hosts" > etc/host.conf
	echo -n "Enter domain name, or just hit enter if not networked (eg: softlanding.com):"
	read ans;
	if [ "$ans" = "" ]; then
		return 0;
	else
		echo $ans > etc/domain
	fi
	echo -n "Do you wish this host to be a name server? (y/n): "
	read ans;
	if [ "$ans" != "y" ]; then
		read ans;
		echo "Domainname set.  running /etc/hostcvt.build to configure DNS".
		( cd etc; hostcvt.build );
		echo "order bind hosts" > etc/host.conf
		return 0;
	else
		echo "enter ip number of nameserver host"
		read ipnum;
		echo "enter domain name of nameserver host"
		read nsdom;
		echo "domain $nsdom" > etc/resolv.conf
		echo "nameserver $ipnum" >> etc/resolv.conf
	fi
}

function setuplilo()
{
	ROOTDEVICE=`grep ROOTDEV etc/hwconfig | cut -c9-20`
	VGAMODE=`grep VGAMODE etc/hwconfig | cut -c9-20`
	case $VGAMODE in
#		-3) VGAMODE=ask;;
#		-2) VGAMODE=extended;;
# A quick hack. Cut two lines above & added 2 below to make VGA always normal.
		-3) VGAMODE=normal;;
		-2) VGAMODE=normal;;
		-1) VGAMODE=normal;;
	esac;
	echo ""
	echo "LILO (Linux Loader) Installation:"
	echo ""
	echo "LILO, the Linux Loader, allows you to boot Linux directly off your"
	echo "hard drive without using a boot floppy disk.  This set of Linux "
        echo "disks has been designed to allow two types of LILO installations,"
	echo "OS/2 Boot Manager and Standard."
	echo ""
	echo "1. If you are using OS/2's Boot Manager, this choice will allow"
	echo "   you to boot Linux from the Boot Manager menu. If you have"
	echo "   already added the Linux partition to the Boot Manager menu,"
	echo "   this choice will complete the Boot Manager installation process."
	echo ""
	echo "2. If you are planning to run Linux as the only operating system on"
	echo "   your machine, use this option to boot directly from the boot sector"
        echo "   of your drive."
	echo ""
	echo "3. If you're not sure, select 3 to skip LILO and use a boot floppy instead."
        echo "   If you are attempting to preserve DOS partitions and are not using "
        echo "   OS/2 Boot Manager, use this option. You can read more about how to "
        echo "   configure LILO manually by reading the file /etc/lilo/README. By the"
        echo "   way, LILO may be removed using MS-DOS fdisk with the command: "
        echo "   fdisk /mbr"
	echo ""
	echo -n "Which option would you like? (1/2/3): "
	read ans;
	ROOTDISK=`echo $ROOTDEVICE | cut -b 1-8`
	if [ "$ans" = "1" ]; then
		ROOTDISK=`echo $ROOTDEVICE`
	fi
	(sed "/ROOTDEV/s+ROOTDEV+$ROOTDEVICE+" | sed "/VGAMODE/s/VGAMODE/$VGAMODE/" | 
		sed "/ROOTDISK/s+ROOTDISK+$ROOTDISK+" ) < etc/lilo/config.in > etc/lilo/config
# This is a new line that allows LILO to automatically decide if your kernel is
# compressed, and modify its configuration accordingly
	if [ -s zImage ]; then
		cp etc/lilo/config etc/lilo/config.in
		sed "/Image/s/Image/zImage/" < etc/lilo/config.in > etc/lilo/config
	fi
	dd if=$ROOTDISK of=etc/lilo/bootsave bs=512 count=1 &> /dev/null
	if [ "$ans" = "1" -o "$ans" = "2" ]; then
		(cd $ROOTDEV/etc/lilo; ./lilo -r $ROOTDEV -m /etc/lilo/map \
			-C /etc/lilo/config)
		echo "Hard drive now setup for booting."
	fi
}

ROOTDEV=/
if [ "$1" = "-instroot" ]; then
	ROOTDEV=$2;
	shift 2;
fi

cd $ROOTDEV

setupmodem;
setupmouse;
if [ -s /root/etc/lilo/config.in ]; then
	setuplilo;
fi
#setuphostname;
if [ "$1" = '-install' ]; then
	etc/sysperms -instroot $ROOTDEV -install
fi
sync
