#!/bin/sh

. /lib/partman/definitions.sh

have_prep=no
good_place=no

# Is there at least one prep-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,$path"
    done
    close_dialog
    
    for part in $partitions; do
	id=${part%,*}
	part=${part#*,}
	[ -f $id/method ] || continue
	method=$(cat $id/method)
	if [ "$method" = prep ]; then
	    end=${id#*-}
	    have_prep=yes
	    if longint_le "$end" 8589934592; then
		good_place=yes
		good_paths="${good_paths:+$good_paths,}$path"
	    fi
	fi
    done
    
done

if [ $good_place = no ]; then
    if [ $have_prep = no ]; then
	db_fset partman-prep/no_prep seen false
	db_input critical partman-prep/no_prep || true
	db_go || exit 1
	db_get partman-prep/no_prep
	if [ "$RET" = 'true' ]; then
	    exit 1
	fi
    else
	db_fset partman-prep/wrong_place seen false
	db_input critical partman-prep/wrong_place || true
	db_go || exit 1
	db_get partman-prep/wrong_place
	if [ "$RET" = 'true' ]; then
	    exit 1
	fi
    fi
fi

db_set partman-prep/boot_partitions "$good_paths"

exit 0
