#!/bin/sh
# Build a small PDF with known words and metadata, run ScrapPY on it in its
# different modes and check the generated wordlists and reports.
set -u

SRCDIR="$(pwd)"
WORKDIR="${AUTOPKGTEST_TMP:-$(mktemp -d)}"
cd "$WORKDIR"

PASSED=0
FAILED=0
check() {
    desc="$1"
    shift
    if "$@"; then
        echo "PASS: $desc"
        PASSED=$((PASSED + 1))
    else
        echo "FAIL: $desc"
        FAILED=$((FAILED + 1))
    fi
}

run() {
    echo "\$ scrappy $*"
    scrappy "$@" >out.log 2>&1
    status=$?
    cat out.log
    return $status
}

has_line() { grep -qx -- "$1" "$2"; }

echo "### Creating sample PDF"
python3 - <<'PY'
from reportlab.pdfgen import canvas

c = canvas.Canvas("sample.pdf")
c.setTitle("Plant Network Manual")
c.setAuthor("Kali Tester")
c.setSubject("Wordlist generation")
c.drawString(72, 750, "The admin password is Winter2026 and the admin user is "
             "operator with admin rights on the scada historian xK9qZ7vR2m")
c.showPage()
c.drawString(72, 750, "Contact the admin at the control room for the "
             "firmware upgrade of the scada gateway")
c.save()
PY
ls -l sample.pdf

echo "### Full mode"
check "full mode exits successfully" run -f sample.pdf -o full.txt
check "full mode reports the output file" grep -q "full.txt has been created" out.log
echo "--- full.txt"; cat full.txt 2>/dev/null
for w in admin password winter2026 operator scada historian firmware gateway; do
    check "full wordlist contains '$w'" has_line "$w" full.txt
done
check "full wordlist has no common words" sh -c '! grep -qxE "the|and|is|at|with|for" full.txt'
check "full wordlist is deduplicated" sh -c '[ "$(sort full.txt | uniq -d | wc -l)" -eq 0 ]'

echo "### Default output file"
check "default output goes to ScrapPY.txt" run -f sample.pdf
check "ScrapPY.txt contains 'historian'" has_line historian ScrapPY.txt

echo "### Word frequency mode"
check "word-frequency mode exits successfully" run -f sample.pdf -m word-frequency -o freq.txt
echo "--- freq.txt"; cat freq.txt 2>/dev/null
check "most frequent word 'admin' comes first" sh -c '[ "$(head -n1 freq.txt)" = admin ]'

echo "### Entropy mode"
check "entropy mode exits successfully" run -f sample.pdf -m entropy
check "highest entropy keyword is the random string" \
    sh -c 'grep -m1 "Keyword:" out.log | grep -q xk9qz7vr2m'

echo "### Metadata mode"
check "metadata mode exits successfully" run -f sample.pdf -m metadata
check "metadata shows the title" grep -q "Title: .*Plant Network Manual" out.log
check "metadata shows the author" grep -q "Author: .*Kali Tester" out.log
check "metadata shows the subject" grep -q "Subject: .*Wordlist generation" out.log

echo "### Upstream sample PDF"
check "upstream sample PDF is scraped" run -f "$SRCDIR/test/test_pdf.pdf" -o sample.txt
check "upstream sample wordlist contains 'test pdf file'" \
    sh -c 'has_line() { grep -qx -- "$1" "$2"; }; has_line test sample.txt && has_line pdf sample.txt && has_line file sample.txt'

echo "$PASSED passed, $FAILED failed"
[ "$FAILED" -eq 0 ]
