::==============================================================================
::
:: Script   : trap_replay.bat
::
:: Author   : Willi Bärtsch (2004-07-30)
::
:: Input    : <Optional replay path>
::
:: Function : If there is a replay path given, this batch scans that directory
::            for any trap files (*.trp). If there is no path given the batch
::            defaults to the NVT log directory.
::
::            All found traps are sequencually injected into nvglobal.bat to
::            fully reprocess the trap by NVT (good for testing and debugging)
::
::==============================================================================
@echo off

:: ----------------------------------------
:: Initialize some globals
:: ----------------------------------------
set _NVT_DIR=\nvt
set _LOG_DIR=%_NVT_DIR%\log
set _FILEMASK=*.trp
set _TRP_BAT=nvglobal.bat

if "%~1"=="" (set _SRC_DIR=%_LOG_DIR%
) else       set _SRC_DIR=%~1

if exist "%_SRC_DIR%" goto :_dirok
echo Trap source directory "%_SRC_DIR%" is not valid!
goto :_cleanup
:_dirok
if exist %_NVT_DIR%\%_TRP_BAT% goto :_main
echo Trap environment "%_SRC_DIR%\%_TRP_BAT%" not available!
goto :_cleanup



:: ----------------------------------------
:: Loop over all trap files
:: ----------------------------------------
:_main
echo Replay path : %_SRC_DIR%
for /F "delims=" %%a in ('dir /b "%_SRC_DIR%.\%_FILEMASK%" 2^>NUL') do if exist "%_SRC_DIR%\%%a" call :_dotrap %%a
goto :_cleanup



:: ----------------------------------------
:: Start trap processing
:: ----------------------------------------
:_dotrap
cd %_NVT_DIR%
call %_TRP_BAT% "%_SRC_DIR%\%1"
ping localhost > NUL
goto :_end



:: ----------------------------------------
:: Cleanup all used variables
:: ----------------------------------------
:_cleanup
set _NVT_DIR=
set _LOG_DIR=
set _SRC_DIR=
set _FILEMASK=
set _TRP_BAT=



:: ----------------------------------------
:: End of batch script
:: ----------------------------------------
:_end
