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

mkdir -p conf dists/c/main/source
mkdir -p conf dists/d/main/source
mkdir -p conf dists/c/main/binary-abacus
mkdir -p conf dists/d/main/binary-abacus
mkdir -p conf dists/c/component/source
mkdir -p conf dists/d/component/source
mkdir -p conf dists/c/component/binary-abacus
mkdir -p conf dists/d/component/binary-abacus
mkdir -p dists/c/main/source
mkdir -p dists/d/main/source
mkdir -p dists/c/main/binary-abacus
mkdir -p dists/d/main/binary-abacus
mkdir -p pool/main/a/aa pool/component/b/bb
mkdir -p pool/component/a/aa pool/main/b/bb
cat > conf/distributions <<EOF
Codename: c
Components: main component
Architectures: abacus source
# Don't do that at home, kids....
DebIndices: Index .
DscIndices: Index .
DebOverride: override-c-deb
DscOverride: override-c-dsc

Codename: d
Components: main component
Architectures: abacus source
# Don't do that at home, kids....
DebIndices: Index .
DscIndices: Index .
DebOverride: override-d-deb
DscOverride: override-d-dsc
EOF
cat > conf/override-c-deb <<EOF
EOF
cat > conf/override-c-dsc <<EOF
EOF
cat > conf/override-d-deb <<EOF
aa Section component/section
aa Somefield value
aa-addons Section component/addons
a* ShouldNot ShowUp
bb Section base
bb-addons Section addons
b* Section blub
EOF
cat > conf/override-d-dsc <<EOF
a* Section component/section
b? Section base
b? SomeOtherfield somevalue
b* ShouldNot ShowUp
EOF

DISTRI=c PACKAGE=aa EPOCH="" VERSION=1 REVISION="-1" SECTION="section" genpackage.sh
mv test.changes aa.changes
DISTRI=c PACKAGE=bb EPOCH="" VERSION=1 REVISION="-1" SECTION="component/base" genpackage.sh
mv test.changes bb.changes

testrun - --nodelete include c aa.changes 3<<EOF
stdout
$(odb)
$(ofa 'pool/main/a/aa/aa-addons_1-1_all.deb')
$(ofa 'pool/main/a/aa/aa_1-1_abacus.deb')
$(ofa 'pool/main/a/aa/aa_1-1.tar.gz')
$(ofa 'pool/main/a/aa/aa_1-1.dsc')
$(opa 'aa-addons' x 'c' 'main' 'abacus' 'deb')
$(opa 'aa' x 'c' 'main' 'abacus' 'deb')
$(opa 'aa' unset 'c' 'main' 'source' 'dsc')
-v0*=Exporting indices...
-v6*= looking for changes in 'c|main|abacus'...
-v6*=  creating './dists/c/main/binary-abacus/Index' (uncompressed)
-v6*= looking for changes in 'c|component|abacus'...
-v6*=  creating './dists/c/component/binary-abacus/Index' (uncompressed)
-v6*= looking for changes in 'c|main|source'...
-v6*=  creating './dists/c/main/source/Index' (uncompressed)
-v6*= looking for changes in 'c|component|source'...
-v6*=  creating './dists/c/component/source/Index' (uncompressed)
EOF
testrun - --nodelete include c bb.changes 3<<EOF
stdout
$(ofa 'pool/component/b/bb/bb-addons_1-1_all.deb')
$(ofa 'pool/component/b/bb/bb_1-1_abacus.deb')
$(ofa 'pool/component/b/bb/bb_1-1.tar.gz')
$(ofa 'pool/component/b/bb/bb_1-1.dsc')
$(opa 'bb-addons' x 'c' 'component' 'abacus' 'deb')
$(opa 'bb' x 'c' 'component' 'abacus' 'deb')
$(opa 'bb' unset 'c' 'component' 'source' 'dsc')
-v0*=Exporting indices...
-v6*= looking for changes in 'c|main|abacus'...
-v6*= looking for changes in 'c|component|abacus'...
-v6*=  replacing './dists/c/component/binary-abacus/Index' (uncompressed)
-v6*= looking for changes in 'c|main|source'...
-v6*= looking for changes in 'c|component|source'...
-v6*=  replacing './dists/c/component/source/Index' (uncompressed)
EOF
ed -s aa.changes <<EOF
g/^Distribution/s/ c/ d/
w
q
EOF
ed -s bb.changes <<EOF
g/^Distribution/s/ c/ d/
w
q
EOF
testrun - --nodelete include d aa.changes 3<<EOF
stdout
$(ofa 'pool/component/a/aa/aa-addons_1-1_all.deb')
$(ofa 'pool/component/a/aa/aa_1-1_abacus.deb')
$(ofa 'pool/component/a/aa/aa_1-1.tar.gz')
$(ofa 'pool/component/a/aa/aa_1-1.dsc')
$(opa 'aa-addons' x 'd' 'component' 'abacus' 'deb')
$(opa 'aa' x 'd' 'component' 'abacus' 'deb')
$(opa 'aa' unset 'd' 'component' 'source' 'dsc')
-v0*=Exporting indices...
-v6*= looking for changes in 'd|component|abacus'...
-v6*=  creating './dists/d/component/binary-abacus/Index' (uncompressed)
-v6*= looking for changes in 'd|main|abacus'...
-v6*=  creating './dists/d/main/binary-abacus/Index' (uncompressed)
-v6*= looking for changes in 'd|component|source'...
-v6*=  creating './dists/d/component/source/Index' (uncompressed)
-v6*= looking for changes in 'd|main|source'...
-v6*=  creating './dists/d/main/source/Index' (uncompressed)
EOF
testrun - --nodelete include d bb.changes 3<<EOF
stdout
$(ofa 'pool/main/b/bb/bb-addons_1-1_all.deb')
$(ofa 'pool/main/b/bb/bb_1-1_abacus.deb')
$(ofa 'pool/main/b/bb/bb_1-1.tar.gz')
$(ofa 'pool/main/b/bb/bb_1-1.dsc')
$(opa 'bb-addons' x 'd' 'main' 'abacus' 'deb')
$(opa 'bb' x 'd' 'main' 'abacus' 'deb')
$(opa 'bb' unset 'd' 'main' 'source' 'dsc')
-v0*=Exporting indices...
-v6*= looking for changes in 'd|component|abacus'...
-v6*= looking for changes in 'd|main|abacus'...
-v6*=  replacing './dists/d/main/binary-abacus/Index' (uncompressed)
-v6*= looking for changes in 'd|component|source'...
-v6*= looking for changes in 'd|main|source'...
-v6*=  replacing './dists/d/main/source/Index' (uncompressed)
EOF

cp dists/c/main/binary-abacus/Index Index.expected
ed -s Index.expected <<EOF
/^Priority:/i
Somefield: value
.
g/Section/s#section#component/addons#
/Section/s#addons#section#
%s/main/component/
w
EOF
dodiff Index.expected dists/d/component/binary-abacus/Index

cp dists/c/component/source/Index Index.expected
ed -s Index.expected <<EOF
/^Priority:/i
SomeOtherfield: somevalue
.
g/Section/s#component/base#base#
g/Directory/s/component/main/
w
EOF
dodiff Index.expected dists/d/main/source/Index


dodo rm -r aa* bb* pool dists db conf

testsuccess
