#!/bin/sh

. /lib/partman/lib/base.sh

ARCH="$(archdetect)"
case $ARCH in
    powerpc/powermac_newworld)
	;;
    *)
	exit 0
	;;
esac

have_newworld=no
good_size=yes

# Is there at least one new-world 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" = hfs ] || continue
		partitions="$partitions $id,$size,$path"
	done
	close_dialog
 
	for part in $partitions; do
		id="${part%%,*}"
		[ -f $id/method ] || continue
		method=$(cat $id/method)
		if [ "$method" = newworld ]; then
			cdr="${part#*,}"
			size="${cdr%%,*}"
			have_newworld=yes
			if longint_le "$size" 819199; then
				good_size=no
			else
				path="${cdr#*,}"
				good_paths="${good_paths:+$good_paths,}$path"
			fi
		fi
	done
done

if [ "$have_newworld" = no ]; then
	db_input critical partman-newworld/no_newworld || true
	db_go || exit 1
	db_get partman-newworld/no_newworld
	if [ "$RET" = true ]; then
		exit 1
	fi
elif [ "$good_size" = no ]; then
	db_input critical partman-newworld/wrong_size || true
	db_go || exit 1
	db_get partman-newworld/wrong_size
	if [ "$RET" = true ]; then
		exit 1
	fi
fi

db_set partman-newworld/boot_partitions "$good_paths"

exit 0
