#!/bin/bash
# Launch wrapper for the Etherpad snap daemon.
#
# 1. On first run, copy settings.json.template -> $SNAP_COMMON/etc/settings.json
#    so the admin can edit it outside the read-only squashfs. Patch the
#    seeded file so the DB (switched from dev-only dirty to sqlite) / ip /
#    port point at writable paths and pick up env-var overrides.
# 2. Create writable data dirs under $SNAP_COMMON.
# 3. Apply `snap set` overrides (port, ip) via env vars — Etherpad's
#    settings.json supports ${PORT:9001}-style substitution natively.
# 4. Exec Node with tsx loader to run server.ts, passing the seeded
#    settings file via --settings (Etherpad reads `argv.settings`, not
#    an env var, so EP_SETTINGS alone would be ignored).
set -euo pipefail

APP_DIR="${SNAP}/opt/etherpad"
NODE_BIN="${SNAP}/opt/node/bin/node"

export PATH="${SNAP}/opt/node/bin:${SNAP}/usr/bin:${SNAP}/bin:${PATH}"

ETC_DIR="${SNAP_COMMON}/etc"
VAR_DIR="${SNAP_COMMON}/var"
LOG_DIR="${SNAP_COMMON}/logs"
# etherpad-app-var/ is the symlink target for ${APP_DIR}/var inside the
# read-only snap mount — see snapcraft.yaml step 4. Etherpad writes its
# plugin registry (installed_plugins.json) here.
APP_VAR_DIR="${SNAP_COMMON}/etherpad-app-var"
mkdir -p "${ETC_DIR}" "${VAR_DIR}" "${LOG_DIR}" "${APP_VAR_DIR}"

SETTINGS="${ETC_DIR}/settings.json"
if [ ! -f "${SETTINGS}" ]; then
  echo "[etherpad-snap] bootstrapping ${SETTINGS} from template"
  cp "${APP_DIR}/settings.json.template" "${SETTINGS}"
  # Switch the template's dev-only dirty default to sqlite and point it
  # at $SNAP_COMMON (absolute path, writable under strict confinement).
  # sqlite is shipped by ueberdb2 via the prebuilt rusty-store-kv native
  # module — no additional build deps required.
  sed -i \
    -e 's|"dbType": "dirty"|"dbType": "sqlite"|' \
    -e 's|"filename": "var/dirty.db"|"filename": "'"${VAR_DIR}"'/etherpad.db"|' \
    "${SETTINGS}"
  # Rewrite ip/port literals to Etherpad's env-substitution syntax so
  # `snap set etherpad port=<n>` / `ip=<addr>` actually take effect.
  # Only substitute the first (top-level) occurrence — `dbSettings.port`
  # has the same key name lower down and must not be touched.
  sed -i \
    -e '0,/"ip": "0.0.0.0"/{s|"ip": "0.0.0.0"|"ip": "${IP:0.0.0.0}"|}' \
    -e '0,/"port": 9001/{s|"port": 9001|"port": "${PORT:9001}"|}' \
    "${SETTINGS}"
fi

PORT_OVERRIDE="$(snapctl get port || true)"
IP_OVERRIDE="$(snapctl get ip || true)"
: "${PORT_OVERRIDE:=9001}"
: "${IP_OVERRIDE:=0.0.0.0}"
export PORT="${PORT_OVERRIDE}"
export IP="${IP_OVERRIDE}"

# `tsx` lives in the `src` workspace's node_modules under pnpm's hoisting
# layout, not at ${APP_DIR}/node_modules. Run from src/ so node's import
# resolution finds it (mirrors the `dev` script in src/package.json).
cd "${APP_DIR}/src"
export NODE_ENV=production

# Pass --settings explicitly; Etherpad's Settings loader reads argv only,
# so exporting EP_SETTINGS is not enough to redirect the config file.
# Use bare `tsx` (not `tsx/esm`) to match bin/cleanRun.sh — Etherpad's
# source mixes CJS+ESM and the ESM-only loader trips ERR_REQUIRE_CYCLE.
exec "${NODE_BIN}" --import tsx node/server.ts \
  --settings "${SETTINGS}" \
  "$@"
