#!/bin/bash

#   BAREOS® - Backup Archiving REcovery Open Sourced
#
#   Copyright (C) 2026 Bareos GmbH & Co. KG
#
#   This program is Free Software; you can redistribute it and/or
#   modify it under the terms of version three of the GNU Affero General Public
#   License as published by the Free Software Foundation and included
#   in the file LICENSE.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#   Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
#   02110-1301, USA.

set -e
set -o pipefail
set -u

TestName="$(basename "$(pwd)")"
export TestName

. ./environment
. "${BAREOS_SCRIPTS_DIR}/functions"

proxy_port="${BAREOS_WEBUI_PROXY_PORT}"
bundle_dir="${current_test_directory}/webui-vue-dist"
proxy_log="${tmp}/webui-vue-proxy.out"
frontend_log="${tmp}/webui-vue-frontend.out"
proxy_config="${tmp}/bareos-webui-proxy.ini"
proxy_pid_file="${tmp}/webui-vue-proxy.pid"
frontend_pid_file="${tmp}/webui-vue-frontend.pid"
PROXY_PID=""
FRONTEND_PID=""

function cleanup_on_error()
{
  local rc=$?
  if [[ $rc -eq 0 ]]; then
    return 0
  fi

  if [[ -n "${FRONTEND_PID}" ]] && kill -0 "${FRONTEND_PID}" >/dev/null 2>&1; then
    kill "${FRONTEND_PID}" || true
    wait "${FRONTEND_PID}" || true
  fi
  if [[ -n "${PROXY_PID}" ]] && kill -0 "${PROXY_PID}" >/dev/null 2>&1; then
    kill "${PROXY_PID}" || true
    wait "${PROXY_PID}" || true
  fi
  rm -f "${frontend_pid_file}" "${proxy_pid_file}"
  stop_bareos || true
}

function wait_for_http()
{
  local url="$1"
  local attempt
  for attempt in $(seq 1 100); do
    if curl --silent --fail "$url" >/dev/null; then
      return 0
    fi
    sleep 0.2
  done
  return 1
}

function wait_for_tcp()
{
  local host="$1"
  local port="$2"
  local attempt
  for attempt in $(seq 1 100); do
    if (echo >/dev/tcp/"$host"/"$port") >/dev/null 2>&1; then
      return 0
    fi
    sleep 0.2
  done
  return 1
}

trap cleanup_on_error EXIT

${BAREOS_SCRIPTS_DIR}/cleanup
${BAREOS_SCRIPTS_DIR}/setup

setup_data

bin/bareos start
bin/bareos status
print_debug "$(bin/bconsole <<<"status dir")"

proxy_tls_psk_disable=false
if [[ "${BAREOS_WEBUI_PROXY_DISABLE_TLS_PSK}" = "1" ]]; then
  proxy_tls_psk_disable=true
fi

cat >"${proxy_config}" <<END_OF_PROXY_CONFIG
[listen]
ws_host = 127.0.0.1
ws_port = ${proxy_port}

[director:bareos-dir]
host = 127.0.0.1
port = ${BAREOS_DIRECTOR_PORT}
director_name = bareos-dir
tls_psk_disable = ${proxy_tls_psk_disable}
END_OF_PROXY_CONFIG

pushd "${CMAKE_SOURCE_DIR}/webui-vue" >/dev/null
if ! [ -x node_modules/.bin/playwright ] || ! [ -x node_modules/.bin/vite ]; then
  echo "Missing webui-vue npm dependencies. Install them before configuring and running the Playwright systemtests." >&2
  exit 1
fi

"${NPM_EXECUTABLE}" run generate:version >"${tmp}/webui-vue-generate-version.out" 2>&1

VITE_DIRECTOR_WS_URL="ws://127.0.0.1:${proxy_port}" \
  VITE_DIRECTOR_HOST="127.0.0.1" \
  VITE_DIRECTOR_PORT="${BAREOS_DIRECTOR_PORT}" \
  "${NPM_EXECUTABLE}" exec vite build -- --outDir "${bundle_dir}" \
  >"${tmp}/webui-vue-build.out" 2>&1
popd >/dev/null

"${BAREOS_WEBUI_PROXY_BINARY}" --config "${proxy_config}" >"${proxy_log}" 2>&1 &
PROXY_PID=$!
printf '%s\n' "${PROXY_PID}" >"${proxy_pid_file}"

"${NODE_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/webui-vue/scripts/serve-spa.mjs" \
  --host 127.0.0.1 \
  --port "${BAREOS_WEBUI_PORT}" \
  --root "${bundle_dir}" \
  >"${frontend_log}" 2>&1 &
FRONTEND_PID=$!
printf '%s\n' "${FRONTEND_PID}" >"${frontend_pid_file}"

if ! wait_for_tcp 127.0.0.1 "${proxy_port}"; then
  echo "bareos-webui-proxy did not start:" >&2
  cat "${proxy_log}" >&2
  exit 1
fi

if ! wait_for_http "${BAREOS_WEBUI_BASE_URL}"; then
  echo "webui-vue frontend did not start:" >&2
  cat "${frontend_log}" >&2
  exit 1
fi
