@echo off
rem ********************************************************************************
rem *
rem * RAIDMGR.BAT
rem *
rem * This file controls the configuration of RAID adapters for the ServerGuide
rem * Scripting Toolkit.
rem *
rem * This batch file is called by cfgraid.bat or may be called directly from
rem * specific scenarios.
rem *
rem ********************************************************************************

rem ********************************************************************************
rem ********************************************************************************
rem * Configure adapters.
rem ********************************************************************************

rem ********************************************************************************
rem * if xxx.bat does not exist, then step down to the .txt-driven block.
rem ********************************************************************************

if not exist %RD_PATH%\%RD_STUB%%1.bat goto CFGTT

rem ********************************************************************************
rem * execute the batch file to load the env variables
rem ********************************************************************************

call %RD_PATH%\%RD_STUB%%1.bat

rem ********************************************************************************
rem * Running the .bat file has populated the RDTYPE env var. Based on its
rem * value, goto the appropriate block.
rem ********************************************************************************
if "%RDTYPE%"  == ""  goto CFGTT
FOR %%i in (ipssend IPSSEND)   do if %RDTYPE% == %%i goto CFGSR
FOR %%i in (raidsel RAIDSEL)   do if %RDTYPE% == %%i goto CFGES
FOR %%i in (cfg1030 CFG1030)   do if %RDTYPE% == %%i goto CFGIS
FOR %%i in (hypercfg HYPERCFG) do if %RDTYPE% == %%i goto CFGID


rem ********************************************************************************
rem * if RDTYPE is none of the types above then this is odd but not fatal,
rem * so goto CFGTT
rem ********************************************************************************
goto CFGTT

:CFGSR
rem ********************************************************************************
rem * Block to configure ServeRAID 4H-6M controller using IPSSEND
rem ********************************************************************************

rem ********************************************************************************
rem * If we fell through to here, then if no SR adapters to config, step to next
rem * .bat-driven block
rem ********************************************************************************
if %NM_SR%==0 goto CFGTT


echo Configuring ServeRAID

if %DPLY_TYPE%==4 goto ALT-SR1
goto U1

:ALT-SR1
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Configuring ServeRAID"

:U1
rem * First pass, create the array
ipssend create %DV_NUM% logicaldrive newarray %LD1% %RL1% %CSG%   noprompt >nul
if errorlevel 1 goto BAT-ERR

rem * if LD2 is set, create a logicaldrive on the array
if not "%LD2%" == "" ipssend create 1 logicaldrive %ARRAY% %LD2% %RL2% NOPROMPT >nul
if errorlevel 1 goto BAT-ERR

rem * if LD3 is set, create a logicaldrive on the array
if not "%LD3%" == "" ipssend create 1 logicaldrive %ARRAY% %LD3% %RL3% NOPROMPT >nul
if errorlevel 1 goto BAT-ERR

rem * if LD4 is set, create a logicaldrive on the array
if not "%LD4%" == "" ipssend create 1 logicaldrive %ARRAY% %LD4% %RL4% NOPROMPT >nul
if errorlevel 1 goto BAT-ERR


rem * if HS1_CSG is set, create the hot spare
if not "%HS1_CSG%" == "" ipssend setstate %DV_NUM% %HS1_CSG% HSP >nul
if errorlevel 1 goto BAT-ERR

rem * if HS2_CSG is set, create the hot spare
if not "%HS2_CSG%" == "" ipssend setstate %DV_NUM% %HS2_CSG% HSP >nul
if errorlevel 1 goto BAT-ERR


:SRDONE
rem * Configured ServeRAID
set CFG_DONE=Y

rem * clear env vars to free up space
call clrsr.bat

rem * Update user on progress.
echo Successfully configured using: %RD_PATH%\%RD_STUB%%1.bat

if %DPLY_TYPE%==4 goto ALT-SR2
goto CFGTT

:ALT-SR2
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Successfully configured using: %RD_PATH%\%RD_STUB%%1.bat"


rem ********************************************************************************
rem * It is possible to have both a stub1.bat and a stub1.txt, so after we
rem * execute the appropriate .bat we need to goto CFGTT to see if there is
rem * an appropriate .txt to execute.
rem ********************************************************************************
goto CFGTT


:CFGES
rem ********************************************************************************
rem * Block to configure ServeRAID E SCSI controller using RAIDSEL
rem ********************************************************************************

rem ********************************************************************************
rem * %1.bat has already been executed, we had to go there to get here,
rem * so no need to re-run it.
rem * If no ES adapters to config, step to next .bat-driven block
rem ********************************************************************************
if %NM_ES%==0 goto CFGTT

rem ********************************************************************************
rem * Configure ServeRAID E SCSI controller using RAIDSEL
rem ********************************************************************************
echo Configuring ServeRAID E SCSI

if %DPLY_TYPE%==4 goto ALT-ESC1
goto U2

:ALT-ESC1
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Configuring ServeRAID E SCSI"

:U2
if %MAS%==Y goto DO_MAS
if %DEF_HS%==Y goto DO_HS
if %RL%==0 goto DO_RL0
if %RL%==1 goto DO_RL1
goto SESCDONE

:DO_MAS
raidsel -l %RL% -g Master %PAD1% Slave %PAD2%  -q >nul
if errorlevel 1 goto BAT-ERR

goto SESCDONE

:DO_HS
raidsel -S %PAD1% + %HS_PAD1% -q >nul
if errorlevel 1 goto BAT-ERR

goto SESCDONE


:DO_RL0
raidsel -l %RL% -g %PAD1% %PAD2% -z %STRIPE%  -q >nul
if errorlevel 1 goto BAT-ERR

goto SESCDONE

:DO_RL1
echo Creating ...
raidsel -l %RL% -g %PAD1% %PAD2% -q > nul
if errorlevel 1 goto BAT-ERR

echo Building HostRAID SCSI array. THIS CAN TAKE UP TO 45 MINUTES.
raidsel -a build %PAD1%
if errorlevel 1 goto BAT-ERR

goto SESCDONE

:SESCDONE
rem * Configured ServeRAID E SCSI
set CFG_DONE=Y

rem clear environment variables to free up space
call clrsesc.bat

echo Successfully configured using: %RD_PATH%\%RD_STUB%%1.bat

if %DPLY_TYPE%==4 goto ALT-ESC2
goto CFGTT

:ALT-ESC2
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Successfully configured using: %RD_PATH%\%RD_STUB%%1.bat"

rem ********************************************************************************
rem * It is possible to have both a stub1.bat and a stub1.txt, so after we
rem * execute the appropriate .bat we need to goto CFGTT to see if there is
rem * an appropriate .txt to execute.
rem ********************************************************************************
goto CFGTT



:CFGIS
rem ********************************************************************************
rem * Block to configure Integrated SCSI controller with RAID caps using CFG1030.
rem ********************************************************************************

rem ********************************************************************************
rem * Configure Integrated SCSI controller with RAID capabilities using CFG1030.
rem * %1.bat has already been executed, we had to go there to get here,
rem * If no IS adapters to config, step to next .bat-driven block
rem ********************************************************************************
if %NM_IS%==0 goto CFGTT

echo Configuring Integrated SCSI with RAID capabilities

if %DPLY_TYPE%==4 goto ALT-L1
goto U3

:ALT-L1
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Configuring Integrated SCSI with RAID capabilities"

:U3
COPY %TK_PATH%\utils\cfg1030.exe %RAMDSK%\ >nul
%RAMDSK%
cd \

cfg1030 create %DV_NUM% logicaldrive newarray MAX 1 1 %SID1% 1 %SID2%  qsync >nul
if errorlevel 1 goto BAT-ERR

if %DEF_HS%==N goto ISRDONE
if %DEF_HS%==n goto ISRDONE

cfg1030 setstate %DV_NUM% 1 %HS_SID1% HSP >nul
if errorlevel 1 goto BAT-ERR


:ISRDONE
rem * Configured Integrated SCSI with RAID capabilities
set CFG_DONE=Y

rem * go back to the main Toolkit drive
%TKDRV%

rem clear environment variables to free up space
call clrisr.bat

echo Successfully configured using: %RD_PATH%\%RD_STUB%%1.bat

if %DPLY_TYPE%==4 goto ALT-L2
goto CFGTT

:ALT-L2
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Successfully configured using: %RD_PATH%\%RD_STUB%%1.bat"


rem ********************************************************************************
rem * It is possible to have both a stub1.bat and a stub1.txt, so after we
rem * execute the appropriate .bat we need to goto CFGTT to see if there is
rem * an appropriate .txt to execute.
rem ********************************************************************************
goto CFGTT



:CFGID
rem ********************************************************************************
rem * Block to configure IDE RAID controller using HYPERCFG
rem ********************************************************************************

rem ********************************************************************************
rem * %1.bat has already been executed, we had to go there to get here,
rem * so no need to re-run it.
rem * If no ID adapters to config, step to first .txt-driven block
rem ********************************************************************************
if %NM_ID%==0 goto CFGTT

echo Configuring IDE RAID

if %DPLY_TYPE%==4 goto ALT-I1
goto U4

:ALT-I1
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Configuring IDE RAID"


:U4
hypercfg /A%IMODE% /S
if errorlevel 1 goto BAT-ERR

rem * Configured IDE RAID
set CFG_DONE=Y

rem clear environment variables to free up space
call clride.bat

echo Successfully configured using: %RD_PATH%\%RD_STUB%%1.bat

if %DPLY_TYPE%==4 goto ALT-I2
goto CFGTT

:ALT-I2
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Successfully configured using: %RD_PATH%\%RD_STUB%%1.bat"

rem * Allow to fall through to CFGTT, first of the .txt-driven blocks.


rem ********************************************************************************
rem ********************************************************************************
rem * Configure adapters that use text files for configuration.
rem ********************************************************************************

:CFGTT
rem * We may have gotten here with no device found, so we need to clean up the
rem * environment to free space.
call clrsr.bat
call clrsesc.bat
call clrisr.bat
call clride.bat

rem ********************************************************************************
rem * Block to configure ServeRAID T controller using ACU
rem ********************************************************************************

rem * verify existence of configuration data file
if not exist %RD_PATH%\%RD_STUB%%1.txt goto END

rem ********************************************************************************
rem * Run CLINI against text file. Look for : in Drives = values.
rem * If : is found, we are to configure a 7T card. If , (comma) is
rem * found, we are to configure an e-type SATA card.
rem * If CLINI returns errorlevel=100, then this is not a proper file
rem * for configuring a 7T card. If it returns errorlevel=1, an error
rem * was encountered. Otherwise, we have a valid txt file for 7T configuration.
rem ********************************************************************************
:ck7t
CLIni.exe %RD_PATH%\%RD_STUB%%1.txt /I:Drives /C::
if errorlevel 100 goto cketype
if errorlevel 1 goto TXT-ERR
goto 7T

:cketype

CLIni.exe %RD_PATH%\%RD_STUB%%1.txt /I:Drives /C:,
if errorlevel 100 goto end
if errorlevel 1 goto TXT-ERR
goto CFGEA

:7T
rem ********************************************************************************
rem * if no TT adapters to config, we know from ":" that we do not care about e-SATA,
rem * so go straight to END
rem ********************************************************************************

if %NM_TT%==0 goto END

echo Configuring ServeRAID T

if %DPLY_TYPE%==4 goto ALT-SRT1
goto U5

:ALT-SRT1
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Configuring ServeRAID T"

:U5
acu /P %RD_PATH%\%RD_STUB%%1.txt /S
if not errorlevel 100 goto TXT-ERR


rem * Configured ServeRAID T
set CFG_DONE=Y

echo Successfully configured using: %RD_PATH%\%RD_STUB%%1.txt

if %DPLY_TYPE%==4 goto ALT-SRT2
goto END

:ALT-SRT2
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Successfully configured using: %RD_PATH%\%RD_STUB%%1.txt"

goto END


:CFGEA
rem ********************************************************************************
rem * Block to configure ServeRAID E SATA controller using ACUICHSV
rem ********************************************************************************


rem ********************************************************************************
rem * Since we can be sent to :CFGEA before ever checking if this is a proper
rem * e-SATA txt file, we need to make sure we check it here.
rem ********************************************************************************
CLIni.exe %RD_PATH%\%RD_STUB%%1.txt /I:Drives /C:,
if errorlevel 100 goto end
if errorlevel 1 goto TXT-ERR


rem ********************************************************************************
rem * Configure ServeRAID E SATA controller using ACUICHSV
rem * We know %1.txt exists or we never would have gotten here, so no need
rem * to re-verify its existence. And we know the .txt file is the proper
rem * type to configure a ServeRAID E SATA controller, or we would not be here.
rem * If no EA adapters to config, no more work to do, go to END
rem ********************************************************************************
if %NM_EA%==0 goto END

echo Configuring ServeRAID E SATA

if %DPLY_TYPE%==4 goto ALT-ESA1
goto U6

:ALT-ESA1
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Configuring ServeRAID E SATA"

:U6
acuichsv /P %RD_PATH%\%RD_STUB%%1.txt /S
if not errorlevel 100 goto TXT-ERR

rem * Configured ServeRAID E SATA
set CFG_DONE=Y

set DV_NUM=

echo Successfully configured using: %RD_PATH%\%RD_STUB%%1.txt

if %DPLY_TYPE%==4 goto ALT-ESA2
goto END

:ALT-ESA2
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"CFGRAID:  Successfully configured using: %RD_PATH%\%RD_STUB%%1.txt"
goto END


rem ********************************************************************************
rem * Error handling...
rem ********************************************************************************
:BAT-ERR
rem * Failed to configure ServeRAID
rem * Failed to configure Integrated SCSI with RAID capabilities
rem * Failed to configure IDE RAID
rem * Failed to configure ServeRAID E SCSI
echo Failed to configure using: %RD_PATH%\%RD_STUB%%1.bat

if %DPLY_TYPE%==4 goto ALT-BAT
goto ERROR

:ALT-BAT
%TKDRV%\logevent.exe -c:2 -l:2 -ss:"CFGRAID:  Failed to configure using: %RD_PATH%\%RD_STUB%%1.bat"
goto ERROR


rem ###############################################################################
:TXT-ERR
rem * Failed to configure ServeRAID T
rem * Failed to configure ServeRAID E SATA
echo Failed to configure using: %RD_PATH%\%RD_STUB%%1.txt

if %DPLY_TYPE%==4 goto ALT-TXT
goto ERROR

:ALT-TXT
%TKDRV%\logevent.exe -c:2 -l:2 -ss:"CFGRAID:  Failed to configure using: %RD_PATH%\%RD_STUB%%1.txt"
goto ERROR

rem ###############################################################################


:ERROR

rem ********************************************************************************
rem * Hit an error in this batch file.
rem ********************************************************************************
FOR %%i in (y Y) do if %RD_IE% == %%i goto SOFTSTOP
goto HARDSTOP


:SOFTSTOP
echo Continuing to process RAID configurations based on RD_IE=Y, ignore errors.


if %CFG_DONE%==N set CFG_DONE=F

rem * Set that we have had a RAID configuration failure.  Needed by Altiris.
set RERS=Y
goto END


:HARDSTOP
echo Encountered an error and stopped processing in "raidmgr.bat".
savestat.exe /set2=251 > nul
set CFG_DONE=E

goto END


:END
%TKDRV%

:CLEANUP
rem ********************************************************************************
rem * Clean up temporary environment variables used here.
rem ********************************************************************************
set RDTYPE=
set DV_NUM=
