@echo off

rem ********************************************************************************
rem *
rem * CLONRAID.BAT
rem *
rem * This file controls both save or restore of RAID configurations to a file.
rem *
rem * This batch file is called by the user.
rem *
rem ********************************************************************************


rem ********************************************************************************
rem * initially, our state is that no RAID configurations have been done.
rem ********************************************************************************
SET CFG_DONE=N

rem ********************************************************************************
rem * Determine if there are any IBM ServeRAID adapters found - max=16
rem ********************************************************************************
set NM_SR=0

call hwdet.bat

rem ********************************************************************************
rem * Attempt to load DOS device driver for ServeRAID since one or more is found
rem ********************************************************************************
if not %NM_SR%==0 call loadraid.bat


rem ********************************************************************************
rem * Determine how to run the RAID utility, save or restore
rem ********************************************************************************
if "%1"== "" goto SAVERAID
if %1 == 1 goto SAVERAID
if %1 == 2 goto RSTRRAID



:SAVERAID
rem ********************************************************************************
rem * Capture RAID configuration and save to a file
rem ********************************************************************************
echo Running appropriate RAID utility to save RAID configuration ...

rem ********************************************************************************
rem * Setup the RAMDSK to handle multiple capture files
rem ********************************************************************************
%RAMDSK%
cd \
md SGTMP
cd SGTMP

rem ********************************************************************************
rem * Now jump based on the first type of RAID controller found
rem ********************************************************************************
if not %NM_SR%==0 goto CAP_SR
if not %NM_IS%==0 goto CAP_LSI
if not %NM_ID%==0 goto CAP_HYP
goto NORAID


:CAP_SR
rem ********************************************************************************
rem * Block to save configuration of ServeRAID 4H-7k controller using IPSSEND
rem ********************************************************************************
set CLDIR=SR
md %CLDIR%

set MSG=Capturing ServeRAID configuration
echo %MSG%
echo.
if %DPLY_TYPE%==4   %TKDRV%\logevent.exe -c:0 -l:1 -ss:"CLONRAID:  %MSG%"

ipssend backup %DV_NUM% %RAMDSK%\SGTMP\%CLDIR%\%CLON_FILE% NOPROMPT >nul

if not errorlevel 1 goto CAP_LSI

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

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


:CAP_LSI
rem ********************************************************************************
rem * Block to save configuration of Integrated SCSI controller with RAID 
rem * capabilities using CFG1030.
rem ********************************************************************************
if %NM_IS%==0 goto CAP_HYP

set CLDIR=LSI
md %CLDIR%

set MSG=Capturing Integrated SCSI with RAID capabilities
echo %MSG%
echo.
if %DPLY_TYPE%==4   %TKDRV%\logevent.exe -c:0 -l:1 -ss:"CLONRAID:  %MSG%"

cfg1030 backup %DV_NUM% %RAMDSK%\SGTMP\%CLDIR%\%CLON_FILE%  >nul

if not errorlevel 1 goto CAP_HYP

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

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


:CAP_HYP
rem ********************************************************************************
rem * Block to save configuration of IDE RAID controller using HYPERCFG
rem ********************************************************************************
if %NM_ID%==0 goto CFCOPY

set CLDIR=IDE
md %CLDIR%

set MSG=Capturing IDE RAID
echo %MSG%
echo.
if %DPLY_TYPE%==4   %TKDRV%\logevent.exe -c:0 -l:1 -ss:"CLONRAID:  %MSG%"

hypercfg /l /f%RAMDSK%\SGTMP\%CLDIR%\%CLON_FILE%  >nul

if not errorlevel 1 goto CFCOPY

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

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



:CFCOPY
rem ********************************************************************************
rem * Move RAID configuation file back to source server if Altiris
rem ********************************************************************************
if not %DPLY_TYPE%==4 goto CLONBOOT

%TKDRV%
cd \
md %CLON_PATH%

xcopy %RAMDSK%\SGTMP\*.* %CLON_PATH%  /s > nul

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

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

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



:RSTRRAID
rem ********************************************************************************
rem * Deploy RAID configuration from a file
rem ********************************************************************************

if exist %CLON_PATH%\SR\%CLON_FILE%  goto SETUP
if exist %CLON_PATH%\LSI\%CLON_FILE% goto SETUP
if exist %CLON_PATH%\IDE\%CLON_FILE% goto SETUP
goto ERR-NCF

:SETUP
rem ********************************************************************************
rem * Setup the RAMDSK to handle multiple capture files
rem ********************************************************************************
%RAMDSK%
cd \
md SGTMP

xcopy %CLON_PATH%\*.* %RAMDSK%\SGTMP /s > nul

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

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

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


:DPLYCLON
rem ********************************************************************************
rem * Setup complete, determine how to run restore.
rem ********************************************************************************
%RAMDSK%
cd \SGTMP

rem ********************************************************************************
rem * Now jump based on the first type of RAID controller found
rem ********************************************************************************
if not %NM_SR%==0 goto SR_CFG
if not %NM_IS%==0 goto LSI_CFG
if not %NM_ID%==0 goto HYP_CFG
goto NORAID


:SR_CFG
rem ********************************************************************************
rem * Block to configure ServeRAID 4H-7k controller using IPSSEND
rem ********************************************************************************
set MSG=Deploying ServeRAID configuration
echo %MSG%
echo.
if %DPLY_TYPE%==4   %TKDRV%\logevent.exe -c:0 -l:1 -ss:"CLONRAID:  %MSG%"

ipssend restore %DV_NUM% SR\%CLON_FILE% noprompt

if not errorlevel 1 goto LSI_CFG

rem * Handle error condition
set MSG=Error deploying the RAID configuration to this system.
echo %MSG%
echo.
if not %DPLY_TYPE%==4 goto ERROR

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


:LSI_CFG
rem ********************************************************************************
rem * Block to configure Integrated SCSI controller with RAID caps using CFG1030.
rem ********************************************************************************
if %NM_IS%==0 goto HYP_CFG

set MSG=Deploying Integrated SCSI with RAID capabilities
echo %MSG%
echo.
if %DPLY_TYPE%==4   %TKDRV%\logevent.exe -c:0 -l:1 -ss:"CLONRAID:  %MSG%"

cfg1030 restore %DV_NUM% LSI\%CLON_FILE%

if not errorlevel 1 goto HYP_CFG

rem * Handle error condition
set MSG=Error deploying the RAID configuration to this system.
echo %MSG%
echo.
if not %DPLY_TYPE%==4 goto ERROR

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


:HYP_CFG
rem ********************************************************************************
rem * Block to configure IDE RAID controller using HYPERCFG
rem ********************************************************************************
if %NM_ID%==0 goto CFGDONE

set MSG=Deploying IDE RAID 
echo %MSG%
echo.
if %DPLY_TYPE%==4   %TKDRV%\logevent.exe -c:0 -l:1 -ss:"CLONRAID:  %MSG%"

hypercfg /s /@IDE\%CLON_FILE%

if not errorlevel 1 goto CFGDONE

rem * Handle error condition
set MSG=Error deploying the RAID configuration to this system.
echo %MSG%
echo.
if not %DPLY_TYPE%==4 goto ERROR

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



:CFGDONE
rem ********************************************************************************
rem * Configured at least one type of RAID controller
rem ********************************************************************************
set CFG_DONE=Y



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

if %CFG_DONE%==Y goto REBOOT 
goto END

:REBOOT
sleep 15
reboot.com

:NORAID
rem ********************************************************************************
rem * No supported RAID devices found
rem ********************************************************************************
set MSG=Can not find a supported RAID controller to clone. 
echo %MSG%
echo.

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


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

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


:ERROR
rem ********************************************************************************
rem * Hit an error in this batch file.
rem ********************************************************************************
echo Encountered an error and stopped processing in "clonraid.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:"CLONRAID: 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:"CLONRAID:  Encountered an error."

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

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


:END
set MSG=
set CLDIR=
set CFG_DONE=
set NM_SR=
set NM_TT=
set NM_EA=
set NM_ES=
set NM_IS=
set NM_ID=
