#!/bin/sh
# Script to set up and customise users system.
PATH="$PATH:/root/usr/bin:/root/bin:/root/etc"
hash -r

function setupmodem()
{
	echo -n "do you have a modem (y/n): "
	read ans;
	if [ "$ans" = "Y" -o "$ans" = "y" ]; then
		while [ "$MODEM" = "" ]; do
			echo "1 - Com1 (/dev/cua0)" 
			echo "2 - Com2 (/dev/cua1)" 
			echo "3 - Com3 (/dev/cua2)" 
			echo "4 - Com4 (/dev/cua3)" 
			echo ""
			echo -n "Your modem is on which com port (1-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 -n "do you have a mouse (y/n): "
	read ans;
	if [ "$ans" = "Y" -o "$ans" = "y" ]; then
		while [ "$MOUSE" = "" ]; do
			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 "1 - Com1 (/dev/cua0)" 
			echo "2 - Com2 (/dev/cua1)" 
			echo "3 - Com3 (/dev/cua2)" 
			echo "4 - Com4 (/dev/cua3)" 
			echo ""
			echo -n "Your mouse is on which com port (1-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 [ ! -f etc/rc.net ]; then
		return 0;
	fi
	echo -n "Host name to give to this machine [`cat etc/host`]: "
	read HostName;
	if [ "$HostName" = "" ]; then
		return 0
	fi
	echo $HostName > etc/host
	echo -n "Enter your ip number: "
	read ipaddr;
	if [ ! -f etc/hosts.in ]; then
		cp etc/hosts etc/hosts.in
	fi
	Network=`echo $ipaddr | cut -f 1-3 -d .`.0
	echo -n "Ip address of network [$Network]: "
	read ans;
	if [ "$ans" != "" ]; then
		Network=$ans;
	fi
	Router=`echo $ipaddr | cut -f 1-3 -d .`.1
	echo -n "Ip address of router [$Router]: "
	read ans;
	if [ "$ans" != "" ]; then
		Router=$ans;
	fi
	DomainName=`cat etc/domain`
	echo -n "Domain name that this machine [$DomainName]: "
	read ans;
	if [ "$ans" != "" ]; then
		DomainName=$ans
		echo $DomainName > etc/domain
	fi
	(sed "/softland/s/softland/$HostName" | sed "/192.0.2.128/s/192.0.2.128/$ipaddr" | \
	sed "/192.0.2.0/s/192.0.2.0/$Network" | sed "/192.0.2.1/s/192.0.2.1/$Router" ) < \
		etc/hosts.in > etc/hosts
	(sed "/victrola.sea.wa.us/s/victrola.sea.wa.us/$HostName.$DomainName" | \
		sed "/victrola/s/victrola/$HostName" ) < usr/local/lib/smail/paths.sample \
		> usr/local/lib/smail/paths
	(sed "/victrola.sea.wa.us/s/victrola.sea.wa.us/$HostName.$DomainName" | \
		sed "/sea.wa.us:uucp/s/sea.wa.us:uucp/$DomainName" ) < usr/local/lib/smail/config.sample \
		> usr/local/lib/smail/config
	(sed "/.sea.wa.us/s/.sea.wa.us/.$DomainName" | \
		sed "/victrola/s/victrola/$HostName" ) < usr/local/lib/elm/elm.rc.sample \
		> usr/local/lib/elm/elm.rc
	echo "order hosts bind" > etc/host.conf
	echo "multi" >> etc/host.conf
	echo -n "Do you wish to have your machine setup as a name server? (y/n): "
	read ans;
	if [ "$ans" = "y" ]; then
		echo "Running /etc/hostcvt.build to configure DNS".
		( cd etc; hostcvt.build );
		return 0;
	else
		echo -n "Enter ip number of nameserver host: "
		read ipnum;
		echo -n "Domain name of nameserver host [$DomainName]: "
		read nsdom;
		if [ "$nsdom" = "" ]; then
			nsdom=$DomainName
		fi
		echo "domain $nsdom" > etc/resolv.conf
		echo "nameserver $ipnum" >> etc/resolv.conf
	fi
}

function setuplilo()
{
	ROOTDEVICE=`fgrep ROOTDEV etc/hwconfig | cut -c9-20`
	VGAMODE=`fgrep VGAMODE etc/hwconfig | cut -c9-20`
	case $VGAMODE in
		-3) VGAMODE=ask;;
		-2) VGAMODE=extended;;
		-1) VGAMODE=normal;;
	esac;
	ROOTDISK=$ROOTDEVICE
	ROOTBASE=`echo $ROOTDISK | cut -c1-8`;
	RunLilo="y"
	if [ "$ROOTBASE" != '/dev/hda' -a "$ROOTBASE" != '/dev/sda' ]; then
		echo "Can not set up for boot from a second hard disk partition."
		echo -n "Is it ok to take over primary disks partition bootstrap (y/n): "
		read RunLilo;
		if [ "$RunLilo" = "y" ]; then
			echo 'Ok, to remove this later use the DOS command "FDISK /MBR"'
		fi
		ROOTDISK="`echo $ROOTBASE | cut -c1-7`a"
	else
		echo 'Setting up lilo on $ROOTDISK (which must be fdisked active to boot from)'
	fi
	BOOTPARMS="linux"
	for i in 'hd=' 'qic02=' 'ether=' 'mitsumi='  'bmouse='; do
		BOOTP="`strings /proc/1/environ | fgrep $i`"
		if [ "$BOOTP" != "" ]; then
			BOOTPARMS="$BOOTPARMS $BOOTP"
		fi
	done
	echo '#!/bin/sh' > etc/lilo/bootargs
	echo "./lilo \$* -R \"$BOOTPARMS\"" >> etc/lilo/bootargs
	(sed "/ROOTDEV/s+ROOTDEV+$ROOTDEVICE+" | sed "/VGAMODE/s/VGAMODE/$VGAMODE/" | 
		sed "/ROOTDISK/s+ROOTDISK+$ROOTDISK+" ) < etc/lilo/config.in > etc/lilo/config
	dd if=$ROOTDISK of=etc/lilo/bootsave bs=512 count=1 &> /dev/null
	if [ "$RunLilo" = "y" ]; then
		echo -n "If any, specify a DOS partition to setup for booting (eg: /dev/hda1): "
		read ans;
		if [ "$ans" != "" ]; then
			echo "other = $ans" >> etc/lilo/config
			echo "table = `echo $ROOTDEVICE | cut -b 1-8`" >> etc/lilo/config
			echo "label = dos" >> etc/lilo/config
		fi
		(cd $ROOTDEV/etc/lilo; ./install "-r $ROOTDEV"; sh ./bootargs -r $ROOTDEV )
		echo "harddrive is now setup for booting"
	fi
}

function setupfloppy()
{
	FLOPPYA=`fgrep FLOPPYA etc/hwconfig | cut -c9-20`
	if [ "/dev/fd0H1440" = "$FLOPPYA"  ]; then
		FLOPPYB=/dev/fd1h1200;
	else
		FLOPPYB=/dev/fd1H1440;
	fi
}

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

cd $ROOTDEV

if [ "$1" = '-install' ]; then
	hash -r
	while [ 1 ]; do
		echo "/etc/syssetup: Configuring system to local defaults"
		setupmodem;
		setupmouse;
		setuplilo;
		setuphostname;
		echo -n "Are you satisfied with this configuration (y/n): "
		read ans;
		if [ "$ans" != "n" ]; then
			echo "Ok, you can always run /etc/syssetup later to change things"
			break;
		fi
	done
	etc/sysperms -instroot $ROOTDEV -install
elif [ "$1" = '-modem' ]; then
	setupmodem;
elif [ "$1" = '-mouse' ]; then
	setupmouse;
elif [ "$1" = '-lilo' ]; then
	setuplilo;
elif [ "$1" = '-hostname' ]; then
	setuphostname;
fi
sync
