#!/bin/sh

. /usr/share/debconf/confmodule
. /lib/partman/definitions.sh

newlayout_exit() {
    if [ "$code" = 255 ]; then
	rm -f /var/lib/partman/initial_auto
	exit 1
    elif [ "$code" -ge 100 ]; then
	exit $code
    fi
}

# copy from auto.d/initial_auto stripped of comments...
dev_to_partman () {
    local dev_name="$1"
    local mapped_dev_name="$(mapdevfs $dev_name)"
    if [ -n "$mapped_dev_name" ]; then
	dev_name="$mapped_dev_name"
    fi
    for dev in $DEVICES/*; do
	if [ "$(mapdevfs $(cat $dev/device))" = "$dev_name" ]; then
	    echo $dev
	fi
    done
}

ask_for_disk() {
    choices=$(
	for dev in $DEVICES/*; do
	    # raw devices
	    device="$(cat $dev/device)"
	    case $device in
		(/dev/mapper/*-*)
		    continue
		    ;;
	    esac
	    printf "%s\t%s: %s\n" "$device" "$device" "$(device_name $dev)"
	    # calculate biggest free
	    [ -d $dev ] || continue
	    cd $dev
	    open_dialog PARTITIONS
	    while { read_line num id size type fs path name; [ "$id" ]; }; do
		if [ "$fs" = free ] && ! longint_le $size $mysize; then
		    mysize=$size
		    mypart=$dev//$id
		fi
	    done
	    close_dialog
	    # end of biggest free
	done
	# add a separator
	printf " \n"
	# add biggest free to menu selection
	if [ "$mypart" ]; then
	    db_metaget partman-auto/text/use_biggest_free description
	    printf "biggest_free: %s\t%s\n" "$mypart" "$RET"
	fi
	# add custom to menu selection
	db_metaget partman-auto/text/custom_partitioning description
	printf "custom\t%s\n" "$RET"
    )
    code=0
    targetdevice=""
    while [ -z "$targetdevice" ]; do
	debconf_select critical partman-auto/select_disk "$choices" "custom" || code=$?
	newlayout_exit
	target="${RET%%: *}"
	bigfree="${RET#*: }"
	case "$target" in
	    custom)
		exit 0
		;;
	    biggest_free)
		# auto select biggest_free. kthxbye
		/lib/partman/automatically_partition/50biggest_free/do_option $bigfree
		exit 100
		;;
	    *)
		targetdevice="$(dev_to_partman $target)"
		;;
	esac
    done
}

STATE=1

# check how many devices
count=0
firstdev=
for devs in $DEVICES/*; do
    [ -d "$devs" ] || continue
    device="$(cat $devs/device)"
    case $device in
	/dev/mapper/*-*)
	    continue
	    ;;
    esac
    [ "$firstdev" ] || firstdev="$devs"
    count=$(($count + 1))
done

# If there is only one device, we autoselect the device, skip the question,
# and jump to the next.
if [ $count -le 1 ]; then
  STATE=2
  targetdevice="$firstdev"
fi

while true; do
    case "$STATE" in
	1)
	    ask_for_disk
	    # we can safely increase because ask_for_disk either
	    # returns a device or exits from this script.
	    STATE=$(($STATE + 1))
	    ;;
	2)
	    code=0
	    ask_user /lib/partman/automatically_partition "$targetdevice" || code=$?
	    echo "partman-auto/automatically_partition" > /lib/partman/automatically_partition/question
	    if [ "$code" = 255 ]; then
		if [ $count -le 1 ]; then
		    rm -f /var/lib/partman/initial_auto
		    exit 1
		else
		    STATE=$(($STATE - 1))
		fi
	    elif [ "$code" -ge 100 ]; then
		exit $code
	    elif [ "$code" = 99 ]; then
		# backup from do_option script
		continue
	    else
		STATE=$(($STATE + 1))
	    fi
	    ;;
	*)
	    break
	    ;;
    esac
done
