#!/usr/bin/env bash

help() {
    cat << 'EOF'
netops-manifest - output json manifest for deploying netops modules

USAGE:
  netops-manifest \
    [-h|--help] \
    [--netops-modules-file NETOPS_MODULES_FILE] \
    [--lighthouse-version-file LIGHTHOUSE_VERSION_FILE] \
    [--]

EXIT CODES:
  3: unsupported lighthouse version
EOF
}

die() { echo FATAL "$@" >&2; exit 1; }

NETOPS_MODULES_FILE="${NETOPS_MODULES_FILE-/modules}"
LIGHTHOUSE_VERSION_FILE="${LIGHTHOUSE_VERSION_FILE-/etc/sw_version_internal}"

positional_args=()
while [ "$#" -gt 0 ]; do
    case "$1" in
        --netops-modules-file)
            NETOPS_MODULES_FILE="" \
            && shift \
            && [ "$#" -gt 0 ] \
            && NETOPS_MODULES_FILE="$1" \
            && [ ! -z "$NETOPS_MODULES_FILE" ] \
            && shift \
            || die "NETOPS_MODULES_FILE invalid: $NETOPS_MODULES_FILE"
            ;;
        --lighthouse-version-file)
            LIGHTHOUSE_VERSION_FILE="" \
            && shift \
            && [ "$#" -gt 0 ] \
            && LIGHTHOUSE_VERSION_FILE="$1" \
            && [ ! -z "$LIGHTHOUSE_VERSION_FILE" ] \
            && shift \
            || die "LIGHTHOUSE_VERSION_FILE invalid: $LIGHTHOUSE_VERSION_FILE"
            ;;
        -h|--help)
            shift
            help
            exit 0
            ;;
        --)
            shift
            while [ "$#" -gt 0 ]; do
                positional_args+=("$1")
                shift
            done
            ;;
        *)
            positional_args+=("$1")
            shift
            ;;
    esac
done
set -- "${positional_args[@]}"
unset -v positional_args

[ "$#" -eq 0 ] \
|| die "invalid args"

system_version="$( cat "$LIGHTHOUSE_VERSION_FILE" )" \
&& [ ! -z "$system_version" ] \
|| die "LIGHTHOUSE_VERSION_FILE error: $LIGHTHOUSE_VERSION_FILE"

# TODO update this prior to next lighthouse release to fail for unknown versions using the defined exit code
# NOTES:
# - all array values must match ^--.+$, else an error will be raised, meaning all values must be actual docker options
# - only certain options are allowed (whitelisted), see the lhcli.Commands.NetOpsManifest implementation in go
# - certain options pertaining to how the deployment container is run are provided / are potentially overridden by
#   the caller, e.g. those which the caller has a direct dependency on or can be considered to "control"
run_options=()
case "$system_version" in
    *)
        run_options+=(
            --network=netops
            --mount=type=bind,source=/home/root/.ssh/,destination=/root/.ssh
            --mount=type=bind,source=/etc/config/lhvpn,destination=/root/.netops/lhvpn,readonly
            --mount=type=bind,source=/var/run/nom-state.sock,destination=/var/run/nom-state.sock,readonly
            --publish=9002:80
            --env=LH_SYSTEM_VERSION="$system_version"
        )
        ;;
esac

jq -M -s 'add' \
    "$NETOPS_MODULES_FILE" \
    <( printf '%s\0' "${run_options[@]}" | jq -s -R -c -M '. | split("\u0000") | {run_options: .}' ) \
|| die "jq error"
