#!/bin/bash
# Release script: builds Windows installer and publishes to website
#
# This script:
# 1. Commits any pending changes (code or installer scripts)
# 2. Builds Windows installer via Wine+PyInstaller+NSIS
# 3. Uploads installer to website (lisa.nexlab.net/files/)
#
# Usage: ./release-windows.sh [version]
#   Example: ./release-windows.sh 2.0.0

set -e
cd "$(cd "$(dirname "$0")" && pwd)/.."

VERSION="${1:-2.0.0}"
ZIP_NAME="hermes-node-agent-v${VERSION}.zip"

echo "=== Hermes Node Agent Release v${VERSION} ==="
echo ""

# ── Step 1: Commit all changes ────────────────────────────────────────────
echo "Step 1: Committing changes to GitLab..."

# Check if there are changes to commit
if [ -n "$(git status --porcelain)" ]; then
    echo "  Changes detected:"
    git status --short
    
    # Add all tracked files + new files (but not ignored)
    git add -A
    
    # Commit with version tag
    git commit -m "Release v${VERSION}: Windows installer + node agent updates"
    
    # Push to GitLab
    git push origin main
    echo "  ✅ Pushed to GitLab"
else
    echo "  No changes to commit"
fi

# ── Step 2: Build Windows installer ───────────────────────────────────────
echo ""
echo "Step 2: Building Windows installer..."
bash windows/build-all.sh

if [ ! -f "windows/dist/hermes-node-agent-installer.exe" ]; then
    echo "❌ Build failed — aborting release"
    exit 1
fi

echo "✅ Windows installer built"

# ─ Step 3: Create release bundle ─────────────────────────────────────────
echo ""
echo "Step 3: Creating release bundle..."

# Create release directory
mkdir -p release
cp windows/dist/hermes-node-agent-installer.exe release/
cp deploy/linux/install-node.sh release/

# Zip it
cd release
zip -q "${ZIP_NAME}" hermes-node-agent-installer.exe install-node.sh
cd ..

echo "✅ Release bundle: release/${ZIP_NAME}"
echo ""
echo "Files:"
ls -lh release/${ZIP_NAME}

# ── Step 4: Upload to website ─────────────────────────────────────────────
echo ""
echo "Step 4: Uploading to website (lisa.nexlab.net)..."

# Upload via scp/rsync to the web server
# Adjust these to match your actual website host/path
WEBSITE_USER="lisa"
WEBSITE_HOST="lisa.nexlab.net"
WEBSITE_PATH="/var/www/html/files/"

# Check if ssh key is available
if ssh -o BatchMode=yes "${WEBSITE_USER}@${WEBSITE_HOST}" "echo ready" 2>/dev/null; then
    echo "  Uploading via scp..."
    scp "release/${ZIP_NAME}" "${WEBSITE_USER}@${WEBSITE_HOST}:${WEBSITE_PATH}"
    echo "  ✅ Uploaded to https://lisa.nexlab.net/files/${ZIP_NAME}"
    
    # Also upload the Windows installer directly
    scp windows/dist/hermes-node-agent-installer.exe "${WEBSITE_USER}@${WEBSITE_HOST}:${WEBSITE_PATH}"
    echo "  ✅ Uploaded installer: https://lisa.nexlab.net/files/hermes-node-agent-installer.exe"
else
    echo "  ⚠️  SSH key not found or auth failed"
    echo "  Manually upload these files:"
    echo "    release/${ZIP_NAME}"
    echo "    windows/dist/hermes-node-agent-installer.exe"
    echo "  To: https://lisa.nexlab.net/files/"
fi

echo ""
echo "╔════════════════════════════════════════════════════════╗"
echo "║  Release v${VERSION} READY                                 ║"
echo "╚════════════════════════════════════════════════════════╝"
echo ""
echo "GitLab:     https://gitlab.nosuchhost.com/lisa/hermes-node-agent"
echo "Website:    https://lisa.nexlab.net/files/hermes-node-agent-installer.exe"
echo "Direct zip: https://lisa.nexlab.net/files/${ZIP_NAME}"
echo ""
