#!/bin/bash
#
# Copyright (c) 2020-2023 by the Zeek Project. See LICENSE for details.
#
# Tool to update autogenerated docs that require external files. Must be
# run manually and requires access to the Spicy TFTP analyzer.

set -e

if [ $# != 1 ]; then
    echo "usage: $(basename "$0") <spicy-tftp-repo>"
    exit 1
fi

TFTP=$1

if [ ! -d "${TFTP}"/analyzer ]; then
    echo "${TFTP} does not seem to point to a spicy-tftp repository."
    exit 1
fi

set -o errexit
set -o nounset

ZEEK="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)/../../.."
DOC="${ZEEK}/doc"
SPICY="${ZEEK}/auxil/spicy"
SPICYDOC="${ZEEK}/build/auxil/spicy/bin/spicy-doc"
AUTOGEN_FINAL="${ZEEK}/doc/devel/spicy/autogen"

if [ ! -x "${SPICYDOC}" ]; then
    >&2 echo "Warning: Could not find spicy-doc in build directory, aborting"
    exit 0
fi

"${SPICY}/doc/scripts/autogen-spicy-lib" functions zeek <"${ZEEK}/scripts/spicy/zeek.spicy" >"${AUTOGEN_FINAL}/zeek-functions.spicy" || exit 1

# Copy some static files over.
cp "${TFTP}"/scripts/main.zeek "${AUTOGEN_FINAL}"/tftp.zeek || exit 1
cp "${TFTP}"/analyzer/tftp.spicy "${AUTOGEN_FINAL}"/tftp.spicy || exit 1
cp "${TFTP}"/analyzer/tftp.evt "${AUTOGEN_FINAL}"/tftp.evt || exit 1

# Copy some files from the Zeek source tree so that zeek-docs remains standaline for CI.
cp "${ZEEK}/scripts/base/frameworks/spicy/init-bare.zeek" "${AUTOGEN_FINAL}/"
cp "${ZEEK}/scripts/base/frameworks/spicy/init-framework.zeek" "${AUTOGEN_FINAL}/"
cp "${ZEEK}/auxil/spicy/doc/scripts/spicy-pygments.py" "${DOC}/ext"
