#!	/bin/ash

echo $0 started OK

#set -x 

readonly bold='[1m'
readonly clear='[H[J'
readonly norm='[0;10m'

last ()
{
	eval echo $"$#"
}

find_partitions ()
{
	local type="$1"
	local line
	while true; do
		read line
		if [ "$line" = "-EOF-" ]; then
			return 0
		fi
		if [ "`last $line`" = "$type" ]; then
			set -- $line
			echo $1
		fi
	done
}


while true; do
	echo "























"
	echo $clear\
"This system has relatively little memory. For a system like this, special 
procedures are required, because there is not enough RAM available
to run the graphical program without the virtual memory provided by the swap
partition. Thus, you should:

1. Run the disk partitioning program to add a \"Linux swap\" (type 82)
   partition and the \"Linux native\" (type 83) partition. You have to create
   an extra temporary root partition that has at least 2MB and type 81 
    (\"Linux/MINIX\").
   You will not be able to re-partition the drive while the
   swap partition on it is active.
2. Then initialize the swap partition.
3. Then initialize the temporary root partition
4. Reboot.

Please select an action from the menu below:

1. Run the disk partitioning program in order to create a swap partition."
	if [ -n "$drive" ]; then
		echo "2. Initialize and activate the swap partition."
	fi
	if [ -n "$swappartition" ]; then
		echo "3. Initialize the temporary root partition."
	fi
	echo "4. Reboot."
	echo ""

	read action

	case "$action" in 
	1)
		echo $clear

		if [ -n "$swappartition" ]; then
			echo \
"You already activated the swap partition $swappartition.
It will be deactivated before running the partitioning program.

Type \"y\" to continue, type \"n\" to cancel: "
			read line
			case "$line" in 
			y|Y)
				swapoff $swappartition
				swappartition=""
				;;
			*)
				break 2
			esac
		fi

		echo "Please wait while disk drives are detected..."
		drives="`tryopen -w /dev/hd[a-z] /dev/sd[a-z]`"
		echo $clear
		if [ -z "$drives" ]; then
			echo \
"No hard disk drives could be found. Make sure they are cabled
correctly and are turned on before the system is started. You
may have to change driver settings when you start the system
with a command at the \"boot:\" prompt, or you may have to load
a driver that is in a loadable module to solve this problem."
			read foo
		else
			echo \
"The following drives are available. Drives that are named /dev/hdX
are IDE, ATA, MFM, or RLL drives. Drives that are named /dev/sdX are
SCSI drives."
			for d in $drives; do
				echo "	"$d
			done
			echo -n "
Please type a drive name from the above list: "
			read drive
# Note the -i flag to cfdisk is undocumented and liable to change.
			if [ -n "$drive" ]; then
				if [ -x /sbin/cfdisk ]; then
					cfdisk -i $drive
				else
					fdisk $drive
				fi
			fi
		fi
		;;
	2)
		echo -n $clear
		echo "Please wait while swap partitions are detected..."
		fdisk -l > /tmp/$$ 2>/dev/null
		echo "-EOF-" >>/tmp/$$
		partitions="`find_partitions swap < /tmp/$$`"
		rm -f /tmp/$$
		echo -n $clear
		if [ -z "$partitions" ]; then
			echo "
No swap partitions are available. You must select menu item 1 and create
one.

Please press enter when you are ready to continue."
			read line
		else
			echo "These swap partitions are available: "
			for p in $partitions; do
				echo "	"$p
			done
			echo -n "
Please type a partition name from the above list to use: "
			read swappartition
			if [ -z "$swappartition" ]; then
				continue
			fi
			echo -n "
This will permanently destroy all data on $swappartition. Type \"y\" to continue,
type \"n\" to cancel: "
			read line
			case "$line" in 
			y|Y)
				echo
				echo "Creating swap signature ..."
				mkswap $swappartition
				echo "        done."
				if [ $? -ne 0 ]; then
					"Error creating swap signature"
					read foo
					break 2
				fi
				echo "Enable swap signature ..."
				swapon $swappartition
				echo "        done."
				if [ $? -ne 0 ]; then
					"Error enabling swap signature"
					read foo
					break 2
				fi
				
				echo "
The swap partition $swappartition has been initialized successfully.

Please press enter when you are ready to continue."
				read line
				;;
			esac
		fi
		;;
	3)
		echo -n $clear
		echo
		echo "Please wait while minix partitions are detected..."
		fdisk -l > /tmp/$$ 2>/dev/null
		echo "-EOF-" >>/tmp/$$
		partitions="`find_partitions Linux/MINIX < /tmp/$$`"
		rm -f /tmp/$$
		echo -n $clear
		if [ -z "$partitions" ]; then
			echo "
No minix partitions are available. You must select menu item 1 and create
one.

Please press enter when you are ready to continue."
			read line
		else
			echo "These minix partitions are available: "
			for p in $partitions; do
				echo "	"$p
			done
			echo -n "
Please type a partition name from the above list to use: "
			read partition
			if [ -z "$partition" ]; then
				continue
			fi
			echo -n "
This will permanently destroy all data on $partition. Type \"y\" to continue,
type \"n\" to cancel: "
			read line
			case "$line" in 
			y|Y)
				echo
				echo "Copying root filesystem from floppy to $partition ..."
				cat /dev/fd0 >$partition
				echo "        done."
				if [ $? -ne 0 ]; then
					"Error copying root filesystem"
					read foo
				fi
				mount -t minix $partition /mnt
				rm /mnt/sbin/swapsetup
				echo $swappartition >/mnt/etc/swappartition
				umount /mnt
				echo "
Please press enter when you are ready to continue."
				read line
				;;
			esac
		fi
		;;
	4)
		echo "The system will reboot now.

When the system is shut down, insert the rescue disk and boot.

When the boot: prompt appears, type
$bold        lowmemboot root=$partition$norm


Please press enter to continue.
"
		read line
				
		echo
		echo "Rebooting ..."
		reboot
		while read foo; do
		    true
		done
		;;
	sh)
		sh
		;;
	esac
done
