@echo off

rem ********************************************************************************
rem *
rem * CUMGT.BAT
rem *
rem * This file controls running CMOSUTIL.EXE to both save or restore the CMOS
rem * settings for a system from a binary file.
rem *
rem * This batch file is called by the user.
rem *
rem ********************************************************************************


rem * Change to Toolkit drive
%TKDRV%

set UPDD=N

rem ********************************************************************************
rem * Determine how to run the CMOS utility, save or restore
rem ********************************************************************************
if "%1"== "" goto SAVECMOS
if %1 == 1 goto SAVECMOS
if %1 == 2 goto RSTRCMOS



:SAVECMOS
rem ********************************************************************************
rem * Gather CMOS setting and save to a file
rem ********************************************************************************
echo Running system CMOS utility to save CMOS settings ...

if exist %UPD_PATH%\%UPD_BDIR%\cmosutil.exe goto CUTIL
if exist %UPD_PATH%\%UPD_BDIR%\cmosram.exe goto CRAM
goto ERR-CUF

:CUTIL
rem ********************************************************************************
rem * Nothing special here, use standard CMOS utility
rem ********************************************************************************

rem * CMOS utility will not create a file if one already exists
if exist %RAMDSK%\%CMOS_FILE% del %RAMDSK%\%CMOS_FILE% > nul

cd %UPD_PATH%\%UPD_BDIR%

CMOSUTIL.EXE /S %RAMDSK%\%CMOS_FILE%  > nul

if not errorlevel 1 goto CFCOPY

rem * Encountered an error, retry with different switch
CMOSUTIL.EXE /s %RAMDSK%\%CMOS_FILE%

if not errorlevel 1 goto CFCOPY

rem * Handle error condition
set MSG=Error saving the CMOS settings for this system.
echo %MSG%
echo.
if not %DPLY_TYPE%==4 goto ERROR

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"CUMGT:  %MSG%"
goto ERROR


:CRAM
rem ********************************************************************************
rem * Use CMOSRAM
rem ********************************************************************************

rem * Clean-up in case of re-run
if exist %RAMDSK%\%CMOS_FILE% del %RAMDSK%\%CMOS_FILE% > nul

cd %UPD_PATH%\%UPD_BDIR%

CMOSRAM.EXE /save:%RAMDSK%\%CMOS_FILE%

if not errorlevel 1 goto CFCOPY

rem * Handle error condition
set MSG=Error saving the CMOS settings for this system.
echo %MSG%
echo.
if not %DPLY_TYPE%==4 goto ERROR

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"CUMGT:  %MSG%"
goto ERROR


:CFCOPY
rem ********************************************************************************
rem * Move CMOS info file back to source server if Altiris
rem ********************************************************************************
if not %DPLY_TYPE%==4 goto CMOSBOOT

%TKDRV%
md %CMOS_PATH%

xcopy %RAMDSK%\%CMOS_FILE% %CMOS_PATH% > nul

rem * Check for errors: XCOPY.EXE
if not errorlevel 1 goto CMOSBOOT

rem * Handle error condition
set MSG=An error occurred while copying the CMOS settings file.
echo %MSG%
echo.

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"CUMGT:  %MSG%"
goto ERROR


:RSTRCMOS
rem ********************************************************************************
rem * Restore CMOS setting from a file
rem ********************************************************************************
if not exist %CMOS_PATH%\%CMOS_FILE% goto ERR-NCF

xcopy %CMOS_PATH%\%CMOS_FILE% %RAMDSK%\ > nul

rem * Check for errors: XCOPY.EXE
if not errorlevel 1 goto UPDCMOS

rem * Handle error condition
set MSG=An error occurred while copying the CMOS settings file.
echo %MSG%
echo.
if not %DPLY_TYPE%==4 goto ERROR

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"CUMGT:  %MSG%"
goto ERROR


:UPDCMOS
rem ********************************************************************************
rem * Setup complete, determine how to run restore.
rem ********************************************************************************
set UPDD=Y

echo Running system CMOS utility to restore CMOS settings ...

if exist %UPD_PATH%\%UPD_BDIR%\lccmos.bat   goto CMOSBAT
if exist %UPD_PATH%\%UPD_BDIR%\cmosutil.exe goto CUEXE
if exist %UPD_PATH%\%UPD_BDIR%\cmosram.exe goto CURAM

goto ERR_CUF


:CMOSBAT
rem ********************************************************************************
rem * Special processing here, use provided batch file
rem ********************************************************************************
cd %UPD_PATH%\%UPD_BDIR%

call lccmos.bat %RAMDSK%\%CMOS_FILE%

goto CMOSBOOT


:CUEXE
rem ********************************************************************************
rem * Nothing special here, use standard CMOS utility
rem ********************************************************************************
cd %UPD_PATH%\%UPD_BDIR%

CMOSUTIL.EXE /r %RAMDSK%\%CMOS_FILE%

if not errorlevel 1 goto CMOSBOOT

rem * Handle error condition
set MSG=Error restoring the CMOS settings for this system.
echo %MSG%
echo.
if not %DPLY_TYPE%==4 goto ERROR

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"CUMGT:  %MSG%"
goto ERROR


:CURAM
rem ********************************************************************************
rem * Use CMOSRAM utility
rem ********************************************************************************
cd %UPD_PATH%\%UPD_BDIR%

CMOSRAM.EXE /load:%RAMDSK%\%CMOS_FILE%

if not errorlevel 1 goto CMOSBOOT

rem * Handle error condition
set MSG=Error restoring the CMOS settings for this system.
echo %MSG%
echo.
if not %DPLY_TYPE%==4 goto ERROR

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"CUMGT:  %MSG%"
goto ERROR



:CMOSBOOT
rem ********************************************************************************
rem * CMOS activity done, so determine if we need to reboot or not before continuing.
rem ********************************************************************************
if %DPLY_TYPE%==4 goto ALTSTAT0

%TKDRV%
cd \

if %UPDD%==Y goto REBOOT
goto END

:REBOOT
sleep 15
reboot.com


:ERR-CUF
rem ********************************************************************************
rem * Can not find executable to run.
rem ********************************************************************************
set MSG=Can not find a CMOS utility to run.
echo %MSG%
echo.

if not %DPLY_TYPE%==4 goto ERROR
%TKDRV%\logevent.exe -c:2 -l:2 -ss:"CUMGT:  %MSG%"
goto ERROR


:ERR-NCF
rem ********************************************************************************
rem * Can not find the CMOS file.
rem ********************************************************************************
set MSG=Can not find the CMOS file to restore.
echo %MSG%
echo.

if not %DPLY_TYPE%==4 goto ERROR
%TKDRV%\logevent.exe -c:2 -l:2 -ss:"CUMGT:  %MSG%"
goto ERROR


:ERROR
rem ********************************************************************************
rem * Hit an error in this batch file.
rem ********************************************************************************
echo Encountered an error and stopped processing in "cumgt.bat".

if %DPLY_TYPE%==4 goto ALTSTAT1

savestat.exe /set2=251 > nul

goto END


:ALTSTAT0
rem ********************************************************************************
rem * Update Altiris with necessary status
rem ********************************************************************************
echo Updating Altiris status: Good
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CUMGT: Completed successfully."


rem Reset to root of Toolkit drive
%TKDRV%
cd \

goto END


:ALTSTAT1
rem ********************************************************************************
rem * Update Altiris with necessary status
rem ********************************************************************************
echo Updating Altiris status: Error
%TKDRV%\logevent.exe -c:251 -l:3 -ss:"CUMGT:  Encountered an error."

rem Reset to root of Toolkit drive
%TKDRV%
cd \

rem Reset persistent storage
savestat.exe /reset > nul
goto END


:END
set UPDD=
set MSG=
