@echo off
rem FSWD lab exam setup (instructor, 21 Nov 2026): the tools only, no class or lab folders, VS Code AI off.
rem   curl -fsSL https://rishi-fswd-java.pages.dev/exam.txt -o "%TEMP%\exam.cmd" && "%TEMP%\exam.cmd"
rem The site serves the files as .txt. Step 1 (setup-steps.txt) installs the tools in a second window with administrator
rem rights; step 2 (user-steps.txt) runs here, as the student: extensions, Git name, Open buttons, packages, the final check.
rem Option:  -CheckOnly   only shows what is ready
setlocal
set "SITE=https://rishi-fswd-java.pages.dev/fswd/files/setup"
set "ROOT=C:\fswd"
rem everything of the setup lives in C:\fswd\setup: the scripts, setup\logs and setup\downloads (9 Nov 2026)
set "SET=C:\fswd\setup"
title FSWD lab exam setup
if not exist "%SET%\logs" mkdir "%SET%\logs" 2>nul
rem an earlier run kept its files loose in C:\fswd: tidy them into C:\fswd\setup once
if exist "%ROOT%\logs\*" move /y "%ROOT%\logs\*" "%SET%\logs\" >nul 2>&1
if exist "%ROOT%\logs" rmdir "%ROOT%\logs" 2>nul
if exist "%ROOT%\downloads" rmdir /s /q "%ROOT%\downloads" 2>nul
for %%F in (fswd-setup.ps1 fswd-own-part.ps1 fswd-setup.cmd) do if exist "%ROOT%\%%F" del /q "%ROOT%\%%F" 2>nul
echo Getting the latest setup steps...
"%SystemRoot%\System32\curl.exe" -fsSL "%SITE%/setup-steps.txt" -o "%SET%\fswd-setup.ps1" 2>nul
if errorlevel 1 goto noweb
"%SystemRoot%\System32\curl.exe" -fsSL "%SITE%/user-steps.txt" -o "%SET%\fswd-own-part.ps1" 2>nul
if errorlevel 1 goto noweb
copy /y "%~f0" "%SET%\fswd-exam.cmd" >nul 2>&1

if /i "%~1"=="-CheckOnly" (
  powershell -NoProfile -ExecutionPolicy Bypass -File "%SET%\fswd-setup.ps1" -CheckOnly -Exam
  powershell -NoProfile -ExecutionPolicy Bypass -File "%SET%\fswd-own-part.ps1" -CheckOnly -Exam
  goto done
)

net session >nul 2>&1
if errorlevel 1 goto elevate
powershell -NoProfile -ExecutionPolicy Bypass -File "%SET%\fswd-setup.ps1" -Exam
goto own

:elevate
echo.
echo   Windows will now ask: "Do you want to allow this app to make changes to your device?"
echo   Press  Yes.  The tools install in a second window; keep both windows open.
echo.
powershell -NoProfile -Command "try { $p = Start-Process powershell -ArgumentList '-NoProfile','-ExecutionPolicy','Bypass','-File','%SET%\fswd-setup.ps1','-Exam' -Verb RunAs -Wait -PassThru -ErrorAction Stop; exit $p.ExitCode } catch { exit 9 }"
if errorlevel 9 (
  echo   PROBLEM  you pressed No, or this account cannot make changes.
  echo     fix    paste the setup line again and press Yes. On a lab computer, ask the lab assistant to type the administrator password.
  echo.
)

:own
powershell -NoProfile -ExecutionPolicy Bypass -File "%SET%\fswd-own-part.ps1" -Exam
:done
echo.
pause
exit /b

:noweb
echo.
echo   PROBLEM  the setup steps could not be downloaded.
echo     fix    check the internet (open any website in Chrome), then paste the setup line again.
echo.
pause
exit /b 1
