#!/bin/sh
# Script to set up and customise users system.
PATH="$PATH:/root/usr/bin:/root/bin:/root/etc:/root/sbin"
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
	if [ -f usr/lib/smail/config ]; then
		mv -f usr/lib/smail/config usr/lib/smail/config.bak
		(sed "s/hostname=[^ ]*/$HostName/" | \
			sed "s/visible_name=[^ ]*/$HostName.$DomainName/" | \
			sed "s/visible_domain=[^ ]*/$HostName.$DomainName/" | \
			sed "s/postmaster=[^ ]*/root/" )\
			< usr/lib/smail/config.bak > usr/lib/smail.config
	fi
	echo "order hosts bind" > 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()
{
	LILOCONFIG=etc/lilo.conf
	LILOBOOTARGS=boot/bootargs
	LILODIR=boot
	if [ ! -e $LILOCONFIG.in ]; then
		LILOCONFIG=etc/lilo/config
		LILOBOOTARGS=etc/lilo/bootargs
		LILODIR=etc/lilo
	fi
	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"
	fi
	echo -n "Is it ok to make $ROOTDISK the active partition? [y]: ";
	read ans;
	if [ "n" != "$ans" ]; then
		activate `echo $ROOTDISK | cut -b1-8` `echo $ROOTDISK | cut -b9`  > /dev/null
	fi
	BOOTPARMS="linux"
	for i in 'hd=' 'qic02=' 'ether=' 'mcd='  'bmouse='; do
		BOOTP="`strings /proc/1/environ | fgrep $i`"
		if [ "$BOOTP" != "" ]; then
			BOOTPARMS="$BOOTPARMS $BOOTP"
		fi
	done
	echo '#!/bin/sh' > $LILOBOOTARGS
	echo "lilo \$* -R \"$BOOTPARMS\"" >> $LILOBOOTARGS
	(sed "/ROOTDEV/s+ROOTDEV+$ROOTDEVICE+" | sed "/VGAMODE/s/VGAMODE/$VGAMODE/" | 
		sed "/ROOTDISK/s+ROOTDISK+$ROOTDISK+" ) < $LILOCONFIG.in > $LILOCONFIG
	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" >> $LILOCONFIG
			echo "table = `echo $ROOTDEVICE | cut -b 1-8`" >> $LILOCONFIG
			echo "label = dos" >> $LILOCONFIG
		fi
		(cd $LILODIR; lilo -r $ROOTDEV); sh $LILOBOOTARGS -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 "/sbin/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 /sbin/syssetup later to change things"
			break;
		fi
	done
	sbin/sysperms -instroot $ROOTDEV -install
elif [ "$1" = '-modem' ]; then
	setupmodem;
elif [ "$1" = '-mouse' ]; then
	setupmouse;
elif [ "$1" = '-lilo' ]; then
	setuplilo;
elif [ "$1" = '-hostname' ]; then
	setuphostname;
else
	while [ 1 ]; do
		echo;
		echo "  0 - quit"
		echo "  1 - Mouse"
		echo "  2 - Modem"
		echo "  3 - Lilo"
		echo "  4 - Hostname"
		echo "  5 - X-Windows"
		echo
		echo -n "Which item to setup? [1-5] ";
		read ans;
		case $ans in
		"0") exit 0;;
		1) setupmouse;;
		2) setupmodem;;
		3) setuplilo;;
		4) setuphostname;;
		5) sbin/xsetup;;
		esac
	done
fi
sync
