#! /bin/sh -e

. /usr/share/debconf/confmodule

log=/var/log/messages
template=autopartkit/partition_table

log() {
    logger -t autopartkit "$@"
}

run_parts() {
    partsdir="$1"

    for script in "$partsdir"/*; do
	if [ -x "$script" ] ; then
	    if "$script" "$template" ; then
		:
	    else
		log "error: Fragment '$script' returned an error: $?."
	    fi
	else
	    log "error: Fragment '$script' is not executable."
	fi
    done
}
# Try to load LVM support
depmod -a >> $log 2>&1 || true
modprobe lvm-mod >> $log 2>&1 || true

# Make sure it is installed into /target/ as well
apt-install lvm10 || true

# Run all scripts to choose table file
run_parts /usr/lib/autopartkit.d

tablefile=`debconf-get $template 2>&1`

if [ -z "$tablefile" -o ! -f "$tablefile" ] ; then
    log "Unable to find table file '$tablefile'"
    exit 1
fi

if autopartkit "$tablefile" ; then
    log "info: autoparkit ran successfully using table file '$tablefile'."
else
    # This should use debconf to report the error.
    log "error: autopartkit failed."
    exit 1
fi

log "info: creating /target/etc/fstab."
/usr/lib/partconf/mkfstab

##DEBHELPER##
