#!/bin/sh
export PATH="$PATH:/etc:/root/etc:/local/bin:/usr/bin:/bin:/root/bin:/root/usr/bin:/root/sbin."
EXPART=/dev/hda1
if [ "/dev/ram /" = "`rdev`" ]; then
	echo 'To avoid accidents, you should remove the bootdisk now!'
fi
echo -n "Do you have a color screen [n]: "
read ans;
if [ "$ans" = "y" ]; then
	echo '[30m[46m[8][H[J'
fi
if [ -f /zImage ]; then
	echo "[7mWelcome to the SLS installation program (copyright Softlanding Software)[27m"
	echo " "
else
	echo "Error: You must reboot after running the demo before you can install"
	exit 0
fi
	

function usage()  {
	echo " "
	echo "Installs SLS onto a hard disk from floppies, tape, CD, HD or Network"
	echo "usage: doinstall PART [ PART2 MNT2 ...]"
	echo "where: PART is the partition to install to, optionally followed by" 
	echo "partition/directory pairs to mount on root.   for example"
	echo "       doinstall                       # does menu driven install"
	echo "       doinstall /dev/hda2"
	echo "       doinstall /dev/hda2 /dev/hda3 /usr /dev/hdb1 /usr/spool"
	exit 1
}

function domount() {
	if [ ! -b $1 ]; then
		MOUNTTYPE="nfs"
	else
		MOUNTTYPE="ext2"
	fi
	mount -t $MOUNTTYPE $1 $2$3
	MNTSTAT=$?;
	if [ $MNTSTAT != 0 ]; then
		echo "Error: can not mount $1.  Did you use: mke2fs $1 SIZE"
		echo "where SIZE is the number of blocks shown by fdisk?"
		exit 1;
	fi
	if [ "$PARTNAMES" = "" ]; then
		echo "$1	/		$MOUNTTYPE	defaults" > /root/fstab.tmp
		mkdir /root/etc >& /dev/null
	else
		echo "$1	$3		$MOUNTTYPE	defaults" >> /root/fstab.tmp
	fi
	PARTNAMES="$PARTNAMES $2"
}

declare -i DISKCOUNT

function runfdisk()
{	
	if [ $DISKCOUNT = 1 ]; then
		CURDISK=$DISKLIST;
	else
		echo -n "which disk do you wish to partition [$DISKLIST]"
		read CURDISK
	fi
	fdisk $CURDISK;
	echo "If you wrote any changes, please (hard) reboot your PC now"
	exit 0
}

function domkfs()
{
	if [ "$1" = "" ]; then
		return 1
	fi
	if [ ! -b $1 ]; then
		echo "$1 is not a valid partition"
		return 1
	fi
	if [ "`echo $1 | cut -c9`" = "" ]; then
		echo "invalid partition"
		return 1
	fi
	echo "Preparing Hard Drive.  This will take a few minutes, please standby..."
	echo " "
	mke2fs -m 1 $1 || (echo "mke2fs failed"; exit 1)
}

function partsetup()
{
	if [ "$ROOTDEVICE"  = "" ]; then
		echo -n "enter the name of the partition to use as root (eg. $EXPART): "
		read ROOTDEVICE
		NEWPART=$ROOTDEVICE
		NEWDIR=
	else
		echo -n "enter the name of the partition (eg. $EXPART):"
		read NEWPART
		echo -n "what directory should this partition be mounted on (eg. /home): "
		read NEWDIR
	fi
	if [ "" = "$NEWPART" ]; then
		return;
	fi
	mount -t ext2 $NEWPART /user >& /dev/null
	MTSTAT=$?;
	umount $NEWPART >& /dev/null
	if [ $MTSTAT = 0 ]; then
		echo -n "Found ext2 fs on $NEWPART.  Is it OK to delete all its data? [y]: ";
		read ans
		if [ "$ans" != "n" ]; then
			MTSTAT=1
		else
			echo "Ok, will attempt to install overtop of existing files"
		fi
	fi
	if [ $MTSTAT != 0 ]; then
		echo -n "WARNING: All data will be lost from partition $NEWPART.  Ok to proceed [y]: "
		read goon;
		if [ "$goon" = "n" ]; then
			return;
		fi
		domkfs $NEWPART || exit 1
	fi
	if [ "$NEWDIR" != "" ]; then
		 mkdir -p /root$NEWDIR
	fi
	domount $NEWPART /root $NEWDIR
}

function linuxsetup()
{	fdisk -l
	while [ 1 ]; do
		echo '          [7m Disk Setup Procedure [27m'
		echo ' '
		echo ' [7m 1 [27m  Setup Linux partitions (first one will be used as the root)'
		echo ' [7m 2 [27m  Setup a swap PARTITION (required for 4 Megs RAM or less)'
		echo ' [7m 3 [27m  Setup a swap FILE on root (must do 1 above first)'
		echo ' [7m 4 [27m  Display partition sizes'
		echo ' [7m 5 [27m  Run fdisk to change partition sizes (will require a reboot)'
		echo ' [7m 6 [27m  Abort installation'
		echo ' [7m 7 [27m  Done (commence installation)'
		echo ' '
		echo -n " Select one of the above (1-7): "
		read func;
		case $func in
		1) 	 partsetup;;
		[2,3])	if [ "$swapsize" != "" ]; then
				echo "Sorry: swap area was already created"
				continue;
			fi
			if [ "$func" = "2" ]; then
				echo -n "Use which partition for swapping (eg. $EXPART): "
				read swappart;
				swapsize=`fdisk -s $swappart`
			else
				if [ "$PARTNAMES" = "" ]; then
					echo "Sorry, you must setup the root partition first";
					continue;
				fi
				echo -n "How big a swap file in Megs (max 16): "
				declare -i swapmegs
				read swapmegs
				swapsize = swapmegs * 1024;
				swappart=/root/swap
			fi
			mkswap $swappart $swapsize &&
			swapon $swappart &&
			if [ -f /root/fstab.tmp ]; then
				echo "$1	$2		swap" >> /root/fstab.tmp
			fi
			;;
		4) fdisk -l;;
		5) runfdisk ;;
		6) exit 0;;
		7) 
			if [ "$PARTNAMES" = "" ]; then
				echo "Error: you must setup a Linux partition first"
				continue;
			fi
			break;; 
		esac
	done
}

function automountcd() {
	for i in sr0 mcd sonycd cdu535 pancd lmscd sbpcd matscd; do
		mount -t iso9660 /dev/$i /mnt
		if [ $? = 0 ]; then
			CDDEVICE=$i
			echo "$i CDROM" >> /root/etc/hwconfig
			return 0 
		fi
	done
	echo "Can not mount CD"
	return 1
}


function mountsource() {
	case $1 in 
	harddrive)
		INSTSRC=/mnt/install
		while [ 0 ]; do
			echo -n "Enter the partition that the source is on (eg. /dev/hda1):"
			read hdloc;
			echo -n "Enter the type of the filesystem (minix/ext2/msdos)";
			read hdtype;
			mount -t $hdtype $hdloc /mnt && break
		done
		while [ 0 ]; do
			echo -n "Enter subdirectory name (if not /install)"
			read ans;
			if [ "$ans" != "" ]; then
				INSTSRC=/mnt/$ans
			fi
			if [ -d $INSTSRC/a2 ]; then
				break
			fi
			echo "failed to find install sets in $INSTSRC"
		done
	;;
	cdrom)
		INSTSRC=/mnt/install
		while [ 1 ]; do
			echo " 0 - Exit"
			echo " 1 - Do not know (try em all)"
			echo " 2 - SCSI"
			echo " 3 - Mitsumi"
			echo " 4 - Sony CDU31A"
			echo " 5 - Sony 531/535"
			echo " 6 - Panasonic"
			echo " 7 - LMS/Philips"
			echo " 8 - Sound Blaster/Lasermate"
			echo " 9 - Matsushitai Sound Blaster"
			echo
			echo -n "What type of CDROM player do you have? (0-9): "
			read ans;
			case  $ans in
			    0)	exit 0;;
			    1) CDDEVICE=sr0 ;;
			    2) CDDEVICE=mcd ;;
			    3) CDDEVICE=sonycd ;;
			    4) CDDEVICE=cdu535 ;;
			    5) CDDEVICE=pancd ;;
			    6) CDDEVICE=lmscd ;;
			    7) CDDEVICE=sbpcd ;;
			    8) CDDEVICE=matscd ;;
			    9) automountcd
				break;;
			    *)	continue;;
			esac
			if [ "" != "$CDDEVICE" ]; then
				(mkdir /root/dev && ln -sf $CDDEVICE /root/dev/cdrom)
				mount -t iso9660 /dev/$CDDEVICE /mnt && break;
				echo "Could not mount CD on /dev/$CDDEVICE";  
			fi
		done
	;;
	network)
		INSTSRC=/mnt/install
		echo "Following is your current IP setup"
		cat /etc/hosts
		echo -n "Do you wish to change or set your IP address? [n]: "
		read ans;
		while [ "y" = "$ans" ]; do
			echo -n "Enter your IP address (eg, 192.0.2.129): "
			read ans;
			echo "$ans	`hostname`" > /etc/hosts
			echo -n "Enter your Network address (eg, 192.0.2.0): "
			read ans;
			echo "$ans	network" >> /etc/hosts
			echo -n "Enter your Netmask address (eg, 255.255.255.0): "
			read ans;
			echo "$ans	netmask" >> /etc/hosts
			echo -n "Enter your Router address, if any (eg, 192.0.2.1): "
			read ans;
			if [ "" != "$ans" ]; then
				echo "$ans	router" >> /etc/hosts
			fi
			echo "127.0.0.1	localhost" >> /etc/hosts
			sync
			echo "IP setup is now changed";
		done
		if [ "$RCNET_RUN" = "" ]; then
			/etc/rc.net
		fi
		RCNET_RUN=1
		echo -n "Enter IP address of NFS server:"
		read ipaddr;
		echo -n "Enter directory on server containing SLS:"
		read path;
		while [ 1 ]; do
			mount -t nfs $ipaddr:$path /mnt  && break;
			echo -n "mount failed (sometimes takes 2-3 tries). Try again [y]: "
			read ans;
			if [ "$ans" = "n" ]; then
				break;
			fi
		done
		INSTSRC="/mnt/$path"
	;;
	esac
	MNTSTAT=$?
	if [ $MNTSTAT != 0 ]; then
		echo "error: can not mount source"
		exit 2;
	fi
	if [ -d $INSTSRC/a2 -a -d $INSTSRC/a3 ]; then
		return
	elif [ -d /mnt/a2 -a -d /mnt/a3 ]; then
		INSTSRC=/mnt
		return
	else
		echo "error: SLS distribution files not found in $INSTSRC"
		usage
	fi
}

umount /root >& /dev/null
INSTDEV=/dev/fd0
INSTSRC=
INSTTYPE=
INSTMEDIA=

while [ 0 ]; do
	umount /mnt >& /dev/null
	echo '          [7m Install Source [27m'
	echo ''
	echo ' [7m 1 [27m Install from Floppy Disks'
	echo ' [7m 2 [27m Install from Hard Disk'
	echo ' [7m 3 [27m Install from Tape'
	echo ' [7m 4 [27m Install from CDROM'
	echo ' [7m 5 [27m Install just bootdisk to HD'
	if [ -e /etc/rc.net ]; then
		echo ' [7m 6 [27m Install from Network (via NFS)'
	fi
	echo ' '
	echo -n 'Where will you be installing SLS from (1-6): '
	read ans;
	case $ans in
		1 ) INSTMEDIA=floppy
		while [ 0 ];do 
			echo " "
			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 Drive You Will Be Doing The Installation From (1/2/3/4): "
			read answer; 
			case $answer in
				1) INSTDEV=/dev/fd0h1200;;
				2) INSTDEV=/dev/fd0H1440;;
				3) INSTDEV=/dev/fd1h1200;;
				4) INSTDEV=/dev/fd1H1440;;
				*) continue;;
			esac
			umount $INSTDEV
			break;
		done 
		;;
		2 ) INSTMEDIA=harddrive; mountsource harddrive ;;
		3 ) INSTMEDIA=tape ;;
		4 ) INSTMEDIA=cdrom; mountsource cdrom ;;
		5 ) umount /proc; cp -ax /[a-qs-z]* /root; mkdir /root/root; sync
		    echo "Reboot now using ALT at LILO prompt and \"harddisk root=$ROOTDEVICE\""
                    exit 0 ;;
		6 ) if [ -e /etc/rc.net ]; then
			INSTMEDIA=network; mountsource network; 
		    fi ;;
		*) echo "$ans invalid, pick again"; continue;; 
	esac
	break;
done
if [ $# -lt 1 ]; then
	linuxsetup
else
	ROOTDEVICE=$1
	domount $ROOTDEVICE / /root
fi

while [ "$INSTMEDIA" != tape ]; do
	echo ' '
	echo '    1 - Install a minimal system (15 Meg)'
	echo '    2 - Install the full base system (60 Meg)'
	echo '    3 - Install base system + X11 (80 Meg)'
	echo '    4 - Install everything (100 Meg)'
	echo '    5 - Install to run with the CD as root'
	echo " "
	echo -n 'Enter type of install (1-5): '
	read ans;
	if [ $ans = 1 ]; then
		INSTTYPE="mini"
		break;
	elif [ $ans = 2 ]; then
		INSTTYPE="base"
		break;
	elif [ $ans = 3 ]; then
		INSTTYPE="all"
		break;
	elif [ $ans = 4 ]; then
		INSTTYPE="everything"
		break;
	elif [ $ans = 5 ]; then
		INSTTYPE="cdroot"
		cp -a /mnt/local/* /root/ || (echo "copy failed"; exit -2)
		break;
	fi
done
	mkdir -p /root/etc >& /dev/null
	echo -n "" > /root/etc/hwconfig
if [ "$INSTTYPE" != "cdroot" ]; then
	mkdir -p /root/install/installed
	mkdir -p /root/install/disks
	mkdir -p /root/install/scripts
	mkdir -p /root/install/catalog
	while [ "$NEWPART" = "" ]; do
		if [ "" != "$2" -a "" != "$3" ]; then
			mkdir /root$3
			domount $2 /root $3
			shift 2;
		else
			break;
		fi
	done
	if [ -f /root/doinst.sh ]; then
		sh /root/doinst.sh;
	fi
	if [ "$INSTMEDIA" != tape ]; then
		echo -n 'Do you want to be prompted, with a description, before each package? [n]: '
		read ans
		if [ "$ans" = "y" ]; then
			DOPROMPT="-doprompt"
		fi
	fi
	if [ "$INSTSRC" != "" ]; then
		INSTSRC="-instsrc $INSTSRC"
	fi
	if [ "$INSTMEDIA" = tape ]; then
		export DOPROMPT
		tarsh /dev/rmt0 /bin/tapeinstall
	else
		echo "sysinstall -instdev $INSTDEV $INSTSRC -instroot /root $DOPROMPT -$INSTTYPE"
		sysinstall -instdev $INSTDEV $INSTSRC -instroot /root $DOPROMPT -$INSTTYPE
		if [ "$INSTTYPE" = "mini" ]; then
			test ! -e local && mv usr local && mv usrlocal usr
		fi
	fi
fi
hash -r
mv /root/fstab.tmp /root/etc/fstab
if [ "$INSTDEV" != "" ]; then
	if [ $INSTDEV =  /dev/fd1H1440 ]; then
		INSTDEV=/dev/fd0h1200;
	elif [ $INSTDEV =  /dev/fd1h1200 ]; then
		INSTDEV=/dev/fd0H1440;
	fi
	if [ -e /etc/rc.net ]; then
		echo "FLOPPYA /dev/fd0H1440" >> /root/etc/hwconfig
	else
		echo "FLOPPYA /dev/fd0h1200" >> /root/etc/hwconfig
	fi
fi
echo "ROOTDEV $ROOTDEVICE" >> /root/etc/hwconfig
VGAMODE=-3

if [ -e /etc/rc.net ]; then
	if [ -e /root/zImage ]; then
		mv -f /root/zImage /root/Image
		dd if=/zImage of=/root/zImage
	else
		dd if=/zImage of=/root/zImage
		if [ ! -e /root/Image ]; then
			(cd /root && ln zImage Image)
		fi
	fi
else
	if [ -e /root/zImage ]; then
		dd if=/zImage of=/root/Image
	else
		if [ -e /root/Image ]; then
			mv -f /root/Image /root/zImage
			dd if=/zImage of=/root/Image
		else
			dd if=/zImage of=/root/zImage
			(cd /root && ln zImage Image)
		fi
	fi
fi


if [ "/dev/ram /" = "`rdev`" -a "cdroot" != "$INSTTYPE" ]; then
	echo -n 'Now put a formatted floppy into your boot drive and hit enter: '
	read ans;
	if [ "$INSTTYPE" = "cdroot" ]; then
		if [ $INSTDEV =  /dev/fd1H1440 ]; then
			dd if=/mnt/install.3/a1 of=/dev/fd0
		else
			dd if=/mnt/install/a1 of=/dev/fd0
		fi
	else
		dd if=/root/zImage of=$INSTDEV
		rdev $INSTDEV $ROOTDEVICE
	fi
fi

echo -n "Do you wish to have the video mode preset at boot time? [n]: "
read ans;
if [ "$ans" = "y" ]; then
	echo -n "Enter the mode (-1 for 80x25 mode, or the key 1, 2, 3, ...): "
	read VGAMODE;
	if [ "/dev/ram /" = "`rdev`" -a "cdroot" != "$INSTTYPE" ]; then
		rdev -v $INSTDEV $VGAMODE
	fi
fi
sync

echo "VGAMODE $VGAMODE" >> /root/etc/hwconfig

if [ "$INSTTYPE" = "cdroot" ]; then

	cat  > /root/etc/lilo.conf << LILOEOF
boot=$ROOTDEVICE
install = /boot/boot.b
delay = 100
vga=$VGAMODE
  image = /zImage
  label = linux
  root = $ROOTDEVICE
LILOEOF
	rm -f /root/dev/MAKEDEV;  cp -a /mnt/etc/MAKEDEV /root/dev
	/mnt/local/sbin/lilo -r /root
	/mnt/local/sbin/lilo -r /root -R "linux local=$ROOTDEVICE root=/dev/$CDDEVICE"
	echo "Installation is finished.  You should use fdisk to make"
	echo "partition $ROOTDEVICE the active one to allow Linux boots."
	echo "----------------------------------------------------------"
	exit 0
fi

(cd /bin && for i in *; do if [ -e /root/sbin/$i -o -e /root/bin/$i -o -e /root/usr/bin/$i ]; then
	echo > /dev/null; else cp $i /root/bin; fi; done)
if [ "$INSTSRC" != "" -a ! -e /root/usr/bin/gawk ]; then
	(cd /root && cat /mnt/*/binutils.tgz | zcat | tar -xpf - usr/bin/awk usr/bin/gawk usr/bin/strings)
fi

if [ -x /root/sbin/syssetup ]; then
	(cd /root && sbin/syssetup -instroot /root -modem && \
	sbin/syssetup -instroot /root -lilo && \
	sbin/syssetup -instroot /root -hostname && \
	sbin/sysperms -instroot /root -install )
#	/bin/xsetup -instroot /root
#	cp -a /bin/xsetup /root/sbin 
	if [ ! -e /root/etc/inet ]; then (cd /root/etc && ln -s . inet) fi
fi

if [ "$INSTTYPE" = "cdroot" ]; then
	/mnt/etc/lilo/lilo -r /root -R "cdrom root=$ROOTDEVICE"
fi
ANOTHER=""
while [ 1 ]; do
	echo -n "If there is a$ANOTHER patch disk to install, insert it in A: and type y [n]: "
	read ans;
	if [ "$ans" = "y" ]; then
		sysinstall -disk && echo "Patch disk installed..."
		ANOTHER=" another"
	else
		break;
	fi
done
sync
echo "Installation is complete.  Please reboot your computer now..."
if [ "/dev/ram /" != "`rdev`" ]; then
echo ""
echo "If you need to reboot using a floppy, do the following:"
echo '     - when you see the word "LILO", hold down the ALT key'
echo "     - type \"harddisk root=$ROOTDEVICE\" to boot to your installation"
fi
