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

dodo test ! -e dists
mkdir conf db logs lists

cat >> conf/distributions <<EOF
Codename: a
Architectures: abacus source
Components: one two three

Codename: b
Architectures: abacus
Components: one two four
EOF

DISTRI=a PACKAGE=aa EPOCH="" VERSION=1 REVISION="-1" FAKEVER="4-2" SECTION="one" genpackage.sh

testrun - -b . --export=never --delete --delete include a test.changes 3<<EOF
*=Warning: database 'a|one|abacus' was modified but no index file was exported.
*=Warning: database 'a|one|source' was modified but no index file was exported.
*=Changes will only be visible after the next 'export'!
stdout
-v2*=Created directory "./pool"
-v2*=Created directory "./pool/one"
-v2*=Created directory "./pool/one/a"
-v2*=Created directory "./pool/one/a/aa"
$(ofa 'pool/one/a/aa/aa-addons_4-2_all.deb')
$(ofa 'pool/one/a/aa/aa_1-1_abacus.deb')
$(ofa 'pool/one/a/aa/aa_1-1.tar.gz')
$(ofa 'pool/one/a/aa/aa_1-1.dsc')
$(opa 'aa-addons' '4-2' 'a' 'one' 'abacus' 'deb')
$(opa 'aa' '1-1' 'a' 'one' 'abacus' 'deb')
$(opa 'aa' '1-1' 'a' 'one' 'source' 'dsc')
$(otta 'a' 'aa')
-v5*=Deleting 'test.changes'.
EOF

DISTRI=a PACKAGE=aa EPOCH="" VERSION=1 REVISION="-2" FAKEVER="3-2" SECTION="two" genpackage.sh
testrun - -b . --export=never --delete --delete include a test.changes 3<<EOF
*=Warning: database 'a|two|abacus' was modified but no index file was exported.
*=Warning: database 'a|two|source' was modified but no index file was exported.
*=Changes will only be visible after the next 'export'!
stdout
-v2*=Created directory "./pool/two"
-v2*=Created directory "./pool/two/a"
-v2*=Created directory "./pool/two/a/aa"
$(ofa 'pool/two/a/aa/aa-addons_3-2_all.deb')
$(ofa 'pool/two/a/aa/aa_1-2_abacus.deb')
$(ofa 'pool/two/a/aa/aa_1-2.tar.gz')
$(ofa 'pool/two/a/aa/aa_1-2.dsc')
$(opa 'aa-addons' '3-2' 'a' 'two' 'abacus' 'deb')
$(opa 'aa' 1-2 'a' 'two' 'abacus' 'deb')
$(opa 'aa' 1-2 'a' 'two' 'source' 'dsc')
$(otta 'a' 'aa')
-v5*=Deleting 'test.changes'.
EOF

testrun - -b . ls aa 3<<EOF
stdout
*=aa | 1-1 | a | abacus, source
*=aa | 1-2 | a | abacus, source
returns 0
EOF
testrun - -b . ls aa-addons 3<<EOF
stdout
*=aa-addons | 4-2 | a | abacus
*=aa-addons | 3-2 | a | abacus
returns 0
EOF

testrun - -b . list a 3<<EOF
stdout
*=a|one|abacus: aa 1-1
*=a|one|abacus: aa-addons 4-2
*=a|one|source: aa 1-1
*=a|two|abacus: aa 1-2
*=a|two|abacus: aa-addons 3-2
*=a|two|source: aa 1-2
returns 0
EOF

testrun - -b . --export=never copy b a bb cc 3<<EOF
stderr
-v0*=Will not copy as not found: bb, cc.
stdout
-v3*=Not looking into 'a|one|source' as no matching target in 'b'!
-v3*=Not looking into 'a|two|source' as no matching target in 'b'!
-v3*=Not looking into 'a|three|abacus' as no matching target in 'b'!
-v3*=Not looking into 'a|three|source' as no matching target in 'b'!
EOF


testrun - -b . --export=never copy b a aa-addons 3<<EOF
stdout
-v3*=Not looking into 'a|one|source' as no matching target in 'b'!
-v3*=Not looking into 'a|two|source' as no matching target in 'b'!
-v3*=Not looking into 'a|three|abacus' as no matching target in 'b'!
-v3*=Not looking into 'a|three|source' as no matching target in 'b'!
-v1*=Adding 'aa-addons' '4-2' to 'b|one|abacus'.
$(opa 'aa-addons' '4-2' 'b' 'one' 'abacus' 'deb')
-v1*=Adding 'aa-addons' '3-2' to 'b|two|abacus'.
$(opa 'aa-addons' '3-2' 'b' 'two' 'abacus' 'deb')
stderr
*=Warning: database 'b|one|abacus' was modified but no index file was exported.
*=Warning: database 'b|two|abacus' was modified but no index file was exported.
*=Changes will only be visible after the next 'export'!
EOF

testrun - -b . list b 3<<EOF
stdout
*=b|one|abacus: aa-addons 4-2
*=b|two|abacus: aa-addons 3-2
returns 0
EOF

testrun - -b . ls aa 3<<EOF
stdout
*=aa | 1-1 | a | abacus, source
*=aa | 1-2 | a | abacus, source
returns 0
EOF
testrun - -b . ls aa-addons 3<<EOF
stdout
*=aa-addons | 4-2 | a | abacus
*=aa-addons | 3-2 | a | abacus
*=aa-addons | 4-2 | b | abacus
*=aa-addons | 3-2 | b | abacus
returns 0
EOF

testrun - -b . --export=never remove b aa-addons 3<<EOF
stdout
$(opd 'aa-addons' unset b one abacus deb)
$(opd 'aa-addons' unset b two abacus deb)
stderr
*=Warning: database 'b|one|abacus' was modified but no index file was exported.
*=Warning: database 'b|two|abacus' was modified but no index file was exported.
*=Changes will only be visible after the next 'export'!
EOF

testrun - -b . ls aa-addons 3<<EOF
stdout
*=aa-addons | 4-2 | a | abacus
*=aa-addons | 3-2 | a | abacus
returns 0
EOF

testrun - -b . --export=never copysrc b a aa-addons 3<<EOF
stdout
-v3*=Not looking into 'a|one|source' as no matching target in 'b'!
-v3*=Not looking into 'a|two|source' as no matching target in 'b'!
-v3*=Not looking into 'a|three|abacus' as no matching target in 'b'!
-v3*=Not looking into 'a|three|source' as no matching target in 'b'!
stderr
-v0*=Nothing to do as no package with source 'aa-addons' found!
EOF

testrun - -b . --export=never copysrc b a aa 4-2 3-2 3<<EOF
stdout
-v3*=Not looking into 'a|one|source' as no matching target in 'b'!
-v3*=Not looking into 'a|two|source' as no matching target in 'b'!
-v3*=Not looking into 'a|three|abacus' as no matching target in 'b'!
-v3*=Not looking into 'a|three|source' as no matching target in 'b'!
stderr
-v0*=Nothing to do as no packages with source 'aa' and a requested source version found!
EOF

testrun - -b . --export=never copysrc b a aa 1-1 2-2 3<<EOF
stdout
-v3*=Not looking into 'a|one|source' as no matching target in 'b'!
-v3*=Not looking into 'a|two|source' as no matching target in 'b'!
-v3*=Not looking into 'a|three|abacus' as no matching target in 'b'!
-v3*=Not looking into 'a|three|source' as no matching target in 'b'!
-v1*=Adding 'aa-addons' '4-2' to 'b|one|abacus'.
$(opa 'aa-addons' 4-2 'b' 'one' 'abacus' 'deb')
-v1*=Adding 'aa' '1-1' to 'b|one|abacus'.
$(opa 'aa' 1-1 'b' 'one' 'abacus' 'deb')
stderr
-v0*=Will not copy as not found: 2-2.
-v6*=Found versions are: 1-1.
*=Warning: database 'b|one|abacus' was modified but no index file was exported.
*=Changes will only be visible after the next 'export'!
EOF

testrun - -b . --export=never copysrc b a aa 1-1 1-2 3<<EOF
stdout
-v3*=Not looking into 'a|one|source' as no matching target in 'b'!
-v3*=Not looking into 'a|two|source' as no matching target in 'b'!
-v3*=Not looking into 'a|three|abacus' as no matching target in 'b'!
-v3*=Not looking into 'a|three|source' as no matching target in 'b'!
-v1*=Adding 'aa-addons' '4-2' to 'b|one|abacus'.
$(opu 'aa-addons' 4-2 4-2 'b' 'one' 'abacus' 'deb')
-v1*=Adding 'aa' '1-1' to 'b|one|abacus'.
$(opu 'aa' 1-1 1-1 'b' 'one' 'abacus' 'deb')
-v1*=Adding 'aa-addons' '3-2' to 'b|two|abacus'.
$(opa 'aa-addons' 3-2 'b' 'two' 'abacus' 'deb')
-v1*=Adding 'aa' '1-2' to 'b|two|abacus'.
$(opa 'aa' 1-2 'b' 'two' 'abacus' 'deb')
stderr
-v6*=Found versions are: 1-1, 1-2.
*=Warning: replacing 'aa-addons' version '4-2' with equal version '4-2' in 'b|one|abacus'!
*=Warning: replacing 'aa' version '1-1' with equal version '1-1' in 'b|one|abacus'!
*=Warning: database 'b|one|abacus' was modified but no index file was exported.
*=Warning: database 'b|two|abacus' was modified but no index file was exported.
*=Changes will only be visible after the next 'export'!
EOF

rm -r db conf pool logs lists
testsuccess
