@ECHO OFF
REM REBOOT.BAT - Manual system file update script
REM
REM NOTE: This file is normally AUTO-GENERATED by STAGE.BAT
REM       This standalone version is for manual testing/recovery only
REM
REM Usage: REBOOT
REM
REM Applies staged system file updates:
REM   C:\AUTOEXEC.NEW ??? C:\AUTOEXEC.BAT
REM   C:\CONFIG.NEW ??? C:\CONFIG.SYS
REM
REM Version: 1.0 - DOS 6.22 compatible
REM Last modified: 2026-01-19

ECHO.
ECHO ==============================================================
ECHO Manual System File Update
ECHO ==============================================================
ECHO.

REM ==================================================================
REM Check if staged files exist
REM ==================================================================

SET HASAUTO=0
SET HASCONF=0

IF EXIST C:\AUTOEXEC.NEW SET HASAUTO=1
IF EXIST C:\CONFIG.NEW SET HASCONF=1

IF "%HASAUTO%"=="0" IF "%HASCONF%"=="0" GOTO NO_UPDATES

REM ==================================================================
REM Warn user
REM ==================================================================

ECHO WARNING: This will replace your current system files:
ECHO.
IF "%HASAUTO%"=="1" ECHO   C:\AUTOEXEC.BAT will be replaced by C:\AUTOEXEC.NEW
IF "%HASCONF%"=="1" ECHO   C:\CONFIG.SYS will be replaced by C:\CONFIG.NEW
ECHO.
ECHO Backups will be saved as .SAV files.
ECHO.
ECHO Press Ctrl+C to cancel, or
PAUSE
ECHO.

REM ==================================================================
REM Backup current files
REM ==================================================================

ECHO Creating backups...

IF EXIST C:\AUTOEXEC.BAT COPY C:\AUTOEXEC.BAT C:\AUTOEXEC.SAV >NUL
IF EXIST C:\AUTOEXEC.BAT IF NOT ERRORLEVEL 1 ECHO   (OK) C:\AUTOEXEC.BAT ??? C:\AUTOEXEC.SAV

IF EXIST C:\CONFIG.SYS COPY C:\CONFIG.SYS C:\CONFIG.SAV >NUL
IF EXIST C:\CONFIG.SYS IF NOT ERRORLEVEL 1 ECHO   (OK) C:\CONFIG.SYS ??? C:\CONFIG.SAV

ECHO.

REM ==================================================================
REM Apply updates
REM ==================================================================

ECHO Applying updates...

REM Apply AUTOEXEC.NEW
IF "%HASAUTO%"=="1" COPY C:\AUTOEXEC.NEW C:\AUTOEXEC.BAT >NUL
IF "%HASAUTO%"=="1" IF NOT ERRORLEVEL 1 ECHO   (OK) AUTOEXEC.BAT updated
IF "%HASAUTO%"=="1" IF ERRORLEVEL 1 ECHO   ERROR: AUTOEXEC.BAT update failed
IF "%HASAUTO%"=="1" IF ERRORLEVEL 1 GOTO UPDATE_ERROR

REM Apply CONFIG.NEW
IF "%HASCONF%"=="1" COPY C:\CONFIG.NEW C:\CONFIG.SYS >NUL
IF "%HASCONF%"=="1" IF NOT ERRORLEVEL 1 ECHO   (OK) CONFIG.SYS updated
IF "%HASCONF%"=="1" IF ERRORLEVEL 1 ECHO   ERROR: CONFIG.SYS update failed
IF "%HASCONF%"=="1" IF ERRORLEVEL 1 GOTO UPDATE_ERROR

ECHO.

REM ==================================================================
REM Clean up staging files
REM ==================================================================

ECHO Cleaning up staging files...

IF EXIST C:\AUTOEXEC.NEW DEL C:\AUTOEXEC.NEW
IF EXIST C:\CONFIG.NEW DEL C:\CONFIG.NEW

ECHO   (OK) Staging files deleted
ECHO.

REM ==================================================================
REM Success
REM ==================================================================

ECHO ==============================================================
ECHO System Files Updated Successfully
ECHO ==============================================================
ECHO.
ECHO Updated files:
IF "%HASAUTO%"=="1" ECHO   - C:\AUTOEXEC.BAT
IF "%HASCONF%"=="1" ECHO   - C:\CONFIG.SYS
ECHO.
ECHO Backup files saved:
ECHO   - C:\AUTOEXEC.SAV (previous AUTOEXEC.BAT)
ECHO   - C:\CONFIG.SAV (previous CONFIG.SYS)
ECHO.
ECHO To activate changes:
ECHO   Reboot the computer (Ctrl+Alt+Del)
ECHO.
ECHO To rollback changes:
ECHO   COPY C:\AUTOEXEC.SAV C:\AUTOEXEC.BAT
ECHO   COPY C:\CONFIG.SAV C:\CONFIG.SYS
ECHO   Then reboot
ECHO.
ECHO ==============================================================
ECHO.
PAUSE
GOTO END

REM ==================================================================
REM ERROR HANDLERS
REM ==================================================================

:NO_UPDATES
ECHO WARNING: No staged update files found
ECHO.
ECHO Expected files:
ECHO   C:\AUTOEXEC.NEW (not found)
ECHO   C:\CONFIG.NEW (not found)
ECHO.
ECHO Run NWTOC to download updates from network, then:
ECHO   CALL C:\BAT\STAGE.BAT
ECHO.
PAUSE
GOTO END

:UPDATE_ERROR
ECHO.
ECHO ERROR: Update failed
ECHO.
ECHO Your system may be in an inconsistent state.
ECHO.
ECHO Recovery steps:
ECHO   1. COPY C:\AUTOEXEC.SAV C:\AUTOEXEC.BAT
ECHO   2. COPY C:\CONFIG.SAV C:\CONFIG.SYS
ECHO   3. Reboot (Ctrl+Alt+Del)
ECHO.
ECHO If system won't boot:
ECHO   1. Boot from DOS floppy
ECHO   2. Copy .SAV files back to .BAT and .SYS
ECHO   3. Remove floppy and reboot
ECHO.
PAUSE
GOTO END

REM ==================================================================
REM CLEANUP AND EXIT
REM ==================================================================

:END
SET HASAUTO=
SET HASCONF=
