#!/bin/sh

# This script sets method "efi" for all fat16 partitions that have the boot
# flag set.

# Give the kernel a chance to create /proc/efi if appropriate.
modprobe efivars >/dev/null 2>&1 || true

if [ ! -d /proc/efi ] && [ ! -d /sys/firmware/efi ]; then
    exit 0
fi

. /lib/partman/definitions.sh

for dev in /var/lib/partman/devices/*; do
    [ -d "$dev" ] || continue
    cd $dev
    partitions=
    open_dialog PARTITIONS
    while { read_line num id size type fs path name; [ "$id" ]; }; do
	if [ "$fs" = fat16 ]; then
	    partitions="$partitions $id"
	fi
    done
    close_dialog

    for id in $partitions; do
	efi=no
	open_dialog GET_FLAGS $id
	while { read_line flag; [ "$flag" ]; }; do
	    if [ "$flag" = boot ]; then
		efi=yes
                # can not break here
	    fi
	done
	close_dialog
	if [ "$efi" = yes ]; then
	    [ -d $id ] || mkdir $id
	    echo efi >$id/method
	fi
    done
done

