#!/bin/sh
# Check that the boot partition is the 1st (primary) partition and that
# it is of type ext2 or ext3.

machine=$(grep "^Hardware" /proc/cpuinfo | sed 's/Hardware\s*:\s*//') || true
case "$machine" in
	"Buffalo Linkstation Pro/Live" | "Buffalo/Revogear Kurobox Pro")
	;;
	"GLAN Tank")
	;;
	"HP Media Vault mv2120")
	;;
	*)
		exit 0
	;;
esac

. /lib/partman/lib/base.sh

for dev in $DEVICES/*; do
	[ -d "$dev" ] || continue
	cd $dev
	open_dialog PARTITIONS
	while { read_line num id size type fs path name; [ "$id" ]; }; do
		[ "$fs" != free ] || continue
		[ -f $id/method ] || continue
		[ -f $id/acting_filesystem ] || continue
		[ -f $id/mountpoint ] || continue
		mountpoint=$(cat $id/mountpoint)
		filesystem=$(cat $id/acting_filesystem)
		if [ "$mountpoint" = / ]; then
			root_fs=$filesystem
			root_type=$type
			root_path=$path
		elif [ "$mountpoint" = /boot ]; then
			boot_fs=$filesystem
			boot_type=$type
			boot_path=$path
		fi
	done
	close_dialog
done

# If no separate boot partition exists root acts as boot
if [ -z "$boot_path" ]; then
	boot_fs=$root_fs
	boot_type=$root_type
	boot_path=$root_path
fi

# We need an ext2 or ext3 filesystem to boot
if [ "$boot_fs" != ext2 ] && [ "$boot_fs" != ext3 ]; then
	db_set partman-ext3/boot_not_ext2_or_ext3 true
	db_input critical partman-ext3/boot_not_ext2_or_ext3 || true
	db_go || true
	db_get partman-ext3/boot_not_ext2_or_ext3
	if [ "$RET" = true ]; then
		exit 1
	fi
fi

# The boot file system has to be the first primary partition
part_num=$(echo "$boot_path" | sed -e 's/.*[^0-9]\+//')
if [ "$boot_type" != primary ] || [ "$part_num" -ne 1 ]; then
	db_set partman/boot_not_first_partition true
	db_input critical partman/boot_not_first_partition || true
	db_go || true
	db_get partman/boot_not_first_partition
	if [ "$RET" = true ]; then
		exit 1
	fi
fi

