#!/bin/sh

. /usr/share/debconf/confmodule

dev=$1
id=$2
part=$dev/$id
filesystem=$(cat $part/acting_filesystem)
os=$(udpkg --print-os)
tpl=partman-basicfilesystems/mountoptions

optionsfile=''
if [ -f /lib/partman/mountoptions/$filesystem.$os ] ; then
	optionsfile="/lib/partman/mountoptions/$filesystem.$os"
else
	optionsfile="/lib/partman/mountoptions/$filesystem"
fi

cd $dev

full_options=''
all_options=''
descriptions=''
for op in $(cat $optionsfile); do
	if db_metaget partman-basicfilesystems/text/$op description && \
	   [ "$RET" ]; then
		all_options="${all_options:+$all_options, }$op"
		descriptions="${descriptions:+$descriptions, }$RET"
	else
		logger -t partman "Error: no description for mount option $op found; skipping"
		break
	fi

	if [ -f $part/options/$op ]; then
		full_options="${full_options:+$full_options,}$(cat $part/options/$op)"
	fi
done

db_subst $tpl options "$all_options"
db_subst $tpl descriptions "$descriptions"
db_set $tpl "$full_options"
db_input critical $tpl || true
db_go || exit

db_get $tpl
rm -rf $part/options
mkdir $part/options
IFS=,
for op in $RET; do
	op=${op# }
	echo "$op" >$part/options/${op%% *}
done
