#!/bin/bash
# One-time local site prep: site.sh for docker/runtime with host port 8080.
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT_DIR"

UID_NOW="$(id -u)"
GID_NOW="$(id -g)"

cat > site.sh <<EOF
#!/bin/bash
# Generated by install-local.sh — laptop runtime under docker/runtime.

export USERID="${UID_NOW}"
export GROUPID="${GID_NOW}"

export IMAGE="\${IMAGE:-thoughtframe-runtime}"
export TAG="\${TAG:-dev}"
export CONTAINER_NAME="thoughtframe"
export COMPOSE_PROJECT_NAME="thoughtframe"
export HTTP_PORT="8080"
export TZ="\${TZ:-America/Halifax}"
export JAVA_OPTS="\${JAVA_OPTS:--Xms512m -Xmx2g}"

export WEBAPP_HOST="../runtime/webapp"
export DATA_HOST="../runtime/data"
export ELASTIC_HOST="../runtime/elastic"

# No fixed Docker network — default compose bridge + host port only.
export NETWORK_NAME=""
export IPV4_ADDRESS=""

export REGISTRY="\${REGISTRY:-registry.thoughtframe.ai}"
export BOOTSTRAP_WAR_URL="\${BOOTSTRAP_WAR_URL:-}"
EOF
chmod +x site.sh

# Pre-create the nested bind-mount targets as this user. If Docker creates
# webapp/WEB-INF/{data,elastic} itself, they become root-owned and the uid-1000
# entrypoint can't unpack the WAR into WEB-INF (see install-testsite.sh).
mkdir -p \
	"$ROOT_DIR/../runtime/webapp/WEB-INF/data" \
	"$ROOT_DIR/../runtime/webapp/WEB-INF/elastic" \
	"$ROOT_DIR/../runtime/data" \
	"$ROOT_DIR/../runtime/elastic"
echo "Wrote site.sh (docker/runtime, host port 8080)"
echo "Next: from monorepo root → ./gradlew :docker:dockerUp"
echo "   or: ./scripts/upgrade.sh"
