#!/bin/sh

. /usr/share/debconf/confmodule

set -e

dev=$1
id=$2
part=$dev/$id

cd $dev

[ -f $part/method -a -f $part/acting_filesystem ] || exit 0

method=$(cat $part/method)
filesystem=$(cat $part/acting_filesystem)

choice_mountpoint () {
    case "$filesystem" in
	ext2|fat16|fat32)
	    if [ -f $part/mountpoint ]; then
		mp=$(cat $part/mountpoint)
	    else
		db_metaget partman-basicfilesystems/text/no_mountpoint description
		mp="$RET"
	    fi
	    db_metaget partman-basicfilesystems/text/specify_mountpoint description
	    RET=$(stralign -25 "$RET")
	    printf "mountpoint\t%s%s\n" "$RET" "$mp"
	    ;;
    esac
}

choice_options () {
    case "$filesystem" in
	ext2)
	    if [ -f $part/mountoptions ]; then
		options=$(cat $part/mountoptions|sed 's/ //g')
	    else
		options=defaults
	    fi
	    db_metaget partman-basicfilesystems/text/options description
	    RET=$(stralign -25 "$RET")
	    printf "options\t%s%.45s\n" "$RET" "$options"
	    ;;
	fat16|fat32)
	    if [ -f $part/fatmountoptions ]; then
		options=$(cat $part/fatmountoptions|sed 's/ //g')
	    else
		options=defaults
	    fi
	    db_metaget partman-basicfilesystems/text/options description
	    RET=$(stralign -25 "$RET")
	    printf "fatoptions\t%s%.45s\n" "$RET" "$options"
	    ;;
    esac
}

choice_format_swap () {
    if 
        [ "$method" = swap -a -f $part/detected_filesystem ] \
        && [ "$(cat $part/detected_filesystem)" = linux-swap ]
    then
	db_metaget partman-basicfilesystems/text/format_swap description
	description=$(stralign -25 "$RET")
	if [ -f $part/format ]; then
	    db_metaget partman-basicfilesystems/text/yes description
	    printf "dont_format_swap\t%s%s\n" "$description" "${RET}"
	else
	    db_metaget partman-basicfilesystems/text/no description
	    printf "format_swap\t%s%s\n" "$description" "${RET}"
	fi
    fi
}

choice_mountpoint

choice_options

choice_format_swap
