#!/bin/bash

# testsuite to test pbuilder-satisfydepends-funcs.

TD="$(dirname "$0")"
if [ -n "$PBUILDER_CHECKOUT" ]; then
    . "$TD/testlib.sh"
    . "$PBUILDER_CHECKOUT/pbuilder-satisfydepends-funcs"
else
    . "$TD/testlib.sh"
    . "$PBUILDER_TEST_PKGLIBDIR/pbuilder-satisfydepends-funcs"
fi

DEBIAN_CONTROL=""

cleanup() {
    if [ -n "$DEBIAN_CONTROL" ]; then
        rm -f "$DEBIAN_CONTROL"
    fi
}

test_get_build_deps() {
    local BINARY_ARCH

    BINARY_ARCH="$1"

    cat <<EOF >"$DEBIAN_CONTROL"
Source: amule
XXXX:
Build-Depends:        
 autotools-dev ( >= 1.2 ) ,
	 debhelper 	,
 quilt(<<12:0),
##
 libwxgtk2.8-dev
Build-Depends-Indep: test [ amd64 i386 ], test1, 
 test2[!i386 ! amd64], test3 | 
 test4, test5
Misc: test
Build-Depends-Arch: foo1, foo2	,	foo3 (<< 12.23.34-5),
	bar4 [!amd64] <notest>, foobar [i386], gnagna [ !s390x     ]

Build-Depends: wrong build depends, line
Description: test
 other things
 wow
EOF
    get_build_deps
}

test_get_build_deps_dsc() {
    cat <<EOF >"$DEBIAN_CONTROL"
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Format: 1.0
Source: hello-debhelper
Build-Depends: debhelper (>= 7)
Checksums-Sha1: 
 fcbf0264928900adf03a7797474375e1a6fa3836 499638 hello-debhelper_2.4.orig.tar.gz
 0ea70eb46b4c90a8dbefbe60bebe4b9f9abb2733 5308 hello-debhelper_2.4-3.diff.gz


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iQEcBAEBCAAGBQJKml74AAoJEEHOfwufG4sysC4H/26EikyIgIqRuSXMiykc1hmd
97id9nWl+QzgCpUEg/+uDqEW+Bl6291FstGvzTFCwmB5JjH9ErH1LsyG1OLXbFUb
H89Em7qkl4o2ACTh255oM7FhVweof3UEaLCZMH+HZVVpHXNpEnhrfDvpTBbPGjUi
-----END PGP SIGNATURE-----
EOF
    get_build_deps
}

test_get_parsed_build_deps_dsc() {
    local ARCH="amd64"
    local BINARY_ARCH="any"

    cat > "$DEBIAN_CONTROL" << EOF
Format: 3.0 (quilt)
Source: haskell-concrete-typerep
Binary: libghc-concrete-typerep-dev, libghc-concrete-typerep-prof, libghc-concrete-typerep-doc
Architecture: any all
Version: 0.1.0.2-4
Maintainer: Debian Haskell Group <pkg-haskell-maintainers@lists.alioth.debian.org>
Uploaders: Masayuki Hatta (mhatta) <mhatta@debian.org>
Homepage: http://hackage.haskell.org/package/concrete-typerep
Standards-Version: 3.9.3
Vcs-Browser: http://darcs.debian.org/cgi-bin/darcsweb.cgi?r=pkg-haskell/haskell-concrete-typerep
Vcs-Darcs: http://darcs.debian.org/pkg-haskell/haskell-concrete-typerep
Build-Depends: debhelper (>= 9), haskell-devscripts (>= 0.9), cdbs, ghc, ghc-prof, libghc-hashable-dev (<< 1.3), libghc-hashable-prof (<< 1.3)
Build-Depends-Indep: ghc-doc, libghc-hashable-doc (<< 1.3)
Package-List:
 libghc-concrete-typerep-dev deb haskell extra arch=any
 libghc-concrete-typerep-doc deb doc extra arch=all
 libghc-concrete-typerep-prof deb haskell extra arch=any
Checksums-Sha1:
 7dc356eef28c83f606b792e3d39b68a04c1a6d98 3159 haskell-concrete-typerep_0.1.0.2.orig.tar.gz
 a5b34d7dd7f1ef42b0afad86140efcd6997d09c1 2272 haskell-concrete-typerep_0.1.0.2-4.debian.tar.xz
Checksums-Sha256:
 f72a41d9d8315528c7b0e13bf61e6122c7d236cb529c207cfb431cf272439e1f 3159 haskell-concrete-typerep_0.1.0.2.orig.tar.gz
 09dcedfef76799dcb3c6a9e22bc22711050cc56769614bdbf61b426579ce10e5 2272 haskell-concrete-typerep_0.1.0.2-4.debian.tar.xz
Files:
 5638a296661fb65145dfbc062cfa53db 3159 haskell-concrete-typerep_0.1.0.2.orig.tar.gz
 706754d44fac13c25e1ee8dd89180c53 2272 haskell-concrete-typerep_0.1.0.2-4.debian.tar.xz
EOF
    get_build_deps | filter_arch_deps "$ARCH" | filter_restriction_deps "$DEB_BUILD_PROFILES"
}

test_get_source_control_field_with_comments() {
    cat <<EOF >"$DEBIAN_CONTROL"
# This is a comment

# So is this, but with a blank line above me
Format: 1.0
Source: something-funny
Build-Depends: debhelper, something-else

EOF
    get_source_control_field "Source"
}

trap cleanup sigpipe sighup exit

# TODO move to build dir
DEBIAN_CONTROL="$(tempfile)"

expect_output "autotools-dev (>= 1.2), debhelper, quilt (<< 12:0), libwxgtk2.8-dev, test [amd64 i386], test1, test2 [!i386 !amd64], test3 | test4, test5, foo1, foo2, foo3 (<< 12.23.34-5), bar4 [!amd64]<notest>, foobar [i386], gnagna [!s390x]" \
    test_get_build_deps "any"
expect_output "autotools-dev (>= 1.2), debhelper, quilt (<< 12:0), libwxgtk2.8-dev, foo1, foo2, foo3 (<< 12.23.34-5), bar4 [!amd64]<notest>, foobar [i386], gnagna [!s390x]" \
    test_get_build_deps "binary"
expect_output "autotools-dev (>= 1.2), debhelper, quilt (<< 12:0), libwxgtk2.8-dev, test [amd64 i386], test1, test2 [!i386 !amd64], test3 | test4, test5" \
    test_get_build_deps "all"


expect_fail checkbuilddep_archdeps "foo [amd64]" "amd64"
expect_success checkbuilddep_archdeps "foo [i386]" "amd64"
expect_fail checkbuilddep_archdeps "foo [i386 amd64]" "amd64"
expect_success checkbuilddep_archdeps "foo [!amd64]" "amd64"
expect_success checkbuilddep_archdeps "foo [!i386 !amd64]" "amd64"

test_filter_arch_deps() {
    echo "$1" | filter_arch_deps "$2"
}

expect_output "foo" test_filter_arch_deps "foo" "amd64"
expect_output "foo" test_filter_arch_deps "foo [amd64]" "amd64"
expect_output "bar, foo" test_filter_arch_deps "bar, foo [amd64]" "amd64"
expect_output "bar | foo" test_filter_arch_deps "bar | foo [amd64]" "amd64"
expect_output "bar" test_filter_arch_deps "bar | foo [amd64]" "i386"

expect_fail checkbuilddep_restrictiondeps "foo <!stage1>" ""
expect_success checkbuilddep_restrictiondeps "foo <!stage1>" "stage1"
expect_fail checkbuilddep_restrictiondeps "foo <!stage1>" "notest"
expect_success checkbuilddep_restrictiondeps "foo <!stage1>" "stage1 notest"

expect_success checkbuilddep_restrictiondeps "foo <stage1>" ""
expect_fail checkbuilddep_restrictiondeps "foo <stage1>" "stage1"
expect_success checkbuilddep_restrictiondeps "foo <stage1>" "notest"
expect_fail checkbuilddep_restrictiondeps "foo <stage1>" "stage1 notest"

expect_fail checkbuilddep_restrictiondeps "foo <!stage1 !notest>" ""
expect_success checkbuilddep_restrictiondeps "foo <!stage1 !notest>" "stage1"
expect_success checkbuilddep_restrictiondeps "foo <!stage1 !notest>" "notest"
expect_success checkbuilddep_restrictiondeps "foo <!stage1 !notest>" "stage1 notest"

expect_success checkbuilddep_restrictiondeps "foo <stage1 notest>" ""
expect_success checkbuilddep_restrictiondeps "foo <stage1 notest>" "stage1"
expect_success checkbuilddep_restrictiondeps "foo <stage1 notest>" "notest"
expect_fail checkbuilddep_restrictiondeps "foo <stage1 notest>" "stage1 notest"

expect_success checkbuilddep_restrictiondeps "foo <!stage1 notest>" ""
expect_success checkbuilddep_restrictiondeps "foo <!stage1 notest>" "stage1"
expect_fail checkbuilddep_restrictiondeps "foo <!stage1 notest>" "notest"
expect_success checkbuilddep_restrictiondeps "foo <!stage1 notest>" "stage1 notest"

expect_success checkbuilddep_restrictiondeps "foo <stage1 !notest>" ""
expect_fail checkbuilddep_restrictiondeps "foo <stage1 !notest>" "stage1"
expect_success checkbuilddep_restrictiondeps "foo <stage1 !notest>" "notest"
expect_success checkbuilddep_restrictiondeps "foo <stage1 !notest>" "stage1 notest"

test_filter_restriction_deps() {
    echo "$1" | filter_restriction_deps "$2"
}

expect_output "foo" test_filter_restriction_deps "foo <!stage1>" ""
expect_output "" test_filter_restriction_deps "foo <!stage1>" "stage1"
expect_output "foo" test_filter_restriction_deps "foo <stage1>" "stage1"
expect_output "bar, foo" test_filter_restriction_deps "bar, foo <stage1>" "stage1"
expect_output "bar | foo" test_filter_restriction_deps "bar | foo <stage1>" "stage1"
expect_output "bar" test_filter_restriction_deps "bar | foo <!stage1>" "stage1"

test_filter_arch_restriction_deps() {
    echo "$1" | filter_arch_deps "$2" | filter_restriction_deps "$3"
}

expect_output "foo" test_filter_arch_restriction_deps "foo [amd64] <!stage1>" "amd64" ""
expect_output "" test_filter_arch_restriction_deps "foo [amd64] <stage1>" "amd64" ""
expect_output "foo" test_filter_arch_restriction_deps "foo [amd64] <!stage1>" "amd64" ""
expect_output "" test_filter_arch_restriction_deps "foo [i386] <stage1>" "amd64" "stage1"

expect_output "debhelper (>= 7)" test_get_build_deps_dsc
expect_output "debhelper (>= 9), haskell-devscripts (>= 0.9), cdbs, ghc, ghc-prof, libghc-hashable-dev (<< 1.3), libghc-hashable-prof (<< 1.3), ghc-doc, libghc-hashable-doc (<< 1.3)" test_get_parsed_build_deps_dsc

expect_output "something-funny" test_get_source_control_field_with_comments

testlib_summary
