@echo off
setlocal
title printHS installer - Hack Snack
REM printHS - install, repair or update on an outlet PC. Same file for every outlet.
REM   Download: https://download.hack-snack.com/install-printHS.bat
REM   Double-click. It asks for Administrator rights by itself.
REM   First install only: when asked "Enter the printHS API key", paste this PC's key and press Enter.
REM   Re-running it later keeps config.ini (the key) and installs the latest released printHS.exe.
REM What setup.ps1 sets up: a Windows service that starts with Windows, restarts itself if it
REM stops, and updates itself every hour from the signed release manifest (see BUILD.md).
REM Keep this file ASCII with CRLF line endings (cmd.exe misreads LF-only .bat files).

net session >nul 2>&1
if %errorlevel% neq 0 (
  powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)

powershell -NoProfile -ExecutionPolicy Bypass -Command ^
 "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';" ^
 "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;" ^
 "New-Item C:\printHS -ItemType Directory -Force | Out-Null; Set-Location C:\printHS;" ^
 "$drive = 'https://drive.usercontent.google.com/download?export=download&confirm=t&id=';" ^
 "Write-Host '==> Downloading the installer' -ForegroundColor Cyan;" ^
 "Invoke-WebRequest ($drive + '1Fdvb6axNyzvLCS-45QlZfEHYH72URsRo') -UseBasicParsing -OutFile setup.ps1;" ^
 "Invoke-WebRequest ($drive + '1ydFdC8vCwKq0LjRf3IwYCO3YUhNKno2k') -UseBasicParsing -OutFile config.example.ini;" ^
 "if (Get-Service printHS -ErrorAction SilentlyContinue) { Write-Host '==> Stopping the running printHS' -ForegroundColor Cyan; Stop-Service printHS -Force -ErrorAction SilentlyContinue; Start-Sleep 3 };" ^
 "if (Test-Path printHS.exe) { Move-Item printHS.exe printHS.exe.prev -Force };" ^
 "$ErrorActionPreference = 'Continue';" ^
 "powershell -NoProfile -ExecutionPolicy Bypass -File .\setup.ps1; $setupFailed = ($LASTEXITCODE -ne 0) -or -not (Test-Path printHS.exe);" ^
 "if ($setupFailed -and (Test-Path printHS.exe.prev)) {" ^
 "  Move-Item printHS.exe.prev printHS.exe -Force;" ^
 "  Write-Host 'Installing the new version failed - kept the previous one.' -ForegroundColor Yellow;" ^
 "  Start-Service printHS -ErrorAction SilentlyContinue" ^
 "} else { Remove-Item printHS.exe.prev -Force -ErrorAction SilentlyContinue };" ^
 "for ($i = 0; $i -lt 20 -and -not (Test-Path printHS.log); $i++) { Start-Sleep 1 }; Start-Sleep 5;" ^
 "$st = (Get-Service printHS -ErrorAction SilentlyContinue).Status; Write-Host '';" ^
 "if ($st -eq 'Running') { Write-Host 'OK - printHS is running. It starts with Windows and updates itself.' -ForegroundColor Green }" ^
 "else { Write-Host ('PROBLEM - printHS service status: ' + $st + '. Send a photo of this window.') -ForegroundColor Red };" ^
 "Write-Host ''; Write-Host '===== printHS.log (last 20 lines) =====';" ^
 "Get-Content C:\printHS\printHS.log -Tail 20 -Encoding UTF8 -ErrorAction SilentlyContinue"

echo.
pause
