#!/bin/sh -e

. /lib/partman/definitions.sh

# Verify that /boot is on an ext2/3 partition on a disk using Sun
# disklabels, so that the system will actually be bootable.

get_silo_root_boot() {
	(for i in /lib/partman/fstab.d/*; do
		[ -x "$i" ] || continue
		$i
	done) | 
	while read fs mp type options dump pass
	do
		if [ "$mp" = / ]; then
			echo "root_type=$type; root_fs=$fs;"
		elif [ "$mp" = /boot ]; then
			echo "boot_type=$type; boot_fs=$fs;"
		fi
	done
}

eval "$(get_silo_root_boot)"

if [ -z "$boot_fs" ]
then
	boot_fs=$root_fs
	boot_type=$root_type
fi

disk=
for dev in $DEVICES/*
do
	[ -d "$dev" ] || continue
	cd $dev
	partitions=
	open_dialog PARTITIONS
	while { read_line num id size type fs path name; [ "$id" ]; }; do
		if [ -d "$id" ] && [ "$path" = "$boot_fs" ]; then
			ourdev=$dev
			disk="$(cat ${dev}/device)"
			break
		fi
	done
	close_dialog

	# We got /boot or /. Hooray!
	if [ -n "$disk" ]; then
		# /boot or / goes past the 1024cyl barrier
		open_dialog GET_CHS "$id"
		read_line start_cyl start_head start_sector end_cyl end_head end_sector
		close_dialog

		# Only sparc32 suffers from this problem though.
		if [ "$end_cyl" -gt 1024 ] && [ "$(archdetect)" = sparc/sparc32 ]; then
			db_subst silo-installer/boot-after-1024cyl CYL "$end_cyl"
			db_input critical silo-installer/boot-after-1024cyl
			db_go
			db_get silo-installer/boot-after-1024cyl
			# Bail out
			if [ "$RET" = false ]; then
				exit 1
			fi
		fi
		break
	fi
done

# Make sure the disk is using a Sun disklabel or nothing will boot!

label=$(parted "$disk" print | sed -n 's/Disk label type: //p')
if [ "$label" != sun ]; then
	db_input critical silo-installer/non-sun-partitions
	db_go
	db_get silo-installer/non-sun-partitions
	if [ "$RET" = false ]; then
		exit 1
	fi
fi

# SILO only groks ext2/3.

if [ "$boot_type" != ext2 ] && [ "$boot_type" != ext3 ]
then
	db_subst silo-installer/non-ext2-boot PARTTYPE $boot_type 
	db_input critical silo-installer/non-ext2-boot
	db_go
	db_get silo-installer/non-ext2-boot
	if [ "$RET" = false ]; then
		exit 1
	fi
fi
