@echo off
rem ************************************************************************************
rem * Description: 	Configures RAID per customer input PRAID policy file
rem * Name: 		CFGRAID.BAT
rem * Dependencies:	PRAID.EXE, HWDETECT.EXE, SETERRLV.EXE, SAVERC.BAT, TKOUT.BAT
rem * Parameters:	
rem *			%1 = Input 	Policy filename for PRAID
rem *			%2 = Output Log File (Optional. Output file for logging)
rem * Return Code:
rem * 			0     = Success 
rem * 			>=1   = Error code defined by PRAID.exe
rem *
rem ************************************************************************************

rem ************************************************************************************
rem *** Save Enviroment variables
rem ************************************************************************************
Set > tkenv.ini
Clini tkenv.ini /E /V: /B:tkclr.bat
Clini tkenv.ini /E /B:tkload.bat
del tkenv.ini

echo SET COMSPEC=%COMSPEC%>> tkclr.bat
echo SET PATH=%PATH%>> tkclr.bat
echo SET AGENTCMD=%AGENTCMD%>> tkclr.bat
call tkclr.bat

rem ************************************************************************************
rem *** Initialize Return Code
rem ************************************************************************************
set RC=0



rem ************************************************************************************
rem *** Check Parameters
rem ************************************************************************************
if "%1" == "" goto PERROR
SET RAIDLOG=%2

rem ************************************************************************************
rem *** Run RAID Configuration Utility
rem ************************************************************************************
set MSG="Executing PRAID.EXE ....."
call TKOUT.BAT
if [%RAIDLOG%] == [] goto SCREEN1

rem *** Saving env vars
set     >>%RAIDLOG%
rem *** Saving policy file
type %1 >>%RAIDLOG%
rem *** LSI-SAS-RAID
rem *** DOn't redirect if LSI-SAS-RAID
hwdetect /vid:1000 /did:0058
if errorlevel 1 goto SCREEN1
hwdetect /vid:1000 /did:0050
if errorlevel 1 goto SCREEN1
hwdetect /vid:1000 /did:0056
if errorlevel 1 goto SCREEN1
hwdetect /vid:1000 /did:005C
if errorlevel 1 goto SCREEN1
hwdetect /vid:1000 /did:0054
if errorlevel 1 goto SCREEN1
hwdetect /vid:1000 /did:0062
if errorlevel 1 goto SCREEN1

PRAID /v:5 /b /l /y /f:%1 >>%RAIDLOG%
if errorlevel 1 goto ERROR_1
goto CHECK1
:SCREEN1
PRAID /v:5 /b /l /y /f:%1
if errorlevel 1 goto ERROR_1

:CHECK1
rem * Check return code for MSDOS
if not exist PRDERR.BAT goto RUN
call PRDERR.BAT

if %PRDERR% == 9 goto ERROR_2
if %PRDERR% == 8 goto ERROR_2
if %PRDERR% == 7 goto ERROR_2
if %PRDERR% == 6 goto ERROR_2
if %PRDERR% == 5 goto ERROR_2
if %PRDERR% == 4 goto ERROR_2
if %PRDERR% == 3 goto ERROR_2
if %PRDERR% == 2 goto ERROR_2
if %PRDERR% == 1 goto ERROR_2

goto RUN

rem ********************************************************************************
rem * Run the output script file if there were RAID controllers in the system
rem ********************************************************************************
:RUN
set PRDERR=0
set MSG="Executing PRAIDOUT.BAT ....."
call TKOUT.BAT
if not exist PRAIDOUT.BAT goto RAIDGOOD
if [%RAIDLOG%] == [] goto SCREEN2
COMMAND /C PRAIDOUT.BAT >>%RAIDLOG%
goto CHECK2
:SCREEN2
COMMAND /C PRAIDOUT.BAT

:CHECK2
rem * Check return code for MSDOS
if not exist PRDERR.BAT goto CHECK3
call PRDERR.BAT
:CHECK3
if %PRDERR% == 0 goto RAIDGOOD
goto ERROR_2

:PERROR
set MSG="Missing script parameter."
call TKOUT.BAT
set RC=1
goto ERROR

:ERROR_1
call saverc.bat
goto RAIDBAD

:ERROR_2
set RC=%PRDERR%
goto RAIDBAD

:RAIDBAD
set MSG="Configure RAID failed."
call TKOUT.BAT
goto ERROR

:RAIDGOOD
set MSG="Configure  RAID succeeded."
call TKOUT.BAT
goto END

:ERROR
set MSG="Encountered an error and stopped processing in cfgraid.bat"
call TKOUT.BAT
goto END

:END
SET RAIDLOG=
SET PRDERR=

call tkload.bat
del tkload.bat
del tkclr.bat
SETERRLV %RC%

