#!/bin/bash
set -e
. tests/lib

t-setup-import gbp

cd $p

t-tstunt-parsechangelog
t-tstunt-debuild
t-tstunt-lintian

git config --global diff.noprefix true

: '----- let gbp build a .orig for comparison -----'

gbp buildpackage --git-notify=off --git-no-sign-tags -us -uc

mkdir ../gbp-output
mv ../*1.0* ../gbp-output/.
rm -f ../*.changes

: '----- now do it ourselves -----'

t-dgit -wgf --dgit-view-save=split.b gbp-build --git-ignore-branch

t-dgit -wgf --quilt=gbp clean # gbp leaves dirty trees :-/

t-dgit -wgf --dgit-view-save=split.p --quilt=gbp push-built --new

t-gbp-pushed-good

: '----- check .origs are the same -----'

# if gbp weren't weird about .gitignore we could just debdiff the .dscs

for d in . gbp-output; do
	cd $tmp/$d
	mkdir tar-x
	cd tar-x
	tar zxf ../${p}_${v%-*}.orig.tar.gz
done

cd $tmp
diff -ruN gbp-output/tar-x tar-x
cd $p

: '----- test download-unfetched-origs mirror-fallback -----'

# Test procedure for actual live configuration:
#
#  * Look in https://incoming.debian.org/debian-buildd/pool
#    and find a package that exists there.  (`valgrind`, in this case).t
#  * Run
#     .../using-these dgit -cdgit-distro.debian.mirror=file://nonexistent/ \
#                          -D download-unfetched-origs
#  * Observe that it fails to find the file at file:// but then
#    successfuly obtains it from buildd incoming.

t-archive-process-incoming sid
rm ../*.orig.tar.gz

mirror_fallback=$tmp/mirror-fallback
mkdir $mirror_fallback
mv ../mirror/pool $mirror_fallback/.
mf_option="-cdgit-distro.test-dummy.mirror-fallback=file://$mirror_fallback/"

t-dgit $mf_option download-unfetched-origs

: '----- test mirror-fallback whole-archive -----'

git checkout -b other-suite-exp
perl -i -pe 's/\bunstable\b/experimental/ if $.==1' debian/changelog
git commit -m 'change suite' debian/changelog

rm ../*.orig.tar.gz
t-dgit $mf_option download-unfetched-origs

git checkout master
mv $mirror_fallback/pool ../mirror/.

t-ok
