::==============================================================================
::
:: Script   : trap_extract.bat
::
:: Author   : Willi Bärtsch (2004-07-30)
::
:: Input    : none
::
:: Function : This batch file scans the log directory of NVT and searches for
::            files that fit the mask of "trap_unknown*.log"
::
::            These log files (created by the NVD log writer module) are 
::            processed and each (unknown) trap is moved into a subdirectory
::            of the debug folder (created if necessary) inside the NVT log.
::
::            The logfile (NVD log writer) MUST contain: 
::                          Text1 = <subdirectory name>
::                          Text2 = <full path to trap file>
::                          Text3 = <additional info that is optional>
::                          Text4 = <not used>
::
::==============================================================================
@echo off

:: ----------------------------------------
:: Initialize some globals
:: ----------------------------------------
set _LOG_DIR=\nvt\log
set _DBG_DIR=%_LOG_DIR%\debug
set _FILEMASK=trap_unknown*.log


if exist %_DBG_DIR% rd /S %_DBG_DIR%
if exist %_DBG_DIR% goto :_main
md %_DBG_DIR%


:: ----------------------------------------
:: Loop over all files containing trap logs
:: ----------------------------------------
:_main
for /F "delims=" %%a in ('dir /b %_LOG_DIR%\%_FILEMASK% 2^>NUL') do if exist "%_LOG_DIR%\%%~a" call :_process "%_LOG_DIR%\%%~a"
goto :_cleanup



:: ----------------------------------------
:: Process, analyze one trap log file
:: ----------------------------------------
:_process
echo Processing : %1
for /F "tokens=4,5,6 delims=," %%a in (%~1) do call :_copy "%%a" %%b "%%c"
goto :_end



:: ----------------------------------------
:: Coppy the traps into individual folders
:: ----------------------------------------
:_copy
echo Copy trap file (%~1) : %2
if not exist %_DBG_DIR%\%1 md %_DBG_DIR%\%1
copy %2 %_DBG_DIR%\%1\. >NUL
echo %~nx2 : %~3 >> %_DBG_DIR%\%1\Summary.txt
goto :_end



:: ----------------------------------------
:: Cleanup all used variables
:: ----------------------------------------
:_cleanup
set _LOG_DIR=
set _DBG_DIR=
set _FILEMASK=



:: ----------------------------------------
:: End of batch script
:: ----------------------------------------
:_end
