#!/bin/sh

# Finds debootstrap in $PATH, or download some version of it if not found.
# This script prefers to use local copies that are already downloaded, over
# system-wide installations, in case you want to test on some specific
# outdated version of debootstrap.

download_it() {
    curl http://deb.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.128+nmu2+deb12u2.tar.gz --output debootstrap.tar.gz
    tar xf debootstrap.tar.gz && rm debootstrap.tar.gz
}

LOCALDIR=`pwd`/debootstrap
if test -e "$LOCALDIR/debootstrap"
then
    echo "Found $LOCALDIR/debootstrap in current directory"

    # Run debootstrap without installing it to the system.
    DEBOOTSTRAP_DIR="$LOCALDIR" "$LOCALDIR/debootstrap" "$@"
elif IT=`command -v debootstrap`
then
    echo "Found $IT in PATH"

    # Use the version of debootstrap that was already installed.
    debootstrap "$@"
else
    echo "debootstrap not found. Downloading to $LOCALDIR."
    download_it

    # Run debootstrap without installing it to the system.
    DEBOOTSTRAP_DIR="$LOCALDIR" "$LOCALDIR/debootstrap" "$@"
fi

