#!/usr/bin/env bash
set -euo pipefail

bundle="$(realpath "$1")"

required_patterns=(
  '/jrpg/index.html$'
  '/jrpg/jrpg.css$'
  '/jrpg/jrpg.js$'
  '/pixel-mplus-12-regular.ttf$'
  '/copilot_sidecar.zip$'
  '/litellm_proxy.zip$'
  '/litellm_config.yaml$'
  '/inference_stack.sh$'
  '/inference_sidecar_launcher.sh$'
  '/production_entrypoint.sh$'
  '/copilot$'
  '/python3$'
)

files="$(find -L "$bundle" -type f)"
for pattern in "${required_patterns[@]}"; do
  if ! grep -Eq "$pattern" <<<"$files"; then
    echo "Production bundle is missing inference runtime: $pattern" >&2
    exit 1
  fi
done

if grep -Eq '/mock_sidecar($|\\.)|/mock_responses\\.json$' <<<"$files"; then
  echo "Production bundle contains the development mock inference runtime" >&2
  exit 1
fi

config="$(find -L "$bundle" -type f -name litellm_config.yaml -print -quit)"
grep -q 'os.environ/LITELLM_MASTER_KEY' "$config"
if grep -Eq 'sk-[A-Za-z0-9_-]{16,}|gh[opurs]_[A-Za-z0-9]{16,}' "$config"; then
  echo "Production LiteLLM config contains a credential" >&2
  exit 1
fi

entrypoint="$(find -L "$bundle" -type f -name production_entrypoint.sh -print -quit)"
bash -n "$entrypoint"
grep -q 'mrjunejune_server_binary' "$entrypoint"

sidecar="$(find -L "$bundle" -type f -path '*/mrjunejune/inference/copilot_sidecar.zip' -print -quit)"
litellm="$(find -L "$bundle" -type f -path '*/mrjunejune/inference/litellm_proxy.zip' -print -quit)"
copilot_cli="$(find -L "$bundle" -type f -path '*/+http_archive+copilot_cli_linux_x86_64/copilot' -print -quit)"
python="$(find -L "$bundle" -type f -path '*/rules_python++python+python_3_11_*/bin/python3' -print -quit)"
state="$(mktemp -d)"
trap 'rm -rf "$state"' EXIT

output="$(
  printf '%s\n' '{"command":"shutdown","request_id":"bundle-test","conversation_id":null}' |
    COPILOT_SIDECAR_HOME="$state/copilot" \
    LITELLM_BASE_URL='http://127.0.0.1:1/v1' \
    LITELLM_MODEL='jrpg-copilot' \
    LITELLM_WIRE_API='completions' \
    "$python" "$sidecar" "$copilot_cli"
)"
grep -q '"type":"ready"' <<<"$output"
"$python" "$litellm" --help >/dev/null
