#!/bin/sh

. /lib/partman/definitions.sh

abort () {
    if [ -f /var/run/parted_server.pid ]; then
	open_infifo
	write_line "QUIT"
	close_infifo
	rm /var/run/parted_server.pid
    fi
    exit $1
}

confirm_changes () {
    local dev x part partitions num id size type fs path name filesystem partitems items formatted_previously
    # Compute the changes we are going to do
    partitems=''
    items=''
    for dev in $DEVICES/*; do
	[ -d "$dev" ] || continue
	cd $dev

	open_dialog IS_CHANGED
	read_line x
	close_dialog
	if [ "$x" = yes ]; then
	    partitems="${partitems}   $(humandev $(cat device))
"
	fi

	partitions=
	open_dialog PARTITIONS
	while { read_line num id size type fs path name; [ "$id" ]; }; do
	    [ "$fs" != free ] || continue
	    partitions="$partitions $id,$num"
	done
	close_dialog
	
	formatted_previously=no
	for part in $partitions; do
	    id=${part%,*}
	    num=${part#*,}
            [ -f $id/method -a -f $id/format \
                -a -f $id/visual_filesystem ] || continue
	    # if no filesystem (e.g. swap) should either be not
            # formatted or formatted before the method is specified
            [ -f $id/filesystem -o ! -f $id/formatted \
                -o $id/formatted -ot $id/method ] || continue
	    # if it is already formatted filesystem it must be formatted 
	    # before the method or filesystem is specified
            [ ! -f $id/filesystem -o ! -f $id/formatted \
                -o $id/formatted -ot $id/method \
                -o $id/formatted -ot $id/filesystem ] ||
	            {
			formatted_previously=yes
			continue
		    }
	    filesystem=$(cat $id/visual_filesystem)
	    db_subst partman/text/confirm_item TYPE "$filesystem"
	    db_subst partman/text/confirm_item PARTITION "$num"
	    db_subst partman/text/confirm_item DEVICE $(humandev $(cat device))
	    db_metaget partman/text/confirm_item description
	    
	    items="${items}   ${RET}
"
	done
    done

    if [ "$items" ]; then
	db_metaget partman/text/confirm_item_header description
	items="$RET
$items"
    fi
    
    if [ "$partitems" ]; then
	db_metaget partman/text/confirm_partitem_header description
	partitems="$RET
$partitems"
    fi
    
    if [ "$partitems$items" ]; then
	if [ -z "$items" ]; then
	    x="$partitems"
	elif [ -z "$partitems" ]; then
	    x="$items"
	else
	    x="$partitems
$items"
	fi
	db_subst partman/confirm ITEMS "$x"
	db_input critical partman/confirm
	db_go || true
	db_get partman/confirm
	if [ "$RET" = false ]; then
	    db_reset partman/confirm
	    return 1
	else
	    db_reset partman/confirm
	    return 0
	fi
    else
	if [ "$formatted_previously" = no ]; then
	    db_input high partman/confirm_nochanges
	    db_go || true
	    db_get partman/confirm_nochanges
	    if [ "$RET" = false ]; then
	        db_reset partman/confirm_nochanges
		return 1
	    else
	        db_reset partman/confirm_nochanges
		return 0
	    fi
	else
	    return 0
	fi
    fi
}

db_capb backup

# Measure the width of partman/text/number here to make things faster.
# number_width is used only in visual.d/number
db_metaget partman/text/number description
RET=$(printf "$RET" '')
RET=$(stralign 50 "$RET" | sed 's/[^ ]//g')
number_width=$((2 + 50 - ${#RET}))
export number_width


# Here is maybe not a good place to set deci (TODO)
#db_metaget partman/text/deci description
#deci="$RET"
#export deci

# The comma has special meaning for debconf.  Lets force dot untill we
# discover where the comma has to be escaped..
deci='.'

#if [ -e /var/lib/partman ]; then 
#    rm -rf /var/lib/partman 
#fi

mkdir /var/lib/partman

while true; do
    
    initcount=`ls /lib/partman/init.d/* | wc -l`
    db_progress START 0 $initcount partman/progress/init/title
    for s in /lib/partman/init.d/*; do
	if [ -x $s ]; then
	    base=$(basename $s | sed 's/[0-9]*//')
	    echo $base >&2
	    # Not every init script has, or needs, its own progress
	    # template. Add them to slow init scripts only.
	    if ! db_progress INFO partman/progress/init/$base; then
		db_progress INFO partman/progress/init/fallback
	    fi
	    if ! $s; then
		db_progress STOP
		abort 10
	    fi
	fi
	db_progress STEP 1
    done
    db_progress STOP
    
    while true; do
	ask_user /lib/partman/choose_partition
	exitcode=$?
	if [ $exitcode -eq 255 ]; then
	    abort 10 # back up to main menu
	elif [ $exitcode -ge 100 ] && confirm_changes; then
	    break
	fi
    done

    for s in /lib/partman/commit.d/*; do
	if [ -x $s ]; then
	    $s || continue 2
	fi
    done
    
    for s in /lib/partman/finish.d/*; do
	if [ -x $s ]; then
	    $s || {
		status=$?
		if [ "$status" = 1 ]; then
		    continue 2
		else
		    abort $status
		fi
	    }
	fi
    done

    break
done    
