#!/usr/bin/env sh
# ---------------------------------------------------------------------------
# Publishes the Sparkplug edge-node simulator into ./simulator/app/, which the
# compose file's `sparkplug-sim` service bind-mounts and runs.
#
# WHY A PUBLISHED DIRECTORY RATHER THAN AN IMAGE BUILD. This fixture is deployed by
# rsyncing THIS directory to /opt/otopcua-mqtt on the Docker host (see the compose
# banner and infra/README.md §1) — the host never sees the repo, so a Dockerfile whose
# build context is the solution root could not run there. A framework-dependent publish
# is portable IL: it runs unchanged under the stock dotnet/runtime image on the host,
# whatever this machine's architecture is.
#
# Usage, from a checkout (needs the .NET 10 SDK):
#
#   ./publish-simulator.sh
#   rsync -av --exclude 'secrets/' ./ dohertj2@10.100.0.35:/opt/otopcua-mqtt/
#   ssh dohertj2@10.100.0.35 'cd /opt/otopcua-mqtt \
#       && MQTT_FIXTURE_USERNAME=otopcua MQTT_FIXTURE_PASSWORD=<same> \
#          docker compose --profile sparkplug up -d'
#
# ./simulator/app/ is gitignored: it is build output, regenerated per machine, exactly
# like ./secrets/.
# ---------------------------------------------------------------------------
set -eu

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
PROJECT="$SCRIPT_DIR/../SparkplugSimulator/ZB.MOM.WW.OtOpcUa.Driver.Mqtt.SparkplugSimulator.csproj"
OUT="$SCRIPT_DIR/simulator/app"

command -v dotnet >/dev/null 2>&1 || { echo "error: dotnet SDK not found on PATH." >&2; exit 2; }
[ -f "$PROJECT" ] || { echo "error: simulator project not found at $PROJECT" >&2; exit 2; }

echo "==> publishing $(basename "$PROJECT") -> $OUT"
rm -rf "$OUT"
dotnet publish "$PROJECT" -c Release -o "$OUT" --nologo

echo "==> done:"
ls -1 "$OUT" | head -20
echo
echo "Next: rsync this directory to the Docker host and bring the stack up with"
echo "  docker compose --profile sparkplug up -d"
