@echo off
setlocal EnableExtensions
title PCNet Remote Support Installer

net session >nul 2>&1
if errorlevel 1 (
  powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)

set "WORKDIR=%TEMP%\PCNetAccess"
set "AGENT=%WORKDIR%\RemoteAccess.exe"

echo.
echo Installing PCNet Remote Support...
echo.

if exist "%ProgramData%\JWrapper-Remote Access" (
  echo Clearing a previous incomplete install...
  net stop "Remote Access Service" >nul 2>&1
  rmdir /s /q "%ProgramData%\JWrapper-Remote Access" 2>nul
)

mkdir "%WORKDIR%" 2>nul
del /q "%AGENT%" 2>nul

echo Downloading installer (about 55 MB)...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
  "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $u='https://mail.pcnetconsult.com:8008/access/Remote Access-windows64-offline.exe?language=en&app=3241885957465604875516100528267321315'; $out=$env:TEMP + '\PCNetAccess\RemoteAccess.exe'; New-Item -ItemType Directory -Force -Path (Split-Path $out) | Out-Null; Invoke-WebRequest -Uri $u -OutFile $out -UseBasicParsing; $len=(Get-Item $out).Length; $fs=[IO.File]::OpenRead($out); $b=New-Object byte[] 2; [void]$fs.Read($b,0,2); $fs.Close(); if ([char]$b[0] -ne 'M' -or [char]$b[1] -ne 'Z') { Remove-Item $out -Force; throw 'Downloaded file is not an EXE' }; Write-Host ('Downloaded {0:N0} bytes' -f $len)"
if errorlevel 1 (
  echo Download failed or the server returned a web page instead of the installer.
  pause
  exit /b 1
)

echo Running silent install...
"%AGENT%" /S /ForceVerification
set "RC=%ERRORLEVEL%"

echo.
if "%RC%"=="0" (
  echo Install finished. You can close this window.
) else (
  echo Installer exited with code %RC%.
  echo If the agent is missing, send the newest log from:
  echo   %ProgramData%\JWrapper-Remote Access\logs
)
echo.
pause
endlocal
