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

ARCH="$(archdetect)"

case $ARCH in
    arm*)
	machine=$(grep "^Hardware" /proc/cpuinfo | sed 's/Hardware\s*:\s*//') || true
	case "$machine" in
	    "Buffalo Linkstation Pro/Live" | "Buffalo/Revogear Kurobox Pro" \
		    | "Buffalo Linkstation Mini")
		;;
	    "GLAN Tank")
		;;
	    "HP Media Vault mv2120")
		;;
	    *)
		exit 0
		;;
	esac
	;;
    mipsel/loongson-2f)
	;;
    *)
	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
			if [ -f $id/bootable ]; then
				root_bootable=yes
			fi
		elif [ "$mountpoint" = /boot ]; then
			boot_fs=$filesystem
			boot_type=$type
			boot_path=$path
			if [ -f $id/bootable ]; then
				boot_bootable=yes
			fi
		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
	boot_bootable=$root_bootable
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

# ... but for now don't run the check if /boot is on RAID1.  Right now,
# there's no good way to map a RAID device to its physical partition
# and then run the checks on that partition (see #509799).
if echo $boot_path | grep -q "^/dev/md"; then
	raid=${boot_path#/dev/}
	if grep "^$raid" /proc/mdstat | grep -q " raid1 "; then
		exit 0
	fi
fi

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

# Make sure that the boot partition is marked as bootable
## Note: this won't work for RAID because the physical partition must be
## marked bootable whereas we're checking the /boot partition (i.e. the
## md partition on top of the physical partition).  However, there's
## some code above that exists this script when /boot is on RAID1.
if [ "$ARCH" != mipsel/loongson-2f ] && [ "$boot_bootable" != "yes" ]; then
	db_set partman-ext3/boot_not_bootable true
	db_input critical partman-ext3/boot_not_bootable || true
	db_go || true
	db_get partman-ext3/boot_not_bootable
	if [ "$RET" = true ]; then
		exit 1
	fi
fi

