@echo off
setlocal EnableExtensions
title Repair EXE file association

echo ============================================================
echo Repair EXE file association for the current Windows user
echo ============================================================
echo.
echo This tool repairs cases where an EXE opens in an archive tool.
echo It does not install or launch ClassroomCallScreen.
echo.

if defined EXE_ASSOC_DRY_RUN goto :dryrun

set "BACKUP_DIR=%~dp0EXE-Association-Backup"
if not exist "%BACKUP_DIR%" mkdir "%BACKUP_DIR%" >nul 2>&1
if not exist "%BACKUP_DIR%" (
  echo [FAILED] Cannot create the backup folder:
  echo %BACKUP_DIR%
  goto :failed
)

echo [1/4] Backing up current user settings...
reg export "HKCU\Software\Classes\.exe" "%BACKUP_DIR%\HKCU-Classes-dot-exe.reg" /y >nul 2>&1
reg export "HKCU\Software\Classes\exefile" "%BACKUP_DIR%\HKCU-Classes-exefile.reg" /y >nul 2>&1
reg export "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.exe" "%BACKUP_DIR%\HKCU-Explorer-FileExts-dot-exe.reg" /y >nul 2>&1

echo [2/4] Removing the incorrect per-user Open With choice...
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.exe\UserChoice" /f >nul 2>&1

echo [3/4] Restoring the standard executable association...
reg add "HKCU\Software\Classes\.exe" /ve /t REG_SZ /d "exefile" /f >nul || goto :failed
reg add "HKCU\Software\Classes\.exe" /v "Content Type" /t REG_SZ /d "application/x-msdownload" /f >nul || goto :failed
reg add "HKCU\Software\Classes\exefile\shell\open\command" /ve /t REG_SZ /d "\"%%1\" %%*" /f >nul || goto :failed
reg add "HKCU\Software\Classes\exefile\shell\runas\command" /ve /t REG_SZ /d "\"%%1\" %%*" /f >nul || goto :failed
reg add "HKCU\Software\Classes\exefile\shell\runas\command" /v "IsolatedCommand" /t REG_SZ /d "\"%%1\" %%*" /f >nul || goto :failed

echo [4/4] Verifying the repaired association...
reg query "HKCU\Software\Classes\.exe" /ve | find /i "exefile" >nul || goto :failed
reg query "HKCU\Software\Classes\exefile\shell\open\command" /ve | find "%%1" >nul || goto :failed

echo.
echo [SUCCESS] The EXE file association has been repaired.
echo Close this window, then double-click the installer again.
echo If it still opens in an archive tool, sign out of Windows or restart.
echo Backup files are in:
echo %BACKUP_DIR%
echo.
pause
exit /b 0

:failed
echo.
echo [FAILED] The repair was not completed.
echo Ask the school IT administrator to run this file for the same user.
echo.
pause
exit /b 1

:dryrun
echo DRY_RUN_OK
echo HKCU\Software\Classes\.exe = exefile
echo HKCU\Software\Classes\exefile\shell\open\command = "%%1" %%*
exit /b 0
