#!/bin/sh

set -eu

NGINX_CONFIG=/etc/nginx/conf.d/fancyindex-autopkgtest.conf
DOCUMENT_ROOT=/var/lib/nginx/fancyindex-autopkgtest
HEADER_FILE=/var/lib/nginx/fancyindex-autopkgtest-header.html
FOOTER_FILE=/var/lib/nginx/fancyindex-autopkgtest-footer.html
TEST_URL=http://127.0.0.1:18080/
HEADERS="${AUTOPKGTEST_TMP}/fancyindex.headers"
BODY="${AUTOPKGTEST_TMP}/fancyindex.body"

cleanup()
{
    rm -f "${NGINX_CONFIG}" "${HEADER_FILE}" "${FOOTER_FILE}" \
        "${DOCUMENT_ROOT}/alpha.txt" "${DOCUMENT_ROOT}/beta.log"
    rmdir "${DOCUMENT_ROOT}" 2>/dev/null || :
    invoke-rc.d nginx reload >/dev/null 2>&1 || :
}

trap cleanup EXIT

diagnostics()
{
    if [ -f "${HEADERS}" ]; then
        echo "=== response headers ==="
        sed 's/\r$//' "${HEADERS}"
    fi
    if [ -f "${BODY}" ]; then
        echo "=== response body ==="
        cat "${BODY}"
    fi
    echo "=== nginx configuration ==="
    nginx -T || :
    echo "=== journalctl ==="
    journalctl -n all -xu nginx.service || :
    echo "=== error.log ==="
    if [ -f /var/log/nginx/error.log ]; then
        cat /var/log/nginx/error.log
    else
        echo "/var/log/nginx/error.log not found"
    fi
}

fail()
{
    echo "FAILED: $*" >&2
    diagnostics
    exit 1
}

install -d -o www-data -g www-data -m 0755 "${DOCUMENT_ROOT}"
printf '%s\n' 'alpha' > "${DOCUMENT_ROOT}/alpha.txt"
printf '%s\n' 'beta' > "${DOCUMENT_ROOT}/beta.log"

cat > "${HEADER_FILE}" <<'EOF'
<!DOCTYPE html>
<html><head><title>FancyIndex autopkgtest</title></head><body>
<div id="autopkgtest-header">local header</div>
EOF
printf '%s' '<h1>Index of ' >> "${HEADER_FILE}"

cat > "${FOOTER_FILE}" <<'EOF'
<div id="autopkgtest-footer">local footer</div>
</body></html>
EOF

cat > "${NGINX_CONFIG}" <<'EOF'
server {
    listen 127.0.0.1:18080;
    server_name localhost;

    root /var/lib/nginx/fancyindex-autopkgtest;

    location / {
        fancyindex on;
        fancyindex_header /var/lib/nginx/fancyindex-autopkgtest-header.html local;
        fancyindex_footer /var/lib/nginx/fancyindex-autopkgtest-footer.html local;
    }
}
EOF

nginx -t || fail "nginx configuration test"
invoke-rc.d nginx restart || fail "nginx restart"

status="$(curl --silent --show-error --max-time 60 \
    --retry 10 --retry-connrefused --retry-delay 1 \
    --dump-header "${HEADERS}" --output "${BODY}" \
    --write-out '%{http_code}' "${TEST_URL}")" ||
    fail "GET ${TEST_URL} request failed"

[ "${status}" = 200 ] || fail "GET returned HTTP ${status}, expected 200"
grep -Eiq '^Content-Type:[[:space:]]*text/html' "${HEADERS}" ||
    fail "response is not HTML"
grep -Fq '<table' "${BODY}" || fail "response lacks the generated index table"
grep -Fq 'Index of /' "${BODY}" || fail "response lacks the index title"
grep -Fq 'alpha.txt' "${BODY}" || fail "response lacks alpha.txt"
grep -Fq 'beta.log' "${BODY}" || fail "response lacks beta.log"
grep -Fq '<div id="autopkgtest-header">local header</div>' "${BODY}" ||
    fail "response lacks the local header"
grep -Fq '<div id="autopkgtest-footer">local footer</div>' "${BODY}" ||
    fail "response lacks the local footer"
echo "FancyIndex generated the expected HTML listing and local header/footer ... OK"
