#!/bin/sh

DEBCONF_FRONTEND=Noninteractive
export DEBCONF_FRONTEND

if test -n "$exclude"; then
    exclude=`echo $exclude | tr ' ' ,`
    opts="--exclude=$exclude"
fi

if test -n "$include"; then
    include=`echo $include | tr ' ' ,`
    opts="$opts --include=$include"
fi

if test -n "$basedebs"; then
    opts="$opts --unpack-tarball $basedebs"
fi

debootstrap $opts $dist $TARGET $mirror $script

echo "deb $mirror $dist main" > $TARGET/etc/apt/sources.list

rm -f $TARGET/var/lib/apt/lists/debootstrap.*

if test -z "$keepdebs"; then
    rm -f $TARGET/var/cache/apt/archives/*.deb
fi

echo $purge | tr ' ' '\n' | xargs --no-run-if-empty -t \
    dpkg --root=$TARGET --purge


if test -n "$install"; then
    chroot $TARGET apt-get update
    chroot $TARGET apt-get install $install
fi
