#! /bin/sh

set -e

. ${srcdir}/test.lib.sh

tl_plan 5

# Calling without arguments is invalid
(${top_builddir}/src/dh-exec-install 2>&1 || true) | \
        tl_expect_match "Calling without arguments is invalid" \
                        "stdin not acceptable"
tl_next

# Calling as a she-bang works
t=$(mktemp --tmpdir=. tmpXXXXXXXX.install)
td=$(mktemp -d --tmpdir=. tmpXXXXX)
dd="debian/$(basename -- ${t} .install)"

install -d "${dd}/var/lib/dh-exec"

cat >"${t}" <<EOF
#! ${top_builddir}/src/dh-exec-install
$(basename ${t}) => /var/lib/dh-exec/test-output
foo/bar /tmp/foo
baz/quux
# This is an empty line:


EOF

chmod +x "${t}"

"${t}" | tl_expect_match "Calling as a she-bang works" "debian/tmp/dh-exec\."
rm -f "${t}"
rm -rf "${td}"
rm -rf "${dd}"

tl_next

# Using an invalid filename should fail, even if the file exists
t=$(mktemp --tmpdir tmpXXXXXX.invalid)
(${top_builddir}/src/dh-exec-install "${t}" 2>&1 || true) | \
        tl_expect_match "Only .install files are allowed" \
                        "Only \.install filename extensions are allowed"
rm -f "${t}"
tl_next

# Calling with too many arguments does NOT work
(${top_builddir}/src/dh-exec-subst too many arguments 2>&1 || true) | \
        tl_expect_match "Calling with too many arguments does NOT work" \
                        "no argument at all"
tl_next

# Calling with a non-existing file does NOT work, either.
(${top_builddir}/src/dh-exec-subst --invalid-file-- 2>&1 || true) | \
        tl_expect_match "Calling with a non-existing file does NOT work" \
                        "can't open --invalid-file--"
tl_next
