#!/usr/bin/env bash
#
# Launch the standalone Frontcache server on embedded Jetty 12 (Jakarta EE10).
#
# Replaces the former Jetty 9.4 `start.jar` distribution: that shipped an ASM 5.1
# annotation scanner which cannot read the Java 17 bytecode of the current
# (Jakarta EE10) war, so the webapp failed to deploy and every request 503'd.
# We now run the org.frontcache.server.FrontcacheJettyServer launcher directly;
# its Jetty 12 EE10 jars live in ./server/lib (assembled by the Gradle
# copyServerLibs task) and it deploys server/frontcache-base/webapps/ROOT.war.
#
# The launcher listens on HTTP only (default 9080). TLS is terminated by whatever fronts it -
# an ingress, an ALB, or nginx proxying 443 -> http://127.0.0.1:9080. A worked nginx front door
# is at https://github.com/eternita/frontcache/tree/master/examples/front-door.
#
set -euo pipefail

# ---- resolve this script's location (following symlinks) ---------------------
SCRIPT="${BASH_SOURCE[0]}"
while [ -h "$SCRIPT" ]; do
  DIR="$(cd -P "$(dirname "$SCRIPT")" && pwd)"
  SCRIPT="$(readlink "$SCRIPT")"
  [[ "$SCRIPT" != /* ]] && SCRIPT="$DIR/$SCRIPT"
done
BIN_DIR="$(cd -P "$(dirname "$SCRIPT")" && pwd)"      # .../server/bin
SERVER_DIR="$(cd "$BIN_DIR/.." && pwd)"              # .../server
BASE_DIR="$(cd "$SERVER_DIR/.." && pwd)"             # .../frontcache-server

# ---- layout ------------------------------------------------------------------
LIB_DIR="$SERVER_DIR/lib"
WEBAPP="$SERVER_DIR/frontcache-base/webapps/ROOT.war"
export FRONTCACHE_HOME="$BASE_DIR/FRONTCACHE_HOME"
PORT="${FRONTCACHE_HTTP_PORT:-9080}"

# ---- java --------------------------------------------------------------------
# A per-platform bundle ships its own jlinked runtime in ./runtime, and that wins over
# $JAVA_HOME: the point of such a bundle is to be self-contained, and a host JAVA_HOME
# pointing at an older JVM would break it (the webapp is Java 25 bytecode). An explicit
# FRONTCACHE_JAVA_HOME still overrides everything, so an operator can force a specific JVM.
if [ -n "${FRONTCACHE_JAVA_HOME:-}" ]; then
  JAVA="$FRONTCACHE_JAVA_HOME/bin/java"
elif [ -x "$BASE_DIR/runtime/bin/java" ]; then
  JAVA="$BASE_DIR/runtime/bin/java"
elif [ -n "${JAVA_HOME:-}" ]; then
  JAVA="$JAVA_HOME/bin/java"
else
  JAVA="java"
fi

# ---- sanity checks -----------------------------------------------------------
if [ ! -d "$LIB_DIR" ] || [ -z "$(ls -A "$LIB_DIR" 2>/dev/null)" ]; then
  echo >&2 "ERROR: $LIB_DIR is missing/empty. Run './gradlew :frontcache-server:build' to assemble it."
  exit 1
fi
if [ ! -f "$WEBAPP" ]; then
  echo >&2 "ERROR: war not found: $WEBAPP. Run './gradlew :frontcache-server:build'."
  exit 1
fi

echo "Starting Frontcache on embedded Jetty 12 (port=$PORT)"
echo "  FRONTCACHE_HOME=$FRONTCACHE_HOME"
echo "  webapp=$WEBAPP"
echo "  java=$JAVA"

exec "$JAVA" \
  -cp "$LIB_DIR/*" \
  -Dfrontcache.home="$FRONTCACHE_HOME" \
  -Dfrontcache.jetty.port="$PORT" \
  -Dfrontcache.jetty.webapp="$WEBAPP" \
  -Dlogback.configurationFile="$FRONTCACHE_HOME/conf/fc-logback.xml" \
  org.frontcache.server.FrontcacheJettyServer
