#!/bin/sh

# Check the standalone pgfplots-autonode public surface without consulting any
# LuaCoolProp source or documentation file.

set -eu

fail()
{
    printf 'error: %s\n' "$*" >&2
    exit 1
}

need_command()
{
    command -v "$1" >/dev/null 2>&1 \
        || fail "required command not found: $1"
}

for command_name in awk dirname grep mktemp rm sed sort; do
    need_command "$command_name"
done
SCRIPT_DIR=$(CDPATH= cd -P "$(dirname "$0")" && pwd) \
    || fail "could not determine the script directory"
PROJECT_DIR=$(CDPATH= cd -P "$SCRIPT_DIR/.." && pwd) \
    || fail "could not determine the project directory"
SOURCE=$PROJECT_DIR/pgflibrarypgfplots.autonode.code.tex
LUA_SOURCE=$PROJECT_DIR/pgfplots-autonode.lua
STYLE=$PROJECT_DIR/pgfplots-autonode.sty
REFERENCE=$PROJECT_DIR/docs/pgfplots-autonode-reference.tex
MANUAL=$PROJECT_DIR/pgfplots-autonode-manual.tex
README=$PROJECT_DIR/pgfplots-autonode-README.md
[ -f "$README" ] || README=$PROJECT_DIR/README.md
LICENSE_FILE=$PROJECT_DIR/pgfplots-autonode-LICENSE
[ -f "$LICENSE_FILE" ] || LICENSE_FILE=$PROJECT_DIR/LICENSE
CHANGELOG=$PROJECT_DIR/pgfplots-autonode-CHANGELOG.md
[ -f "$CHANGELOG" ] || CHANGELOG=$PROJECT_DIR/CHANGELOG.md
MANIFEST=$PROJECT_DIR/PGFPLOTS-AUTONODE-MANIFEST.txt
[ -f "$MANIFEST" ] || MANIFEST=$PROJECT_DIR/MANIFEST.txt
VERSION_FILE=$PROJECT_DIR/PGFPLOTS-AUTONODE-VERSION
[ -f "$VERSION_FILE" ] || VERSION_FILE=$PROJECT_DIR/VERSION

for file_name in "$SOURCE" "$LUA_SOURCE" "$STYLE" "$REFERENCE" \
    "$MANUAL" "$README" "$LICENSE_FILE" "$CHANGELOG" "$MANIFEST" \
    "$VERSION_FILE"; do
    [ -f "$file_name" ] || fail "required file not found: $file_name"
done

TEMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/pgfplots-autonode-doc.XXXXXX") \
    || fail "could not create a temporary directory"
cleanup()
{
    rm -rf "$TEMP_DIR"
}
trap cleanup 0
trap 'exit 1' HUP INT TERM

LABEL_KEYS=$TEMP_DIR/label-keys
AXIS_KEYS=$TEMP_DIR/axis-keys
MISSING=$TEMP_DIR/missing
: >"$MISSING"

awk '
  /User keys for \\pgfplotsautonode/ { inside=1; next }
  /pgfplots axis-level keys/ { inside=0 }
  inside && /^[[:space:]]*[A-Za-z]/ && /\/\./ {
    line=$0
    sub(/^[[:space:]]*/, "", line)
    sub(/\/\..*$/, "", line)
    sub(/\/(uniform|around preferred|around-preferred|adaptive)$/, "", line)
    if (line != "default") print line
  }
' "$SOURCE" | sort -u >"$LABEL_KEYS"

awk '
  /pgfplots axis-level keys/ { inside=1; next }
  inside && /^[[:space:]]*auto node / && /\/\./ {
    line=$0
    sub(/^[[:space:]]*/, "", line)
    sub(/\/\..*$/, "", line)
    sub(/\/(false|off|summary|verbose|trace|greedy|repair|local-search|exact-small|axis-aligned|oriented|warn|error|hide-low-priority|allow-minimal-overlap|uniform|around preferred|around-preferred|adaptive)$/, "", line)
    print line
  }
' "$SOURCE" | sort -u >"$AXIS_KEYS"

check_names()
{
    kind=$1
    names=$2
    while IFS= read -r name; do
        [ -n "$name" ] || continue
        grep -Fq "$name" "$REFERENCE" \
            || printf '%s: %s\n' "$kind" "$name" >>"$MISSING"
    done <"$names"
}
check_names "per-label key" "$LABEL_KEYS"
check_names "axis key" "$AXIS_KEYS"

VERSION=$(sed -n 's/^VERSION=//p' "$VERSION_FILE")
RELEASE_DATE=$(sed -n 's/^RELEASE_DATE=//p' "$VERSION_FILE")
[ -n "$VERSION" ] || fail "VERSION is missing from $VERSION_FILE"
[ -n "$RELEASE_DATE" ] || fail "RELEASE_DATE is missing from $VERSION_FILE"
grep -Fq "v$VERSION" "$STYLE" \
    || printf '%s\n' 'version: pgfplots-autonode.sty' >>"$MISSING"
grep -Fq "_VERSION = \"$VERSION\"" "$LUA_SOURCE" \
    || printf '%s\n' 'version: pgfplots-autonode.lua' >>"$MISSING"
grep -Fq "# pgfplots-autonode $VERSION" "$README" \
    || printf '%s\n' 'version: README' >>"$MISSING"
grep -Fq "Version $VERSION --- $RELEASE_DATE" "$MANUAL" \
    || printf '%s\n' 'version: manual' >>"$MISSING"
grep -Fq '\input{docs/pgfplots-autonode-reference.tex}' "$MANUAL" \
    || printf '%s\n' 'manual: complete reference input' >>"$MISSING"
grep -Fq '\makeindex[name=commands' "$MANUAL" \
    || printf '%s\n' 'manual: command index' >>"$MISSING"
grep -Fq '\makeindex[name=keys' "$MANUAL" \
    || printf '%s\n' 'manual: key index' >>"$MISSING"
grep -Fq 'affine functions' "$MANUAL" \
    || printf '%s\n' 'manual: affine-network tutorial' >>"$MISSING"
grep -Fq 'Case study: a dense thermodynamic chart' "$MANUAL" \
    || printf '%s\n' 'manual: LuaCoolProp case study' >>"$MISSING"
grep -Fq 'examples/ph-r134a-autonode-full-network.pdf' "$MANUAL" \
    || printf '%s\n' 'manual: vector PH case-study figure' >>"$MISSING"
grep -Fq 'vibe-coded with ChatGPT' "$README" \
    || printf '%s\n' 'README: AI disclosure' >>"$MISSING"
grep -Fq 'LaTeX Project Public License' "$LICENSE_FILE" \
    || printf '%s\n' 'license: LPPL declaration' >>"$MISSING"
grep -Fq 'Christophe Jorssen' "$LICENSE_FILE" \
    || printf '%s\n' 'license: current maintainer' >>"$MISSING"
grep -Fq "## $VERSION - $RELEASE_DATE" "$CHANGELOG" \
    || printf '%s\n' 'changelog: current release' >>"$MISSING"

for runtime_file in "$SOURCE" "$LUA_SOURCE" \
    "$PROJECT_DIR/pgfplots-autonode.code.tex" "$STYLE"; do
    if grep -Fiq 'luacoolprop' "$runtime_file"; then
        printf 'runtime dependency leak: %s\n' "${runtime_file##*/}" >>"$MISSING"
    fi
done
if grep -Eq '(^|/)luacoolprop(\.lua|\.tex|\.sty|-manual\.pdf)?$' "$MANIFEST"; then
    printf '%s\n' 'manifest: LuaCoolProp file included' >>"$MISSING"
fi

if [ -s "$MISSING" ]; then
    printf 'error: standalone autonode documentation check failed:\n' >&2
    sed 's/^/  - /' "$MISSING" >&2
    exit 1
fi
printf 'Standalone pgfplots-autonode documentation coverage passed.\n'
