@ECHO OFF
REM UPDATE.BAT - Backup Dataforth test machine to network storage
REM Usage: UPDATE [machine-name]
REM Example: UPDATE TS-4R
REM
REM If machine-name not provided, uses MACHINE environment variable
REM from AUTOEXEC.BAT
REM
REM Version: 2.3 - Fixed XCOPY trailing backslash for DOS 6.22
REM Last modified: 2026-01-20

REM ==================================================================
REM STEP 1: Determine machine name
REM ==================================================================

IF NOT "%1"=="" GOTO USE_PARAM
IF NOT "%MACHINE%"=="" GOTO USE_ENV

:NO_MACHINE
ECHO.
ECHO ERROR: Machine name not specified
ECHO.
ECHO Usage: UPDATE machine-name
ECHO   Example: UPDATE TS-4R
ECHO.
ECHO Or set MACHINE variable in AUTOEXEC.BAT:
ECHO   SET MACHINE=TS-4R
ECHO.
PAUSE
GOTO END

:USE_PARAM
SET MACHINE=%1
GOTO CHECK_DRIVE

:USE_ENV
REM Machine name from environment variable
GOTO CHECK_DRIVE

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

:CHECK_DRIVE
ECHO Checking network drive T:...

REM DOS 6.22: Direct file test is most reliable
IF NOT EXIST T:\*.* GOTO NO_T_DRIVE

ECHO (OK) T: drive accessible
GOTO START_BACKUP

:NO_T_DRIVE
ECHO.
ECHO ERROR: T: drive not available
ECHO.
ECHO Network drive T: must be mapped to \\D2TESTNAS\test
ECHO.
ECHO Run STARTNET.BAT to map network drives:
ECHO   C:\STARTNET.BAT
ECHO.
ECHO Or map manually:
ECHO   NET USE T: \\D2TESTNAS\test /YES
ECHO.
PAUSE
GOTO END

REM ==================================================================
REM STEP 3: Create backup directory structure
REM ==================================================================

:START_BACKUP
ECHO.
ECHO ==============================================================
ECHO Backup: Machine %MACHINE%
ECHO ==============================================================
ECHO Source: C:\
ECHO Target: T:\%MACHINE%\BACKUP
ECHO.

REM Create machine directory if it doesn't exist
IF NOT EXIST T:\%MACHINE%\NUL MD T:\%MACHINE%

REM Create backup directory
IF NOT EXIST T:\%MACHINE%\BACKUP\NUL MD T:\%MACHINE%\BACKUP

REM Check if backup directory was created successfully
IF NOT EXIST T:\%MACHINE%\BACKUP\*.* GOTO BACKUP_DIR_ERROR

ECHO (OK) Backup directory ready
ECHO.

REM ==================================================================
REM STEP 4: Perform backup
REM ==================================================================

ECHO Starting backup...
ECHO This may take several minutes depending on file count.
ECHO.

REM XCOPY options for DOS 6.22:
REM /S = Copy subdirectories (except empty ones)
REM /E = Copy subdirectories (including empty ones)
REM /Y = Suppress prompts (auto-overwrite)
REM /H = Copy hidden and system files
REM /K = Copy attributes
REM /C = Continue on errors
REM
REM NOTE: /D flag removed - requires date parameter in DOS 6.22 (/D:mm-dd-yy)
REM NOTE: /Q flag not available in DOS 6.22 (added in later Windows versions)

XCOPY C:\*.* T:\%MACHINE%\BACKUP /S /E /Y /H /K /C

REM Check XCOPY error level
REM 0 = Files copied OK
REM 1 = No files found to copy
REM 2 = User terminated (Ctrl+C)
REM 4 = Initialization error (insufficient memory, invalid path, etc)
REM 5 = Disk write error

IF ERRORLEVEL 5 GOTO DISK_ERROR
IF ERRORLEVEL 4 GOTO INIT_ERROR
IF ERRORLEVEL 2 GOTO USER_ABORT
IF ERRORLEVEL 1 GOTO NO_FILES

ECHO.
ECHO (OK) Backup completed successfully
ECHO.
ECHO Files backed up to: T:\%MACHINE%\BACKUP
GOTO END

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

:BACKUP_DIR_ERROR
ECHO.
ECHO ERROR: Could not create backup directory
ECHO Target: T:\%MACHINE%\BACKUP
ECHO.
ECHO Check:
ECHO - T: drive is writable
ECHO - Sufficient disk space on T:
ECHO - Network connection is stable
ECHO.
PAUSE
GOTO END

:DISK_ERROR
ECHO.
ECHO ERROR: Disk write error
ECHO.
ECHO Possible causes:
ECHO - Target drive is full
ECHO - Network connection lost
ECHO - Permission denied
ECHO.
PAUSE
GOTO END

:INIT_ERROR
ECHO.
ECHO ERROR: Backup initialization failed
ECHO.
ECHO Possible causes:
ECHO - Insufficient memory
ECHO - Invalid path
ECHO - Target drive not accessible
ECHO.
PAUSE
GOTO END

:USER_ABORT
ECHO.
ECHO WARNING: Backup terminated by user (Ctrl+C)
ECHO.
ECHO Backup may be incomplete!
ECHO.
PAUSE
GOTO END

:NO_FILES
ECHO.
ECHO WARNING: No files found to copy
ECHO.
ECHO This may indicate:
ECHO - All files are already up to date (/D option)
ECHO - Source drive is empty
ECHO.
PAUSE
GOTO END

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

:END
REM Clean up environment variables (DOS has limited space)
SET OLDDRV=
