#!/bin/sh
# Patched entrypoint: fixes the sed ordering bug in the server-ui image.
# The original entrypoint inserts <base href> then rewrites ALL href="/..."
# including the just-inserted base tag, causing /server/server/ doubling.

BASE_PATH="${BASE_PATH:-/}"

if [ "$BASE_PATH" != "/" ]; then
  BASE_PATH=$(echo "$BASE_PATH" | sed 's#/*$#/#; s#^/*#/#')

  INDEX="/usr/share/nginx/html/index.html"
  # Rewrite absolute asset paths FIRST (before inserting <base>)
  sed -i "s|href=\"/|href=\"${BASE_PATH}|g; s|src=\"/|src=\"${BASE_PATH}|g" "$INDEX"
  # THEN inject <base> tag
  sed -i "s|<head>|<head><base href=\"${BASE_PATH}\">|" "$INDEX"

  echo "BASE_PATH set to ${BASE_PATH} — rewrote index.html"
fi

exec /docker-entrypoint.sh "$@"
