#!/bin/sh -e

set -e

. /lib/partman/lib/base.sh

dev=${1%//*}
id=${1#*//}

cd $dev
device=$(humandev $(cat device))

# If the user wants to modify a device or partition
# the device may not be locked
if [ -e "$dev/locked" ]; then
	locked=$(cat "$dev/locked")
	db_subst partman-base/devicelocked DEVICE "$device"
	db_subst partman-base/devicelocked MESSAGE "$locked"
	db_set partman-base/devicelocked false
	db_input critical partman-base/devicelocked
	db_capb
	db_go || true
	db_capb backup align
	exit 0
fi

# Two scenarios to check for here:
# 1) If the user wants to modify a partition - it may not be locked
# 2) If the user wants to modify a device - none of its partitions may be locked
open_dialog PARTITIONS
while { read_line num tmpid size type fs path name; [ "$tmpid" ]; }; do
	if [ -n "$id" ]; then
		[ "$id" = "$tmpid" ] || continue
	fi

	if [ -e "$dev/$tmpid/locked" ]; then
		locked=$(cat "$dev/$tmpid/locked")
		db_subst partman-base/partlocked DEVICE "$device"
		db_subst partman-base/partlocked PARTITION "$num"
		db_subst partman-base/partlocked MESSAGE "$locked"
		db_set partman-base/partlocked false
		db_input critical partman-base/partlocked
		db_capb
		db_go || true
		db_capb backup align
		close_dialog
		exit 0
	fi
done
close_dialog


if [ -z "$id" ]; then
	# ask_user /lib/partman/storage_device "$dev" "$id" || true
	open_dialog GET_LABEL_TYPE
	read_line x
	close_dialog
	# do not try to create partition table on sw RAID device or LVM LV
	if [ "$x" = loop ]; then
		exit 0
	fi
	mklabel=$(echo /lib/partman/storage_device/[0-9][0-9]label/do_option)
	[ -x "$mklabel" ] || exit 0
	$mklabel label "$dev" || true
	exit 0
else
	open_dialog PARTITION_INFO $id
	read_line num id size type fs path name
	close_dialog
	[ "$id" ] || exit 0
	case "$fs" in
	    free)
		ask_user /lib/partman/free_space "$dev" "$id" || true
		;;
	    *)
		while true; do
			set +e
			device="$(humandev $(cat device))"
			db_subst partman/active_partition DEVICE "$device"
			db_subst partman/active_partition PARTITION "$num"
			if [ -f  $id/detected_filesystem ]; then
				filesystem=$(cat $id/detected_filesystem)
				RET=''
				db_metaget partman/filesystem_long/"$filesystem" description || RET=''
				if [ "$RET" ]; then
					filesystem="$RET"
				fi
				db_subst partman/text/there_is_detected FILESYSTEM "$filesystem"
				db_metaget partman/text/there_is_detected description
			else
				db_metaget partman/text/none_detected description
			fi
			db_subst partman/active_partition OTHERINFO "${RET}"

			if [ -f $id/detected_filesystem ] && [ -f $id/format ]; then
				db_metaget partman/text/destroyed description
				db_subst partman/active_partition DESTROYED "${RET}"
			else
				db_subst partman/active_partition DESTROYED ''
			fi

			ask_user /lib/partman/active_partition "$dev" "$id"
			exitcode="$?"
			if [ "$exitcode" -ge 100 ]; then
				break
			fi
			set -e
		done
		;;
	esac
fi
