#!/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
}

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]*//')
	    # 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
    
    skip_choose_partition=no
    for s in /lib/partman/auto.d/*; do
	if [ -x $s ]; then
	    $s
	    exitcode=$?
	    if [ $exitcode -ge 100 ]; then
		# Partitioning complete; go straight to confirmation.
		skip_choose_partition=yes
	    elif [ $exitcode -ne 0 ]; then
		abort 10
	    fi
	fi
    done

    if [ "$skip_choose_partition" != yes ]; then
	while true; do
	    ask_user /lib/partman/choose_partition
	    exitcode=$?
	    [ $exitcode -ge 100 ] && break
	done
    fi
    
    if [ $exitcode -eq 255 ]; then
	abort 10
    fi
    
    # Compute the changes we are going to do
    items=''
    for dev in $DEVICES/*; do
	[ -d "$dev" ] || continue
	cd $dev
	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
	
	for part in $partitions; do
	    id=${part%,*}
	    num=${part#*,}
            [ -f $id/method -a -f $id/format \
                -a -f $id/visual_filesystem ] || continue
            [ -f $id/filesystem -o ! -f $id/formatted \
                -o $id/formatted -ot $id/method ] || continue
            [ ! -f $id/filesystem -o ! -f $id/formatted \
                -o $id/formatted -ot $id/method \
                -o $id/formatted -ot $id/filesystem ] || 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
    
    db_reset partman/confirm
    db_subst partman/confirm ITEMS "$items"
    db_input critical partman/confirm
    db_go || true
    db_get partman/confirm
    if [ "$RET" = false ]; then
	    abort 10 # back up to main menu
    fi
    
    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    
