@ECHO OFF
REM DEPLOY.BAT - One-time deployment script for DOS Update System
REM
REM Purpose: Installs the new NWTOC update system on DOS 6.22 machines
REM Location: Run from T:\COMMON\ProdSW\DEPLOY.BAT
REM
REM What this does:
REM   1. Backs up current AUTOEXEC.BAT
REM   2. Prompts for machine name (TS-4R, TS-7A, etc.)
REM   3. Updates AUTOEXEC.BAT with MACHINE variable
REM   4. Copies update batch files to C:\BAT\
REM   5. Runs initial NWTOC to download all updates
REM
REM Version: 1.0 - DOS 6.22 compatible
REM Last modified: 2026-01-19

CLS
ECHO ==============================================================
ECHO DOS Update System - One-Time Deployment
ECHO ==============================================================
ECHO.
ECHO This script will install the new update system on this machine.
ECHO.
ECHO What will be installed:
ECHO   - NWTOC.BAT   (Download updates from network)
ECHO   - CTONW.BAT   (Upload changes to network)
ECHO   - UPDATE.BAT  (Full system backup)
ECHO   - STAGE.BAT   (System file staging)
ECHO   - REBOOT.BAT  (Apply updates on reboot)
ECHO   - CHECKUPD.BAT (Check for updates)
ECHO.
PAUSE Press any key to continue...
ECHO.

REM ==================================================================
REM STEP 1: Verify T: drive is accessible
REM ==================================================================

ECHO Step 1 of 5: Checking network drive...
ECHO.

T: 2>NUL
IF ERRORLEVEL 1 GOTO NO_T_DRIVE

C:
IF NOT EXIST T:\NUL GOTO NO_T_DRIVE

ECHO (OK) T: drive is accessible
ECHO     T: = \\D2TESTNAS\test
ECHO.
GOTO CHECK_DEPLOY_FILES

:NO_T_DRIVE
C:
ECHO.
ECHO ERROR: T: drive not available
ECHO.
ECHO The network drive T: must be mapped to \\D2TESTNAS\test
ECHO.
ECHO Run network startup first:
ECHO   C:\NET\STARTNET.BAT
ECHO.
ECHO Or map manually:
ECHO   NET USE T: \\D2TESTNAS\test /YES
ECHO.
ECHO Then run DEPLOY.BAT again.
ECHO.
PAUSE Press any key to exit...
GOTO END

REM ==================================================================
REM STEP 2: Verify deployment files exist on network
REM ==================================================================

:CHECK_DEPLOY_FILES
ECHO Step 2 of 5: Verifying deployment files...
ECHO.

IF NOT EXIST T:\COMMON\ProdSW\NWTOC.BAT GOTO MISSING_FILES
IF NOT EXIST T:\COMMON\ProdSW\CTONW.BAT GOTO MISSING_FILES
IF NOT EXIST T:\COMMON\ProdSW\UPDATE.BAT GOTO MISSING_FILES
IF NOT EXIST T:\COMMON\ProdSW\STAGE.BAT GOTO MISSING_FILES
IF NOT EXIST T:\COMMON\ProdSW\CHECKUPD.BAT GOTO MISSING_FILES

ECHO (OK) All deployment files found on network
ECHO     Location: T:\COMMON\ProdSW\
ECHO.
GOTO GET_MACHINE_NAME

:MISSING_FILES
ECHO ERROR: Deployment files not found on network
ECHO.
ECHO Expected location: T:\COMMON\ProdSW\
ECHO.
ECHO Files needed:
ECHO   - NWTOC.BAT
ECHO   - CTONW.BAT
ECHO   - UPDATE.BAT
ECHO   - STAGE.BAT
ECHO   - CHECKUPD.BAT
ECHO.
ECHO Contact system administrator.
ECHO.
PAUSE Press any key to exit...
GOTO END

REM ==================================================================
REM STEP 3: Get machine name from user
REM ==================================================================

:GET_MACHINE_NAME
ECHO Step 3 of 5: Configure machine name...
ECHO.
ECHO Enter this machine's name (e.g., TS-4R, TS-7A, TS-12B):
ECHO.
ECHO Machine name must match the folder on T: drive.
ECHO Example: If this is TS-4R, there should be T:\TS-4R\
ECHO.
SET /P MACHINE=Machine name:

REM Validate machine name was entered
IF "%MACHINE%"=="" GOTO MACHINE_NAME_EMPTY

ECHO.
ECHO (OK) Machine name: %MACHINE%
ECHO.

REM Verify machine folder exists on network
ECHO Checking for T:\%MACHINE%\ folder...
IF NOT EXIST T:\%MACHINE%\NUL MD T:\%MACHINE%
IF NOT EXIST T:\%MACHINE%\NUL GOTO MACHINE_FOLDER_ERROR

ECHO (OK) Machine folder ready: T:\%MACHINE%\
ECHO.
GOTO BACKUP_AUTOEXEC

:MACHINE_NAME_EMPTY
ECHO.
ECHO ERROR: Machine name cannot be empty
ECHO.
GOTO GET_MACHINE_NAME

:MACHINE_FOLDER_ERROR
ECHO.
ECHO ERROR: Could not create machine folder on network
ECHO.
ECHO Check:
ECHO - T: drive is writable
ECHO - Network connection is stable
ECHO - Permissions to create directories
ECHO.
PAUSE Press any key to exit...
GOTO END

REM ==================================================================
REM STEP 4: Backup current AUTOEXEC.BAT and install batch files
REM ==================================================================

:BACKUP_AUTOEXEC
ECHO Step 4 of 5: Installing update system files...
ECHO.

REM Backup current AUTOEXEC.BAT
IF NOT EXIST C:\AUTOEXEC.BAT GOTO NO_AUTOEXEC_BACKUP

ECHO Backing up AUTOEXEC.BAT...
COPY C:\AUTOEXEC.BAT C:\AUTOEXEC.SAV >NUL
IF ERRORLEVEL 1 GOTO BACKUP_ERROR
ECHO (OK) Backup created: C:\AUTOEXEC.SAV
GOTO AUTOEXEC_BACKUP_DONE

:NO_AUTOEXEC_BACKUP
ECHO WARNING: No existing AUTOEXEC.BAT found

:AUTOEXEC_BACKUP_DONE
ECHO.

REM Create C:\BAT directory if it doesn't exist
IF NOT EXIST C:\BAT\NUL MD C:\BAT
IF NOT EXIST C:\BAT\NUL GOTO BAT_DIR_ERROR

ECHO Copying update system files to C:\BAT\...

REM Copy batch files from network to local machine
XCOPY T:\COMMON\ProdSW\NWTOC.BAT C:\BAT\ /Y
IF ERRORLEVEL 4 GOTO COPY_ERROR
ECHO   (OK) NWTOC.BAT

XCOPY T:\COMMON\ProdSW\CTONW.BAT C:\BAT\ /Y
IF ERRORLEVEL 4 GOTO COPY_ERROR
ECHO   (OK) CTONW.BAT

XCOPY T:\COMMON\ProdSW\UPDATE.BAT C:\BAT\ /Y
IF ERRORLEVEL 4 GOTO COPY_ERROR
ECHO   (OK) UPDATE.BAT

XCOPY T:\COMMON\ProdSW\STAGE.BAT C:\BAT\ /Y
IF ERRORLEVEL 4 GOTO COPY_ERROR
ECHO   (OK) STAGE.BAT

XCOPY T:\COMMON\ProdSW\CHECKUPD.BAT C:\BAT\ /Y
IF ERRORLEVEL 4 GOTO COPY_ERROR
ECHO   (OK) CHECKUPD.BAT

ECHO.
ECHO (OK) All update system files installed
ECHO.
GOTO UPDATE_AUTOEXEC

:BACKUP_ERROR
ECHO.
ECHO ERROR: Could not backup AUTOEXEC.BAT
ECHO.
ECHO Continue anyway? (Y/N)
CHOICE /C:YN /N
IF ERRORLEVEL 2 GOTO END
ECHO.
GOTO UPDATE_AUTOEXEC

:BAT_DIR_ERROR
ECHO.
ECHO ERROR: Could not create C:\BAT directory
ECHO.
PAUSE Press any key to exit...
GOTO END

:COPY_ERROR
ECHO.
ECHO ERROR: Failed to copy files from network
ECHO.
ECHO Check:
ECHO - T: drive is accessible
ECHO - C: drive has free space
ECHO - No file locks on C:\BAT\
ECHO.
PAUSE Press any key to exit...
GOTO END

REM ==================================================================
REM STEP 5: Update AUTOEXEC.BAT with MACHINE variable
REM ==================================================================

:UPDATE_AUTOEXEC
ECHO Step 5 of 5: Updating AUTOEXEC.BAT...
ECHO.

REM Check if MACHINE variable already exists in AUTOEXEC.BAT
IF NOT EXIST C:\AUTOEXEC.BAT GOTO ADD_MACHINE_VAR

FIND "SET MACHINE=" C:\AUTOEXEC.BAT >NUL
IF NOT ERRORLEVEL 1 GOTO MACHINE_EXISTS

:ADD_MACHINE_VAR

REM Append MACHINE variable to AUTOEXEC.BAT
ECHO SET MACHINE=%MACHINE% >> C:\AUTOEXEC.BAT
IF ERRORLEVEL 1 GOTO AUTOEXEC_ERROR

ECHO (OK) Added to AUTOEXEC.BAT: SET MACHINE=%MACHINE%
ECHO.
GOTO DEPLOYMENT_COMPLETE

:MACHINE_EXISTS
ECHO WARNING: MACHINE variable already exists in AUTOEXEC.BAT
ECHO.
ECHO Current AUTOEXEC.BAT contains:
TYPE C:\AUTOEXEC.BAT | FIND "SET MACHINE="
ECHO.
ECHO Update MACHINE variable to %MACHINE%? (Y/N)
CHOICE /C:YN /N
IF ERRORLEVEL 2 GOTO DEPLOYMENT_COMPLETE

ECHO.
ECHO Manual edit required:
ECHO   1. Edit C:\AUTOEXEC.BAT
ECHO   2. Find line: SET MACHINE=...
ECHO   3. Change to: SET MACHINE=%MACHINE%
ECHO   4. Save and reboot
ECHO.
PAUSE Press any key to continue...
GOTO DEPLOYMENT_COMPLETE

:AUTOEXEC_ERROR
ECHO.
ECHO ERROR: Could not update AUTOEXEC.BAT
ECHO.
ECHO You must manually add this line to C:\AUTOEXEC.BAT:
ECHO   SET MACHINE=%MACHINE%
ECHO.
PAUSE Press any key to continue...
GOTO DEPLOYMENT_COMPLETE

REM ==================================================================
REM DEPLOYMENT COMPLETE
REM ==================================================================

:DEPLOYMENT_COMPLETE
CLS
ECHO ==============================================================
ECHO Deployment Complete!
ECHO ==============================================================
ECHO.
ECHO The DOS Update System has been installed on this machine.
ECHO.
ECHO Machine name: %MACHINE%
ECHO Backup location: T:\%MACHINE%\BACKUP\
ECHO Update location: T:\COMMON\ProdSW\
ECHO.
ECHO ==============================================================
ECHO Available Commands:
ECHO ==============================================================
ECHO.
ECHO NWTOC      - Download updates from network
ECHO CTONW      - Upload local changes to network
ECHO UPDATE     - Backup entire C: drive to network
ECHO CHECKUPD   - Check for available updates
ECHO.
ECHO ==============================================================
ECHO Next Steps:
ECHO ==============================================================
ECHO.
ECHO 1. REBOOT this machine to activate MACHINE variable
ECHO    Press Ctrl+Alt+Del to reboot
ECHO.
ECHO 2. After reboot, run NWTOC to download all updates:
ECHO    C:\BAT\NWTOC
ECHO.
ECHO 3. Create initial backup:
ECHO    C:\BAT\UPDATE
ECHO.
ECHO ==============================================================
ECHO.
ECHO Deployment log saved to: T:\%MACHINE%\DEPLOY.LOG
ECHO.

REM Create deployment log
ECHO Deployment completed: %DATE% %TIME% > T:\%MACHINE%\DEPLOY.LOG
ECHO Machine: %MACHINE% >> T:\%MACHINE%\DEPLOY.LOG
ECHO Files installed to: C:\BAT\ >> T:\%MACHINE%\DEPLOY.LOG
ECHO AUTOEXEC.BAT backup: C:\AUTOEXEC.SAV >> T:\%MACHINE%\DEPLOY.LOG

PAUSE Press any key to exit...
GOTO END

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

:END
REM Clean up environment variables
SET MACHINE=
