#!/bin/bash
set -e

SRCDIR="${AUTOPKGTEST_SRCDIR:-$(pwd)}"
TESTDIR="$SRCDIR/tests"

cd "$SRCDIR"

echo "=== Using installed binaries ==="

# Replace build-tree binaries with symlinks to installed binaries
for bin in pwqcheck pwqgen pwqfilter; do
    target="$SRCDIR/$bin"
    rm -f "$target"
    ln -s "/usr/bin/$bin" "$target"

    echo "  $bin -> $(readlink -f "$target")"
done

echo
echo "=== Running tests ==="

PASS=0
FAIL=0

for script in "$TESTDIR"/*.sh; do
    name=$(basename "$script")
    echo "--- $name ---"

    if bash "$script"; then
        echo "--- $name: OK ---"
        PASS=$((PASS+1))
    else
        echo "--- $name: FAILED ---"
        FAIL=$((FAIL+1))
    fi
    echo
done

echo "=== Results: $PASS passed, $FAIL failed ==="

[ "$FAIL" -eq 0 ]
