#!/bin/sh

. /lib/partman/lib/base.sh

have_efi=no
new_efi_size=

# Is there at least one efi-partition?
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
		[ "$fs" != free ] || continue
		partitions="$partitions $id,$num,$size"
	done
	close_dialog

	for part in $partitions; do
		id=${part%%,*}
		part=${part##*,}
		num=${part%%,*}
		size=${part#*,}
		[ -f $id/method ] || continue
		method=$(cat $id/method)
		if [ "$method" = efi ] && [ -f $id/bootable ]; then
			have_efi=yes
			if [ -f $id/format ] && [ -z "$new_efi_size" ]; then
				new_efi_size="$size"
			fi
		fi
	done
done

if [ -e /var/lib/partman/efi ] && \
   [ "$have_efi" = no ]; then
	db_input critical partman-efi/no_efi || true
	db_go || exit 1
	db_get partman-efi/no_efi
	if [ "$RET" = true ]; then
		exit 1
	fi
fi

ARCH="$(archdetect)"

# Experimentally-verified minimum size for a FAT32 filesystem created using
# libparted.
if [ "$new_efi_size" ] && longint_le "$new_efi_size" 34091007; then
	db_input critical partman-efi/too_small_efi || true
	db_go || true
	exit 1
fi

exit 0
