#!/usr/bin/env bash
#
# Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
#
# This script regenerates documentation files. Running it again a `main`
# commit should not produce any changes.

set -o errexit
set -o nounset

# GNU `sort` sorts case-insensitive while macOS `sed` sorts case-sensitive which
# we want. Force GNU sed into case-sensitive mode.
export LC_COLLATE=C

# We expect a GNU sed. If we find `gsed` use that instead since we are likely
# on macOS which by default provides a BSD sed.
SED="sed"
if command -v gsed >/dev/null 2>&1; then
    SED=gsed
fi

ROOTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)/../.."
BUILDDIR="${ROOTDIR}/build"
SPICYDOC="${BUILDDIR}/bin/spicy-doc"
SPICYDTR="${ROOTDIR}/doc/scripts/spicy-doc-to-rst"
AUTOGEN_FINAL="${ROOTDIR}/doc/autogen"
AUTOGEN_STAGE=$(mktemp -d -t spicy-autogen-docs.XXXXXXXXXX)

update_architecture_diagram() {
    # autogen-architecture-diagram needs the diagrams package and dot.
    if ! python3 -c "import diagrams" >/dev/null 2>&1; then
        >&2 echo "Warning: Need Python diagrams to run autogen-docs, skipping"
        return
    fi
    if ! command -v dot >/dev/null 2>&1; then
        >&2 echo "Warning: Need 'dot' to run autogen-docs, skipping"
        return
    fi

    "${ROOTDIR}/doc/scripts/autogen-architecture-diagram" "${AUTOGEN_STAGE}/architecture" || exit 1

    # Remove comments and titles from SVG to make content stable.
    ${SED} -i"" -e '/<!--/d' -e '/-->/d' "${AUTOGEN_STAGE}/architecture.svg"
    ${SED} -i"" -e '/<title/d' -e '/title>/d' "${AUTOGEN_STAGE}/architecture.svg"

    # Delete unstable DOT and PDF outputs.
    rm -f "${AUTOGEN_STAGE}/architecture.dot" "${AUTOGEN_STAGE}/architecture.pdf"
}

if ! command -v rsync >/dev/null 2>&1; then
    >&2 echo "Warning: Need rsync to run autogen-docs, aborting"
    exit 0
fi

if [[ ! -x ${BUILDDIR}/bin/spicy-doc ]]; then
    >&2 echo "Warning: Could not find required executable ${BUILDDIR}/bin/spicy-doc, aborting"
    exit 0
fi

trap 'rm -rf "${AUTOGEN_STAGE}"' EXIT
rm -rf "${AUTOGEN_STAGE}" && mkdir -p "${AUTOGEN_STAGE}"

"${ROOTDIR}/doc/scripts/autogen-spicy-lib" functions spicy <"${ROOTDIR}/spicy/lib/spicy.spicy" >"${AUTOGEN_STAGE}/spicy-functions.spicy" || exit 1
"${ROOTDIR}/doc/scripts/autogen-spicy-lib" types spicy <"${ROOTDIR}/spicy/lib/spicy.spicy" >"${AUTOGEN_STAGE}/spicy-types.spicy" || exit 1
"${ROOTDIR}/doc/scripts/autogen-spicy-lib" types filter <"${ROOTDIR}/spicy/lib/filter.spicy" >"${AUTOGEN_STAGE}/filter-types.spicy" || exit 1

# Include these namespaces into the autogenerated type reference.
SPICY_TYPES=address,bitfield,bool,bytes,enum,exception,integer,interval,list,map,optional,port,real,regexp,set,sink,stream,string,struct,time,tuple,unit,vector
("${SPICYDOC}" | ${SPICYDTR} -t "${SPICY_TYPES}" -d "${AUTOGEN_STAGE}/types") || exit 1

# Create list of reserved keywords.
cat "${ROOTDIR}"/spicy/toolchain/src/compiler/parser/scanner.ll |
    grep "return.*token::" |
    grep '^[a-zA-Z_]\{2,\}[^{}]* ' |
    awk '{print $1}' |
    sort >"${AUTOGEN_STAGE}"/reserved-keywords.txt

update_architecture_diagram

# All done, move staged files to final location where changed.
# "-rlpgo" is "-a" minus "-tD".
mkdir -p "${AUTOGEN_FINAL}" &&
    rsync -rlpgo --update --checksum --omit-dir-times --itemize-changes --out-format='Updating %n' "${AUTOGEN_STAGE}/" "${AUTOGEN_FINAL}"
