#!/bin/sh

. /lib/partman/lib/base.sh

dev=$2
id=$3
part=$dev/$id

cd $dev

[ -f $part/method -a -f $part/acting_filesystem ] || exit 0
filesystem=$(cat $part/acting_filesystem)

case $1 in
    mountpoint)
	if select_mountpoint $dev $id; then
		update_partition $dev $id
	fi
	;;
    options)
	select_mountoptions $dev $id
	;;
    label)
	label=''
	if [ -f $part/label ]; then
		label=$(cat $part/label)
	fi
	db_set partman-basicfilesystems/choose_label "$label"
	db_input critical partman-basicfilesystems/choose_label || true
	db_go || exit 1
	db_get partman-basicfilesystems/choose_label
	if [ "$RET" ]; then
		echo "$RET" >$part/label
	else
		rm -f $part/label
	fi
	db_reset partman-basicfilesystems/choose_label
	;;
    reserved_for_root)
	if [ -f $part/reserved_for_root ]; then
		reserved=$(cat $part/reserved_for_root)
	else
		reserved=5
	fi
	db_set partman-basicfilesystems/specify_reserved "$reserved%"
	db_input critical partman-basicfilesystems/specify_reserved || true
	db_go || exit 1
	db_get partman-basicfilesystems/specify_reserved
	RET=$(expr "$RET" : '\([0-9][0-9]\?\)\([,. %].*\)\?$')
	if [ "$RET" ]; then
		echo "$RET" >$part/reserved_for_root
	else
		rm -f $part/reserved_for_root
	fi
	db_reset partman-basicfilesystems/specify_reserved
	;;
    usage)
	db_metaget partman-basicfilesystems/text/typical_usage description
	typical_usage="$RET"
	if [ -f $part/usage ]; then
		usage=$(cat $part/usage)
	else
		usage="$typical_usage"
	fi
	db_subst partman-basicfilesystems/specify_usage CHOICES "$typical_usage, news, largefile, largefile4"
	db_set partman-basicfilesystems/specify_usage "$usage"
	db_input critical partman-basicfilesystems/specify_usage || true
	db_go || exit 1
	db_get partman-basicfilesystems/specify_usage
	if [ "$RET" != "$typical_usage" ]; then
		echo "$RET" >$part/usage
	else
		rm -f $part/usage
	fi
	;;
esac

exit 0
