#!/usr/bin/env bash

set -euo pipefail

source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/oysterun_service_common.sh"

parse_common_args "$@"
configure_stack_runtime
require_host_port_configured

BOOTSTRAP_NODE="$(resolve_bootstrap_node_command_path)"
load_host_node_npm_lifecycle_bundle verify-persisted "${BOOTSTRAP_NODE}"
NODE_BIN="${NODE_NPM_BUNDLE_NODE_PATH}"
NPM_CLI="${NODE_NPM_BUNDLE_NPM_CLI_PATH}"
NODE_NPM_IDENTITY_DIGEST="${NODE_NPM_BUNDLE_IDENTITY_DIGEST}"
NODE_NPM_IDENTITY_PATH="${NODE_NPM_BUNDLE_IDENTITY_PATH}"
export OYSTERUN_NODE_BIN="${NODE_BIN}"
export OYSTERUN_NPM_CLI="${NPM_CLI}"
export OYSTERUN_NODE_NPM_IDENTITY_DIGEST="${NODE_NPM_IDENTITY_DIGEST}"
export OYSTERUN_NODE_NPM_IDENTITY_PATH="${NODE_NPM_IDENTITY_PATH}"
export OYSTERUN_BOOTSTRAP_NODE_BIN="${NODE_BIN}"

ensure_runtime_dirs
append_service_control_audit "run_host_supervisor" "attempt" "script=tool_scripts/run_oysterun_host.sh"
sync_host_stack_port_config

ensure_stack_dashboard_credentials "${NODE_BIN}"
quarantine_legacy_routec_runtime_env_files_if_present "${NODE_BIN}"
cleanup_legacy_routec_spike0_synapse_processes_if_present "${NODE_BIN}"
ensure_routec_web_chat_static_app "${NODE_BIN}" "${NPM_CLI}"
LAUNCH_PATH="$(build_launch_path "${NODE_BIN}" "${CONFIG_PATH}")"
HOST_CHILD_PID=""
HOST_LAUNCH_LABEL="${LAUNCH_JOB_LABEL:-${HOST_LABEL}}"

cleanup() {
  local exit_code=$?

  append_service_control_audit "run_host_supervisor" "exit" "script=tool_scripts/run_oysterun_host.sh;exit_code=${exit_code};host_child_pid=${HOST_CHILD_PID}"
  clear_host_origin_file
  rm -f "${HOST_PID_FILE}"

  if [[ -n "${HOST_CHILD_PID}" ]] && pid_is_running "${HOST_CHILD_PID}"; then
    append_service_control_audit "process_signal" "attempt" "signal=TERM;reason=run_host_cleanup;label=${HOST_LABEL};pid=${HOST_CHILD_PID}"
    kill "${HOST_CHILD_PID}" 2>/dev/null || true
    append_service_control_audit "process_signal" "done" "signal=TERM;reason=run_host_cleanup;label=${HOST_LABEL};pid=${HOST_CHILD_PID}"
    wait "${HOST_CHILD_PID}" 2>/dev/null || true
  fi

  exit "${exit_code}"
}

trap cleanup EXIT INT TERM

export PATH="${LAUNCH_PATH}"

cd "${HOST_DIR}"

collect_routec_host_env_args

/usr/bin/env \
  OYSTERUN_CONFIG_DIR="${CONFIG_DIR}" \
  OYSTERUN_PORT="${HOST_PORT}" \
  OYSTERUN_REPO_ROOT="${ROOT_DIR}" \
  OYSTERUN_NODE_BIN="${NODE_BIN}" \
  OYSTERUN_NPM_CLI="${NPM_CLI}" \
  OYSTERUN_NODE_NPM_IDENTITY_DIGEST="${NODE_NPM_IDENTITY_DIGEST}" \
  OYSTERUN_NODE_NPM_IDENTITY_PATH="${NODE_NPM_IDENTITY_PATH}" \
  "${ROUTEC_HOST_ENV_ARGS[@]}" \
  "${NODE_BIN}" server.mjs &
HOST_CHILD_PID=$!
append_service_control_audit "run_host_child" "started" "script=tool_scripts/run_oysterun_host.sh;host_child_pid=${HOST_CHILD_PID}"

wait_for_http "${HOST_URL}/health" "Host service" "${HOST_CHILD_PID}" "${HOST_LOG}"
printf '%s\n' "${HOST_CHILD_PID}" > "${HOST_PID_FILE}"
verify_process_cwd "${HOST_CHILD_PID}" "${HOST_DIR}" "Host service"
write_host_origin_file "${HOST_CHILD_PID}" "${HOST_LAUNCH_LABEL}"
append_service_control_audit "run_host_child" "ready" "script=tool_scripts/run_oysterun_host.sh;host_child_pid=${HOST_CHILD_PID}"

child_status=0
if wait "${HOST_CHILD_PID}"; then
  child_status=0
else
  child_status=$?
fi
append_service_control_audit "run_host_child" "exited" "script=tool_scripts/run_oysterun_host.sh;host_child_pid=${HOST_CHILD_PID};child_status=${child_status}"

exit "${child_status}"
