@echo off
setlocal EnableExtensions
set "LOG=%TEMP%\hermes-touch-strixg18-install.log"
call :main > "%LOG%" 2>&1
exit /b %ERRORLEVEL%

:main
echo === HERMES TOUCH-STRIXG18 INSTALL START %DATE% %TIME% ===
echo Hostname:
hostname
echo Whoami:
whoami

net session >nul 2>&1
if %errorlevel% neq 0 (
  echo [ERROR] Please run as Administrator.
  exit /b 1
)

set "TSKEY=tskey-auth-kT1oDv1fJs11CNTRL-cApFweeoJ3hprPk6NZhG4hrHQKy6ZW5pT"
set "TS_EXE=%ProgramFiles%\Tailscale\tailscale.exe"

if not exist "%TS_EXE%" (
  echo [INFO] Installing Tailscale...
  winget install --id Tailscale.Tailscale -e --accept-package-agreements --accept-source-agreements
)
if exist "%TS_EXE%" (
  echo [INFO] Starting Tailscale service...
  sc start Tailscale >nul 2>&1
  echo [INFO] Tailscale logout old state...
  "%TS_EXE%" logout >nul 2>&1
  echo [INFO] Joining tailnet as touch-strixg18...
  "%TS_EXE%" up --authkey %TSKEY% --hostname touch-strixg18 --accept-dns=false
  echo [INFO] Tailscale IP:
  "%TS_EXE%" ip -4
  echo [INFO] Tailscale status:
  "%TS_EXE%" status
) else (
  echo [WARN] Tailscale executable not found after install attempt.
)

echo [INFO] Enabling OpenSSH server...
powershell -NoProfile -ExecutionPolicy Bypass -Command "Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 -ErrorAction SilentlyContinue; Set-Service -Name sshd -StartupType Automatic; Start-Service sshd; if (!(Test-Path $env:USERPROFILE\.ssh)) { New-Item -ItemType Directory -Force $env:USERPROFILE\.ssh | Out-Null }; $ak=$env:USERPROFILE+'\.ssh\authorized_keys'; $pub='ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPuw4BcpCf5uleZef7BSPy2OwpO3fKLXgMey0WxcziD1 hermes-mac-to-anydesk-1376584215'; if (!(Test-Path $ak) -or -not (Select-String -Path $ak -Pattern ([regex]::Escape($pub)) -Quiet)) { Add-Content -Path $ak -Value $pub }; New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 -ErrorAction SilentlyContinue | Out-Null"

echo [INFO] Checking/installing Python 3.12...
if not exist "C:\Python312\python.exe" (
  winget install -e --id Python.Python.3.12 --accept-package-agreements --accept-source-agreements
)
set "PY=C:\Python312\python.exe"
if not exist "%PY%" set "PY=python"
"%PY%" --version
"%PY%" -m ensurepip --upgrade
"%PY%" -m pip install --upgrade pip pipx
"%PY%" -m pipx ensurepath
"%PY%" -m pipx install git+https://github.com/NousResearch/hermes-agent.git

set "HERMES_EXE=%USERPROFILE%\.local\bin\hermes.exe"
if exist "%HERMES_EXE%" (
  "%HERMES_EXE%" --version
  "%HERMES_EXE%" doctor
  "%HERMES_EXE%" skills list
) else (
  echo [ERROR] Hermes exe not found at %HERMES_EXE%
)

echo === HERMES TOUCH-STRIXG18 INSTALL DONE %DATE% %TIME% ===
exit /b 0
