#!/bin/sh

set -eu

PBUILDER_BUILD="/home/pbuilder.buexcl/build"

touch /testfile.$$
BUILD=$(dirname $PBUILDER_BUILD/*/testfile.$$)

if ! test -f "$BUILD/testfile.$$"; then
	echo "Could not determine our build directory in $PBUILDER_BUILD ($BUILD)"
	ls $PBUILDER_BUILD $PBUILDER_BUILD/*
	mount
	printenv | sort
	exit 1
fi
rm -f /testfile.$$

COPY="$PBUILDER_BUILD/${BUILD_TAG:-ftbfs.$$}"

echo "Preserving build chroot for inspection in $COPY"
cp -alx $BUILD $COPY

du -sh $COPY
