@echo off
setlocal
title code212 Computers - remove
net session >nul 2>&1
if %errorlevel% neq 0 (
  echo Asking Windows for administrator permission...
  powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)
echo Looking for code212 Computers on this computer...
set "CODE="
for /f "delims=" %%c in ('powershell -NoProfile -Command "$k = Get-ChildItem 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionUninstall','HKLM:SOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstall' | ForEach-Object { Get-ItemProperty $_.PSPath } | Where-Object { $_.DisplayName -eq 'Library PC' } | Select-Object -First 1; if ($k) { $k.PSChildName }"') do set "CODE=%%c"
if not defined CODE (
  echo The app is not installed on this computer.
  pause
  exit /b 0
)
echo Removing the app (this takes about a minute)...
msiexec /x %CODE% /qb /L*v "%TEMP%code212-remove.log"
if errorlevel 1 (
  echo The removal did not finish. The log is at %TEMP%code212-remove.log
  pause
  exit /b 1
)
echo Removing the guest account...
set "GUEST=Patron"
powershell -NoProfile -Command "(Get-Content 'C:\ProgramData\LibraryPC\config.json' -Raw | ConvertFrom-Json).patronAccount | Out-File -Encoding ascii '%TEMP%\code212-guest.txt'"
if exist "%TEMP%\code212-guest.txt" set /p GUEST=<"%TEMP%\code212-guest.txt"
del "%TEMP%\code212-guest.txt" >nul 2>&1
if /i "%GUEST%"=="admin" set "GUEST=Patron"
reg add "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionWinlogon" /v AutoAdminLogon /t REG_SZ /d 0 /f >nul 2>&1
net user "%GUEST%" /delete >nul 2>&1
if errorlevel 1 (
  echo The guest account "%GUEST%" was not removed. It may be signed in; remove it in Settings - Accounts.
) else (
  echo Guest account "%GUEST%" removed.
)
echo.
echo Done. The app is removed from this computer.
pause
exit /b 0
