#!/bin/bash
#
# Install all dependencies for all tests for running in-tree.
# Installs then *REMOVE* all debs generated by this source package!
#
# With --our-debs-dir=DIR, instead removes all packages with debs in DIR
#
# For use in chroots.

set -e
set -o pipefail
shopt -s inherit_errexit # #514862, wtf

. tests/lib-core

x () { echo >&2 "+ $*"; "$@"; }

# Assume we'll have t2usm somehow, for these purposes.  That just means
# we don't skip the t2u-integration test and instead *do* install its deps.
deps=" $(
       export DGIT_TEST_T2USM_PROGRAM=/bin/false
       tests/enumerate-tests dep-packages
) "

case "$#.$1" in
    0.)
	our_pkgs=$(perl -ne 'print if s{^Package: }{}' debian/control)

	x apt-get -y install $our_pkgs
	;;
    1.--our-debs-dir=*)
	our_debs=$(find "${1#--our-debs-dir=}" -name '*.deb')

	our_pkgs=''; for p in $our_debs; do
	    p=${p##*/}
	    p=${p%%_*}
	    our_pkgs+=" $p"
	    deps="${deps// $p / }"
	done

	x apt-get -y install $our_debs
	;;
    *)
	fail "$0: bad usage"
	;;
esac

x apt-get -y install --no-install-recommends $deps eatmydata moreutils
x apt-get -y build-dep .
x apt-get -y remove $our_pkgs
