#! /bin/sh

set -e

# Check that the listed policies are as expected when there are only builtin policies
echo "sigsum-test1-2025" > test.policies.expected
echo "sigsum-test2-2025" >> test.policies.expected
# Note: this test will need to be updated by appending any additional builtin policy names to test.policies.expected here
sort test.policies.expected > test.policies.expected.sorted
SIGSUM_POLICY_DIR=/dev/null ./bin/sigsum-policy list | sort | diff test.policies.expected.sorted -

TMP_POLICY_DIR=$(mktemp -d)

cleanup () {
    rm -f "${TMP_POLICY_DIR}"/testpolicy1.sigsum-policy
    rm -f "${TMP_POLICY_DIR}"/testpolicy2.sigsum-policy
    rm -f "${TMP_POLICY_DIR}"/abc-test.sigsum-policy
    rm -f "${TMP_POLICY_DIR}"/sigsum-test1-2025.sigsum-policy
    rm -f "${TMP_POLICY_DIR}"/testpolicy-symlinked.sigsum-policy
    rm -f "${TMP_POLICY_DIR}"/UPPERCASE-should-be-skipped.sigsum-policy
    rm -f "${TMP_POLICY_DIR}"/lowercase-should-be-included.sigsum-policy
    rmdir "${TMP_POLICY_DIR}"/dir-should-be-skipped.sigsum-policy
    rm -f "${TMP_POLICY_DIR}"/dir-link-should-be-skipped.sigsum-policy
    rmdir "${TMP_POLICY_DIR}"
}

trap cleanup EXIT

echo "log $(./bin/sigsum-key to-hex -k test.log.key.pub) http://localhost:6965" > test.policy
echo "quorum none" >> test.policy
cp test.policy "${TMP_POLICY_DIR}"/testpolicy1.sigsum-policy
cp test.policy "${TMP_POLICY_DIR}"/testpolicy2.sigsum-policy
cp test.policy "${TMP_POLICY_DIR}"/abc-test.sigsum-policy
cp test.policy "${TMP_POLICY_DIR}"/sigsum-test1-2025.sigsum-policy # override builtin policy

# Check that the listed policies are as expected when there are installed policies in addition to the builtin policies
printf "testpolicy1\ntestpolicy2\nabc-test\n" >> test.policies.expected
sort test.policies.expected > test.policies.expected.sorted
SIGSUM_POLICY_DIR=${TMP_POLICY_DIR} ./bin/sigsum-policy list | sort | diff test.policies.expected.sorted -

echo "Checking that we can override the \"sigsum-test1-2025\" builtin policy"
SIGSUM_POLICY_DIR=/dev/null ./bin/sigsum-policy show sigsum-test1-2025 > test.builtin.sigsum-policy
SIGSUM_POLICY_DIR=${TMP_POLICY_DIR} ./bin/sigsum-policy show sigsum-test1-2025 > test.other.sigsum-policy
# There should be a diff
diff -q test.builtin.sigsum-policy test.other.sigsum-policy && false
# The test.other.sigsum-policy file should be the one from TMP_POLICY_DIR
diff test.other.sigsum-policy "${TMP_POLICY_DIR}"/sigsum-test1-2025.sigsum-policy

# Check that "sigsum-policy list" works for symlink
ln -sr test.policy "${TMP_POLICY_DIR}"/testpolicy-symlinked.sigsum-policy
SIGSUM_POLICY_DIR=${TMP_POLICY_DIR} ./bin/sigsum-policy list | grep testpolicy-symlinked

# Check that "sigsum-policy show" works for symlink
SIGSUM_POLICY_DIR=${TMP_POLICY_DIR} ./bin/sigsum-policy show testpolicy-symlinked > test.symlinked.sigsum-policy
diff test.policy test.symlinked.sigsum-policy

# Check that "sigsum-policy list" skips directories
mkdir "${TMP_POLICY_DIR}"/dir-should-be-skipped.sigsum-policy
ln -sr "${TMP_POLICY_DIR}"/dir-should-be-skipped.sigsum-policy "${TMP_POLICY_DIR}"/dir-link-should-be-skipped.sigsum-policy
SIGSUM_POLICY_DIR=${TMP_POLICY_DIR} ./bin/sigsum-policy list | grep dir-should-be-skipped && false
SIGSUM_POLICY_DIR=${TMP_POLICY_DIR} ./bin/sigsum-policy list | grep dir-link-should-be-skipped && false

# Check that "sigsum-policy list" skips filenames that do not follow the rules for policy names
cp test.policy "${TMP_POLICY_DIR}"/UPPERCASE-should-be-skipped.sigsum-policy
cp test.policy "${TMP_POLICY_DIR}"/lowercase-should-be-included.sigsum-policy
SIGSUM_POLICY_DIR=${TMP_POLICY_DIR} ./bin/sigsum-policy list | grep UPPERCASE-should-be-skipped && false
SIGSUM_POLICY_DIR=${TMP_POLICY_DIR} ./bin/sigsum-policy list | grep lowercase-should-be-included
