#!/bin/sh

# This script installs the latest debootstrap in the current directory.

set -e

TMPDIR=`pwd`/tmp.$$

test ! -d $TMPDIR
mkdir $TMPDIR

DEBOOTSTRAP_VERSION=${DEBOOTSTRAP_VERSION:-`
    wget -o/dev/null -O- http://packages.debian.org/unstable/admin/debootstrap | \
	grep '<h1>Package' | \
	sed 's/.*(//; s/).*//'
`}
DEBOOTSTRAP_DEB=debootstrap_${DEBOOTSTRAP_VERSION}_all.deb

if [ ! -f $DEBOOTSTRAP_DEB ]; then
    wget -nc http://ftp.debian.org/debian/pool/main/d/debootstrap/$DEBOOTSTRAP_DEB
fi

if false && [ -x /usr/bin/dpkg ]; then
    dpkg -x $DEBOOTSTRAP_DEB $TMPDIR
else
    cd $TMPDIR
    ar x ../$DEBOOTSTRAP_DEB
    tar zxf data.tar.gz
    cd ..
fi

cp -PpR \
    $TMPDIR/usr/lib/debootstrap/* \
    $TMPDIR/usr/sbin/debootstrap \
    $TMPDIR/usr/share/man/man8/debootstrap.8.gz \
    $TMPDIR/usr/share/doc/debootstrap/* \
    .

sed 's@DEBOOTSTRAP_DIR=/usr/lib/debootstrap@DEBOOTSTRAP_DIR=`pwd`@' debootstrap \
    > debootstrap.tmp \
    && mv -f debootstrap.tmp debootstrap \
    && chmod +x debootstrap

rm -rf $TMPDIR
