#!/bin/sh

binpac_root="BinPAC_ROOT_DIR-NOTFOUND"
broker_root="/usr/local"
btest_tools_dir="/usr/local/share/btest"
build_type="release"
cmake_dir="/usr/local/share/zeek/cmake"
config_dir="/etc/zeek"
have_af_packet="no"
have_geoip="true"
have_javascript="no"
have_spicy="no"
include_dir="/usr/local/include"
lib_dir="/usr/local/lib"
plugin_dir="/usr/local/lib/zeek/plugins"
prefix="/usr/local"
python_dir="/usr/local/lib/zeek/python"
script_dir="/usr/local/share/zeek"
site_dir="/usr/local/share/zeek/site"
version="8.2.0"
zeek_dist=""
zeekpath=".:/usr/local/share/zeek:/usr/local/share/zeek/policy:/usr/local/share/zeek/site:/usr/local/share/zeek/builtin-plugins"

report_feature() {
    # $1: cmake feature flag value
    input=$(echo "$1" | tr '[:lower:]' '[:upper:]')
    if [ "$input" = "1" ] || [ "$input" = "ON" ] || [ "$input" = "YES" ] || [ "$input" = "TRUE" ] || [ "$input" = "Y" ]; then
        echo "yes"
        exit 0
    fi

    echo "no"
    exit 1
}

add_path() {
    # $1: existing path
    # $2: path to add
    if test -z "$2" || test "$1" = "$2" ||
        echo "$1" | grep -q "^$2:" 2>/dev/null ||
        echo "$1" | grep -q ":$2:" 2>/dev/null ||
        echo "$1" | grep -q ":$2$" 2>/dev/null; then
        echo "$1"
        return
    fi

    echo "$1:$2"
}

# When changing any of these, also update "src/spicy/spicyz/config.h.in".
include_dir=$(add_path "$include_dir" "/usr/include")
include_dir=$(add_path "$include_dir" "/usr/include")
include_dir=$(add_path "$include_dir" "/usr/include")
include_dir=$(add_path "$include_dir" "")
include_dir=$(add_path "$include_dir" "")

usage() {
    echo "Usage: zeek-config [OPTIONS]

Basic options:

  --build_type          Zeek build type as per cmake, lower case (e.g. 'relwithdebinfo')
  --prefix              Toplevel Zeek distribution installation directory
  --version             Zeek version number
  --zeek_dist           Toplevel directory of source tree the distribution built from
  --zeekpath            ZEEKPATH environment variable paths for this distribution

Specific directories in the Zeek distribution:

  --btest_tools_dir     Zeek-related BTest tooling
  --cmake_dir           Zeek's cmake modules
  --config_dir          Configuration files for cluster topology, zkg, etc
  --include_dir         C/C++ header folders for Zeek and related components, colon-separated
  --lib_dir             Toplevel folder for shared libraries, Python packages, etc
  --plugin_dir          Native-code Zeek plugins
  --python_dir          Python packages (Broker, ZeekControl, zkg, etc)
  --script_dir          Toplevel folder for Zeek scripts
  --site_dir            Site-specific Zeek scripts

Toplevel installation directories for third-party components:

  --binpac_root         BinPAC compiler
  --broker_root         Broker communication framework

Feature tests (prints 'yes' if supported; exit code reflects result):

  --have-af-packet        Native AF_PACKET support
  --have-geoip            IP address geolocation & AS lookups
  --have-javascript       JavaScript support
  --have-spicy-analyzers  built-in Spicy analyzers
"
}

if [ $# -eq 0 ]; then
    usage 1>&2
    exit 1
fi

while [ $# -ne 0 ]; do
    case "$1" in
        -*=*) optarg=$(echo "$1" | sed 's/[-_a-zA-Z0-9]*=//') ;;
        *) optarg= ;;
    esac

    case $1 in
        --binpac_root)
            echo $binpac_root
            ;;
        --bro_dist) # For compatibility with legacy Bro plugins.
            echo $zeek_dist
            ;;
        --broker_root)
            echo $broker_root
            ;;
        --bropath) # For compatibility with legacy Bro plugins.
            echo $zeekpath
            ;;
        --btest_tools_dir)
            echo $btest_tools_dir
            ;;
        --build_type)
            echo $build_type
            ;;
        --cmake_dir)
            echo $cmake_dir
            ;;
        --config_dir)
            echo $config_dir
            ;;
        --have-af-packet)
            report_feature "$have_af_packet"
            ;;
        --have-geoip)
            report_feature "$have_geoip"
            ;;
        --have-javascript)
            report_feature "$have_javascript"
            ;;
        --have-spicy-analyzers)
            report_feature "$have_spicy"
            ;;
        --include_dir)
            echo $include_dir
            ;;
        --lib_dir)
            echo $lib_dir
            ;;
        --plugin_dir)
            echo $plugin_dir
            ;;
        --prefix)
            echo $prefix
            ;;
        --python_dir)
            echo $python_dir
            ;;
        --script_dir)
            echo $script_dir
            ;;
        --site_dir)
            echo $site_dir
            ;;
        --version)
            echo $version
            ;;
        --zeek_dist)
            echo $zeek_dist
            ;;
        --zeekpath)
            echo $zeekpath
            ;;
        *)
            usage 1>&2
            exit 1
            ;;
    esac
    shift
done

exit 0
