set -u
. "$TESTSDIR"/test.inc

mkdir conf
mkdir package-1.0
mkdir package-1.0/debian
cat >package-1.0/debian/control <<END
Source: package
Section: sound
Priority: extra
Maintainer: me <me@example.org>
Standards-Version: 0.0

Package: rumsrumsrums
Architecture: all
Description: a package
 .

Package: dumdidum
Architecture: another
Description: a package not build
 .

Package: troettroet
Architecture: abacus
Description: some test-package
 .
END
cat >package-1.0/debian/changelog <<END
package (1.0-1) test; urgency=critical

  * first version

 -- me <me@example.orgguess@who>  Mon, 01 Jan 1980 01:02:02 +0000
END

dpkg-source -b package-1.0

cat > conf/distributions <<EOF
Codename: bla
Suite: test
Components: main
Architectures: source abacus another
Tracking: all

Codename: blub
Components: main
Architectures: notinbla

Codename: oses
Components: main
Architectures: source abacus kfreebsd-abacus hurd-abacus
Tracking: all
EOF
cat >> conf/options <<EOF
export silent-never
EOF

# to get databases:
testrun - rereference 3<<EOF
stdout
$(odb)
-v1*=Referencing oses...
-v3*=Unlocking dependencies of oses|main|abacus...
-v3*=Referencing oses|main|abacus...
-v2=Rereferencing oses|main|abacus...
-v3*=Unlocking dependencies of oses|main|kfreebsd-abacus...
-v3*=Referencing oses|main|kfreebsd-abacus...
-v2=Rereferencing oses|main|kfreebsd-abacus...
-v3*=Unlocking dependencies of oses|main|hurd-abacus...
-v3*=Referencing oses|main|hurd-abacus...
-v2=Rereferencing oses|main|hurd-abacus...
-v3*=Unlocking dependencies of oses|main|source...
-v3*=Referencing oses|main|source...
-v2=Rereferencing oses|main|source...
-v1*=Referencing bla...
-v3*=Unlocking dependencies of bla|main|abacus...
-v3*=Referencing bla|main|abacus...
-v2=Rereferencing bla|main|abacus...
-v3*=Unlocking dependencies of bla|main|another...
-v3*=Referencing bla|main|another...
-v2=Rereferencing bla|main|another...
-v3*=Unlocking dependencies of bla|main|source...
-v3*=Referencing bla|main|source...
-v2=Rereferencing bla|main|source...
-v1*=Referencing blub...
-v3*=Unlocking dependencies of blub|main|notinbla...
-v3*=Referencing blub|main|notinbla...
-v2=Rereferencing blub|main|notinbla...
EOF

testrun empty build-needing test abacus
testrun empty build-needing test all
testrun empty build-needing test any

testrun - includedsc test package_1.0-1.dsc 3<<EOF
stderr
-v1*=package_1.0-1.dsc: component guessed as 'main'
stdout
-v2*=Created directory "./pool"
-v2*=Created directory "./pool/main"
-v2*=Created directory "./pool/main/p"
-v2*=Created directory "./pool/main/p/package"
$(ofa 'pool/main/p/package/package_1.0-1.dsc')
$(ofa 'pool/main/p/package/package_1.0-1.tar.gz')
$(opa 'package' '1.0-1' 'bla' 'main' 'source' 'dsc')
$(ota 'bla' 'package')
EOF
rm package_1.0*

testrun - build-needing test another 3<<EOF
stdout
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc
EOF
testrun - build-needing test abacus 3<<EOF
stdout
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc
EOF
testrun - build-needing test all 3<<EOF
stdout
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc
EOF
testrun - build-needing test any 3<<EOF
stdout
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc all
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc another
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc abacus
EOF
testrun - build-needing test source 3<<EOF
stderr
*=Error: Architecture 'source' makes no sense for build-needing!
-v0*=There have been errors!
returns 255
EOF
testrun - build-needing test mistake 3<<EOF
stderr
*=Error: Architecture 'mistake' is not known!
-v0*=There have been errors!
returns 255
EOF
testrun - build-needing test notinbla 3<<EOF
stderr
*=Error: Architecture 'notinbla' not found in distribution 'bla'!
-v0*=There have been errors!
returns 255
EOF

mkdir package-1.0/debian/tmp
mkdir package-1.0/debian/tmp/DEBIAN
mkdir -p package-1.0/debian/tmp/usr/share/sounds
touch package-1.0/debian/tmp/usr/share/sounds/krach.wav
cd package-1.0
dpkg-gencontrol -prumsrumsrums
dpkg --build debian/tmp ..
cd ..

testrun - -C main includedeb test rumsrumsrums_1.0-1_all.deb 3<<EOF
stderr
stdout
$(ofa 'pool/main/p/package/rumsrumsrums_1.0-1_all.deb')
$(opa 'rumsrumsrums' '1.0-1' 'bla' 'main' 'abacus' 'deb')
$(opa 'rumsrumsrums' x 'bla' 'main' 'another' 'deb')
EOF

testrun - build-needing test another 3<<EOF
stdout
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc
EOF
testrun - build-needing test abacus 3<<EOF
stdout
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc
EOF
testrun - build-needing test any 3<<EOF
stdout
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc another
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc abacus
EOF
testrun empty build-needing test all

cd package-1.0
dpkg-gencontrol -ptroettroet
dpkg --build debian/tmp ..
cd ..

testrun - -C main includedeb test troettroet_1.0-1_abacus.deb 3<<EOF
stderr
stdout
$(ofa 'pool/main/p/package/troettroet_1.0-1_abacus.deb')
$(opa 'troettroet' x 'bla' 'main' 'abacus' 'deb')
EOF

testrun - build-needing test another 3<<EOF
stdout
*=package 1.0-1 pool/main/p/package/package_1.0-1.dsc
EOF
testrun - build-needing test abacus 3<<EOF
stdout
EOF

# Include a fake .log file to tell reprepro that architecture is done:

echo "There was nothing to do on this architecture!" > package_1.0-1_another.log
echo "package_1.0-1_another.log - -" > package-1.0/debian/files
cd package-1.0
dpkg-genchanges -B > ../package_1.0-1_another.changes
cd ..

# work around dpkg-dev 1.18.15+ no longer adding Architectures of log files:
if ! grep -q -s '^Architecture:' package_1.0-1_another.changes ; then
	sed -e 's/^Version:/Architecture: another\n&/' -i package_1.0-1_another.changes
fi

testrun - -C main include test package_1.0-1_another.changes 3<<EOF
stderr
*=Ignoring log file: 'package_1.0-1_another.log'!
*=package_1.0-1_another.changes: Not enough files in .changes!
-v0*=There have been errors!
returns 255
EOF

sed -i -e 's/Tracking: all/Tracking: all includelogs/' conf/distributions

testrun - -C main include test package_1.0-1_another.changes 3<<EOF
stderr
stdout
$(ofa 'pool/main/p/package/package_1.0-1_another.log')
EOF

testrun empty build-needing test another
testrun empty build-needing test abacus

# TODO: add a new version of that package...
rm -r package-1.0

mkdir onlyonearch-1.0
mkdir onlyonearch-1.0/debian
cat >onlyonearch-1.0/debian/control <<END
Source: onlyonearch
Section: something
Priority: extra
Maintainer: me <me@example.org>
Standards-Version: 0.0

Package: onearch
Architecture: abacus
Description: some test-onlyonearch
 .
END
cat >onlyonearch-1.0/debian/changelog <<END
onlyonearch (1.0-1) test; urgency=critical

  * first version

 -- me <me@example.orgguess@who>  Mon, 01 Jan 1980 01:02:02 +0000
END
dpkg-source -b onlyonearch-1.0
mkdir onlyonearch-1.0/debian/tmp
mkdir onlyonearch-1.0/debian/tmp/DEBIAN
mkdir -p onlyonearch-1.0/debian/tmp/usr/bin
touch onlyonearch-1.0/debian/tmp/usr/bin/program
cd onlyonearch-1.0
dpkg-gencontrol -ponearch
dpkg --build debian/tmp ..
cd ..
rm -r onlyonearch-1.0

testrun - --delete includedsc test onlyonearch_1.0-1.dsc 3<<EOF
stderr
-v1*=onlyonearch_1.0-1.dsc: component guessed as 'main'
stdout
-v2*=Created directory "./pool/main/o"
-v2*=Created directory "./pool/main/o/onlyonearch"
$(ofa 'pool/main/o/onlyonearch/onlyonearch_1.0-1.dsc')
$(ofa 'pool/main/o/onlyonearch/onlyonearch_1.0-1.tar.gz')
$(opa 'onlyonearch' x 'bla' 'main' 'source' 'dsc')
$(ota 'bla' 'onlyonearch')
EOF

testrun empty build-needing test another
testrun empty build-needing test all
testrun - build-needing test abacus 3<<EOF
stdout
*=onlyonearch 1.0-1 pool/main/o/onlyonearch/onlyonearch_1.0-1.dsc
EOF

testrun - build-needing test any 3<<EOF
stdout
*=onlyonearch 1.0-1 pool/main/o/onlyonearch/onlyonearch_1.0-1.dsc abacus
EOF

testrun - --delete -C main includedeb test onearch_1.0-1_abacus.deb 3<<EOF
stderr
stdout
$(ofa 'pool/main/o/onlyonearch/onearch_1.0-1_abacus.deb')
$(opa 'onearch' x 'bla' 'main' 'abacus' 'deb')
EOF

testrun empty build-needing test another
testrun empty build-needing test abacus
testrun empty build-needing test all
testrun empty build-needing test any

mkdir onlyarchall-1.0
mkdir onlyarchall-1.0/debian
cat >onlyarchall-1.0/debian/control <<END
Source: onlyarchall
Section: something
Priority: extra
Maintainer: me <me@example.org>
Standards-Version: 0.0

Package: archall
Architecture: all
Description: some test-arch all package
 .
END
cat >onlyarchall-1.0/debian/changelog <<END
onlyarchall (1.0-1) test; urgency=critical

  * first version

 -- me <me@example.orgguess@who>  Mon, 01 Jan 1980 01:02:02 +0000
END
dpkg-source -b onlyarchall-1.0
mkdir onlyarchall-1.0/debian/tmp
mkdir onlyarchall-1.0/debian/tmp/DEBIAN
mkdir -p onlyarchall-1.0/debian/tmp/usr/bin
touch onlyarchall-1.0/debian/tmp/usr/bin/program
cd onlyarchall-1.0
dpkg-gencontrol -parchall
dpkg --build debian/tmp ..
cd ..
rm -r onlyarchall-1.0

testrun - --delete includedsc test onlyarchall_1.0-1.dsc 3<<EOF
stderr
-v1*=onlyarchall_1.0-1.dsc: component guessed as 'main'
stdout
-v2*=Created directory "./pool/main/o/onlyarchall"
$(ofa 'pool/main/o/onlyarchall/onlyarchall_1.0-1.dsc')
$(ofa 'pool/main/o/onlyarchall/onlyarchall_1.0-1.tar.gz')
$(opa 'onlyarchall' x 'bla' 'main' 'source' 'dsc')
$(ota 'bla' 'onlyarchall')
EOF

testrun empty build-needing test another
testrun empty build-needing test abacus
testrun - build-needing test all 3<<EOF
stdout
*=onlyarchall 1.0-1 pool/main/o/onlyarchall/onlyarchall_1.0-1.dsc
EOF
testrun - build-needing test any 3<<EOF
stdout
*=onlyarchall 1.0-1 pool/main/o/onlyarchall/onlyarchall_1.0-1.dsc all
EOF

testrun - --delete -C main includedeb test archall_1.0-1_all.deb 3<<EOF
stderr
stdout
$(ofa 'pool/main/o/onlyarchall/archall_1.0-1_all.deb')
$(opa 'archall' x 'bla' 'main' 'abacus' 'deb')
$(opa 'archall' x 'bla' 'main' 'another' 'deb')
EOF

testrun empty build-needing test another
testrun empty build-needing test abacus
testrun empty build-needing test any

mkdir allandany-1.0
mkdir allandany-1.0/debian
cat >allandany-1.0/debian/control <<END
Source: allandany
Section: something
Priority: extra
Maintainer: me <me@example.org>
Standards-Version: 0.0

Package: allpart
Architecture: all
Description: some test-arch all package
 .

Package: anypart
Architecture: any
Description: some test-arch any package
 .
END
cat >allandany-1.0/debian/changelog <<END
allandany (1.0-1) test; urgency=critical

  * first version

 -- me <me@example.org>  Mon, 01 Jan 1980 01:02:02 +0000
END
dpkg-source -b allandany-1.0
mkdir allandany-1.0/debian/tmp
mkdir allandany-1.0/debian/tmp/DEBIAN
mkdir -p allandany-1.0/debian/tmp/usr/bin
touch allandany-1.0/debian/tmp/usr/bin/program
cd allandany-1.0
dpkg-gencontrol -panypart
dpkg --build debian/tmp ..
cd ..
rm -r allandany-1.0/debian/tmp
mkdir allandany-1.0/debian/tmp
mkdir allandany-1.0/debian/tmp/DEBIAN
mkdir -p allandany-1.0/debian/tmp/usr/share
touch allandany-1.0/debian/tmp/usr/share/data
cd allandany-1.0
dpkg-gencontrol -pallpart
dpkg --build debian/tmp ..
cd ..
echo "There was nothing to do on this architecture!" > allandany_1.0-1_another.log
echo "allandany_1.0-1_another.log - -" > allandany-1.0/debian/files
cd allandany-1.0
dpkg-genchanges -B > ../allandany_1.0-1_another.changes
cd ..
rm -r allandany-1.0

# work around dpkg-dev 1.18.15+ no longer adding Architectures of log files:
if ! grep -q -s '^Architecture:' allandany_1.0-1_another.changes ; then
	sed -e 's/^Version:/Architecture: another\n&/' -i allandany_1.0-1_another.changes
fi

testrun - --delete includedsc test allandany_1.0-1.dsc 3<<EOF
stderr
-v1*=allandany_1.0-1.dsc: component guessed as 'main'
stdout
-v2*=Created directory "./pool/main/a"
-v2*=Created directory "./pool/main/a/allandany"
$(ofa 'pool/main/a/allandany/allandany_1.0-1.dsc')
$(ofa 'pool/main/a/allandany/allandany_1.0-1.tar.gz')
$(opa 'allandany' x 'bla' 'main' 'source' 'dsc')
$(ota 'bla' 'allandany')
EOF

testrun - build-needing test another 3<<EOF
stdout
*=allandany 1.0-1 pool/main/a/allandany/allandany_1.0-1.dsc
EOF
testrun - build-needing test abacus 3<<EOF
stdout
*=allandany 1.0-1 pool/main/a/allandany/allandany_1.0-1.dsc
EOF
testrun - build-needing test all 3<<EOF
stdout
*=allandany 1.0-1 pool/main/a/allandany/allandany_1.0-1.dsc
EOF
testrun - build-needing test any 3<<EOF
stdout
*=allandany 1.0-1 pool/main/a/allandany/allandany_1.0-1.dsc abacus
*=allandany 1.0-1 pool/main/a/allandany/allandany_1.0-1.dsc another
*=allandany 1.0-1 pool/main/a/allandany/allandany_1.0-1.dsc all
EOF

testrun - --delete -C main includedeb test anypart_1.0-1_abacus.deb 3<<EOF
stderr
stdout
$(ofa 'pool/main/a/allandany/anypart_1.0-1_abacus.deb')
$(opa 'anypart' x 'bla' 'main' 'abacus' 'deb')
EOF

testrun empty build-needing test abacus
testrun - build-needing test all 3<<EOF
stdout
*=allandany 1.0-1 pool/main/a/allandany/allandany_1.0-1.dsc
EOF
testrun - build-needing test any 3<<EOF
stdout
*=allandany 1.0-1 pool/main/a/allandany/allandany_1.0-1.dsc another
*=allandany 1.0-1 pool/main/a/allandany/allandany_1.0-1.dsc all
EOF

testrun - --delete -C main includedeb test allpart_1.0-1_all.deb 3<<EOF
stderr
stdout
$(ofa 'pool/main/a/allandany/allpart_1.0-1_all.deb')
$(opa 'allpart' x 'bla' 'main' 'abacus' 'deb')
$(opa 'allpart' x 'bla' 'main' 'another' 'deb')
EOF

testrun empty build-needing test abacus
testrun empty build-needing test all
testrun - build-needing test any 3<<EOF
stdout
*=allandany 1.0-1 pool/main/a/allandany/allandany_1.0-1.dsc another
EOF

testrun - -C main include test allandany_1.0-1_another.changes 3<<EOF
stderr
stdout
$(ofa 'pool/main/a/allandany/allandany_1.0-1_another.log')
EOF

testrun empty build-needing test any

mkdir anyonly-1.0
mkdir anyonly-1.0/debian
cat >anyonly-1.0/debian/control <<END
Source: anyonly
Section: something
Priority: extra
Maintainer: me <me@example.org>
Standards-Version: 0.0

Package: anyonly
Architecture: any
Description: some test-arch any package
 .
END
cat >anyonly-1.0/debian/changelog <<END
anyonly (1.0-1) test; urgency=critical

  * first version

 -- me <me@example.org>  Mon, 01 Jan 1980 01:02:02 +0000
END
dpkg-source -b anyonly-1.0
mkdir anyonly-1.0/debian/tmp
mkdir anyonly-1.0/debian/tmp/DEBIAN
mkdir -p anyonly-1.0/debian/tmp/usr/bin
touch anyonly-1.0/debian/tmp/usr/bin/program
cd anyonly-1.0
dpkg-gencontrol -panyonly
dpkg --build debian/tmp ..
cd ..
rm -r anyonly-1.0

testrun - --delete includedsc test anyonly_1.0-1.dsc 3<<EOF
stderr
-v1*=anyonly_1.0-1.dsc: component guessed as 'main'
stdout
-v2*=Created directory "./pool/main/a/anyonly"
$(ofa 'pool/main/a/anyonly/anyonly_1.0-1.dsc')
$(ofa 'pool/main/a/anyonly/anyonly_1.0-1.tar.gz')
$(opa 'anyonly' x 'bla' 'main' 'source' 'dsc')
$(ota 'bla' 'anyonly')
EOF

testrun empty build-needing test all
testrun - build-needing test any 3<<EOF
stdout
*=anyonly 1.0-1 pool/main/a/anyonly/anyonly_1.0-1.dsc another
*=anyonly 1.0-1 pool/main/a/anyonly/anyonly_1.0-1.dsc abacus
EOF

mkdir linuxwildcard-1.0
mkdir linuxwildcard-1.0/debian
cat >linuxwildcard-1.0/debian/control <<END
Source: linuxwildcard
Section: something
Priority: extra
Maintainer: me <me@example.org>
Standards-Version: 0.0

Package: linuxwildcard
Architecture: linux-any
Description: some test-arch any package
 .
END
cat >linuxwildcard-1.0/debian/changelog <<END
linuxwildcard (1.0-1) test; urgency=critical

  * first version

 -- me <me@example.org>  Mon, 01 Jan 1980 01:02:02 +0000
END
dpkg-source -b linuxwildcard-1.0
rm -r linuxwildcard-1.0

mkdir kfreebsdwildcard-1.0
mkdir kfreebsdwildcard-1.0/debian
cat >kfreebsdwildcard-1.0/debian/control <<END
Source: kfreebsdwildcard
Section: something
Priority: extra
Maintainer: me <me@example.org>
Standards-Version: 0.0

Package: kfreebsdwildcard
Architecture: kfreebsd-any
Description: some test-arch any package
 .
END
cat >kfreebsdwildcard-1.0/debian/changelog <<END
kfreebsdwildcard (1.0-1) test; urgency=critical

  * first version

 -- me <me@example.org>  Mon, 01 Jan 1980 01:02:02 +0000
END
dpkg-source -b kfreebsdwildcard-1.0
rm -r kfreebsdwildcard-1.0

testrun - includedsc oses linuxwildcard_1.0-1.dsc 3<<EOF
stderr
-v1*=linuxwildcard_1.0-1.dsc: component guessed as 'main'
stdout
-v2*=Created directory "./pool/main/l"
-v2*=Created directory "./pool/main/l/linuxwildcard"
$(ofa 'pool/main/l/linuxwildcard/linuxwildcard_1.0-1.dsc')
$(ofa 'pool/main/l/linuxwildcard/linuxwildcard_1.0-1.tar.gz')
$(opa 'linuxwildcard' x 'oses' 'main' 'source' 'dsc')
$(ota 'oses' 'linuxwildcard')
EOF

testrun - includedsc oses kfreebsdwildcard_1.0-1.dsc 3<<EOF
stderr
-v1*=kfreebsdwildcard_1.0-1.dsc: component guessed as 'main'
stdout
-v2*=Created directory "./pool/main/k"
-v2*=Created directory "./pool/main/k/kfreebsdwildcard"
$(ofa 'pool/main/k/kfreebsdwildcard/kfreebsdwildcard_1.0-1.dsc')
$(ofa 'pool/main/k/kfreebsdwildcard/kfreebsdwildcard_1.0-1.tar.gz')
$(opa 'kfreebsdwildcard' x 'oses' 'main' 'source' 'dsc')
$(ota 'oses' 'kfreebsdwildcard')
EOF

testrun empty build-needing oses all
testrun empty build-needing oses hurd-abacus
testrun - build-needing oses abacus 3<<EOF
stdout
*=linuxwildcard 1.0-1 pool/main/l/linuxwildcard/linuxwildcard_1.0-1.dsc
EOF
testrun - build-needing oses kfreebsd-abacus 3<<EOF
stdout
*=kfreebsdwildcard 1.0-1 pool/main/k/kfreebsdwildcard/kfreebsdwildcard_1.0-1.dsc
EOF

rm -r pool conf db *.deb *.log *.changes
testsuccess
