#!/bin/bash
# Copyright (C) 2026 Stefy Lanza <stefy@nexlab.net>
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyleft: this program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Build Windows offline installer for Hermes Node Agent
# Requires: makensis (NSIS compiler), wget/curl, python3 + pip

set -e
cd "$(cd "$(dirname "$0")"/.. && pwd)"
WORKDIR=$(mktemp -d)
echo "Build directory: $WORKDIR"

# Download components
echo "Downloading Python 3.13.1 embeddable..."
wget -q "https://www.python.org/ftp/python/3.13.1/python-3.13.1-embed-amd64.zip" -O "$WORKDIR/python.zip"

echo "Downloading websockets..."
python3 -m pip download websockets --platform win_amd64 --python-version 3.13 --only-binary=:all: -d "$WORKDIR"

# Build payload
PAYLOAD="$WORKDIR/payload"
mkdir -p "$PAYLOAD"/{python,node,config}

# Extract Python
unzip -q "$WORKDIR/python.zip" -d "$PAYLOAD/python"
mkdir -p "$PAYLOAD/python/Lib/site-packages"

# Install websockets
WHEEL=$(ls "$WORKDIR"/websockets*.whl)
unzip -q "$WHEEL" -d "$PAYLOAD/python/Lib/site-packages"

# Copy sources
cp ../hermes_node_agent.py "$PAYLOAD/node/"
cp ../browser_controller.py "$PAYLOAD/node/"

# sexec placeholder
echo '@echo off' > "$PAYLOAD/sexec.bat"
echo 'echo "sexec not implemented on Windows"' >> "$PAYLOAD/sexec.bat"
echo 'exit /b 0' >> "$PAYLOAD/sexec.bat"

# Config template
cat > "$PAYLOAD/config/config.json" <<'EOF'
{
  "gateway_url": "wss://YOUR-GATEWAY:8765",
  "node_name": "WIN-NODE",
  "token": "YOUR-TOKEN-HERE",
  "sexec_path": "./sexec.bat",
  "reconnect_interval": 5,
  "heartbeat_interval": 30,
  "capabilities": ["exec"]
}
EOF

# Launchers
cat > "$PAYLOAD/setup.bat" <<'EOF'
@echo off
echo === Hermes Node Agent Setup ===
set /p GATEWAY_HOST="Gateway host: "
set /p NODE_NAME="Node name [%COMPUTERNAME%]: "
if "%NODE_NAME%"=="" set NODE_NAME=%COMPUTERNAME%
set /p GATEWAY_TOKEN="Gateway token: "
(
echo {
echo   "gateway_url": "ws://%GATEWAY_HOST%:8765",
echo   "node_name": "%NODE_NAME%",
echo   "token": "%GATEWAY_TOKEN%",
echo   "sexec_path": ".\sexec.bat",
echo   "reconnect_interval": 5,
echo   "heartbeat_interval": 30,
echo   "capabilities": ["exec"]
echo }
) > config\config.json
echo Configuration saved. Run agent.bat to start.
pause
EOF

cat > "$PAYLOAD/agent.bat" <<'EOF'
@echo off
cd /d "%~dp0"
set PYTHONHOME=python
set PYTHONPATH=python;python\Lib;python\Lib\site-packages
set PATH=%~dp0python;%PATH%
python.exe node\hermes_node_agent.py --config config\config.json
pause
EOF

# NSIS script
cat > "$WORKDIR/installer.nsi" <<'NSIS'
!define APP "Hermes Node Agent"
!define VER "2.0"
Name "${APP} ${VER}"
OutFile "hermes-node-agent-installer.exe"
InstallDir "$PROGRAMFILES64\HermesNode"
RequestExecutionLevel admin

!include "MUI2.nsh"
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE "English"

Section "Install"
  SetOutPath "$INSTDIR"
  File /r "payload\*.*"
  WriteUninstaller "$INSTDIR\uninstall.exe"
  CreateDirectory "$SMPROGRAMS\${APP}"
  CreateShortCut "$SMPROGRAMS\${APP}\Setup.lnk" "$INSTDIR\setup.bat"
  CreateShortCut "$SMPROGRAMS\${APP}\Start.lnk" "$INSTDIR\agent.bat"
  CreateShortCut "$SMPROGRAMS\${APP}\Uninstall.lnk" "$INSTDIR\uninstall.exe"
SectionEnd

Section "Uninstall"
  RMDir /r "$INSTDIR"
  RMDir /r "$SMPROGRAMS\${APP}"
SectionEnd
NSIS

# Compile
echo "Compiling NSIS installer..."
cd "$WORKDIR"
makensis installer.nsi

# Move output
cp hermes-node-agent-installer.exe "$OLDPWD/../../deploy/windows/"
echo "Build complete: deploy/windows/hermes-node-agent-installer.exe"
rm -rf "$WORKDIR"
