#!/bin/sh -e

set -e

. /lib/partman/definitions.sh

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

cd $dev

if [ -z "$id" ]; then
#    ask_user /lib/partman/storage_device "$dev" "$id" || true
    open_dialog GET_DISK_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 -a -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
