#!/bin/bash
set -euo pipefail

REPO_DIR="$(cd "$(dirname "$0")" && pwd)"
MIRROR_DIR="/home/lisa/nexlab.net/files"
WEB_DIR="/home/web/lisa.nexlab.net/files"
RELEASE_DIR="$REPO_DIR/release"
BUNDLE_DIR="$RELEASE_DIR/bundle"
LINUX_WRAPPER_SRC="$REPO_DIR/deploy/linux/install-node.sh"
LINUX_WRAPPER_OUT="$BUNDLE_DIR/hermes-node-agent-installer.sh"
WINDOWS_INSTALLER=""
CHROME_EXTENSION=""

pick_first_existing() {
  for p in "$@"; do
    if [ -f "$p" ]; then
      printf '%s\n' "$p"
      return 0
    fi
  done
  return 1
}

echo "=== Hermes Node Agent Release ==="
cd "$REPO_DIR"

./build-linux.sh

mkdir -p "$BUNDLE_DIR" "$MIRROR_DIR"
cp "$LINUX_WRAPPER_SRC" "$LINUX_WRAPPER_OUT"

WINDOWS_INSTALLER=$(pick_first_existing \
  "$REPO_DIR/deploy/windows/hermes-node-agent-installer.exe" \
  "$REPO_DIR/windows/dist/hermes-node-agent-installer.exe" \
  "$REPO_DIR/release/hermes-node-agent-installer.exe")

CHROME_EXTENSION=$(pick_first_existing \
  "$REPO_DIR/release/bundle/hermes-browser-agent-chrome-extension.zip" \
  "$REPO_DIR/chrome-extension.zip" \
  "$REPO_DIR/dist/hermes-node-chrome.zip")

if [ -z "$WINDOWS_INSTALLER" ]; then
  echo "❌ Windows installer not found in expected locations"
  exit 1
fi
if [ -z "$CHROME_EXTENSION" ]; then
  echo "❌ Chrome extension zip not found in expected locations"
  exit 1
fi

if [ "$WINDOWS_INSTALLER" != "$BUNDLE_DIR/hermes-node-agent-installer.exe" ]; then
  cp "$WINDOWS_INSTALLER" "$BUNDLE_DIR/hermes-node-agent-installer.exe"
fi
if [ "$CHROME_EXTENSION" != "$BUNDLE_DIR/hermes-browser-agent-chrome-extension.zip" ]; then
  cp "$CHROME_EXTENSION" "$BUNDLE_DIR/hermes-browser-agent-chrome-extension.zip"
fi

rm -f "$RELEASE_DIR/hermes-node-agent-bundle.zip"
(
  cd "$BUNDLE_DIR"
  zip -q -r ../hermes-node-agent-bundle.zip .
)

cp "$RELEASE_DIR/hermes-node-agent-bundle.zip" "$MIRROR_DIR/hermes-node-agent-bundle.zip"
cp "$LINUX_WRAPPER_OUT" "$MIRROR_DIR/hermes-node-agent-installer.sh"

sudo cp "$RELEASE_DIR/hermes-node-agent-bundle.zip" "$WEB_DIR/hermes-node-agent-bundle.zip"
sudo cp "$LINUX_WRAPPER_OUT" "$WEB_DIR/hermes-node-agent-installer.sh"
sudo chown www-data:www-data "$WEB_DIR/hermes-node-agent-bundle.zip" "$WEB_DIR/hermes-node-agent-installer.sh"
sudo chmod 644 "$WEB_DIR/hermes-node-agent-bundle.zip" "$WEB_DIR/hermes-node-agent-installer.sh"

echo "--- SHA256 parity ---"
sha256sum \
  "$RELEASE_DIR/hermes-node-agent-bundle.zip" \
  "$MIRROR_DIR/hermes-node-agent-bundle.zip" \
  "$WEB_DIR/hermes-node-agent-bundle.zip"
printf '\n'
sha256sum \
  "$LINUX_WRAPPER_OUT" \
  "$MIRROR_DIR/hermes-node-agent-installer.sh" \
  "$WEB_DIR/hermes-node-agent-installer.sh"

echo "--- Live download verification ---"
curl -fsSL https://lisa.nexlab.net/files/hermes-node-agent-bundle.zip -o /tmp/hermes-node-agent-bundle-live.zip
curl -fsSL https://lisa.nexlab.net/files/hermes-node-agent-installer.sh -o /tmp/hermes-node-agent-installer-live.sh
sha256sum /tmp/hermes-node-agent-bundle-live.zip /tmp/hermes-node-agent-installer-live.sh
curl -I -s https://lisa.nexlab.net/files/hermes-node-agent-bundle.zip | sed -n '1,5p'

echo "✅ Release complete"
