@echo off

set CRNfilename=crunch_x64.exe

if NOT exist %CRNfilename% (
echo.
echo [%CRNfilename%] is not located in this folder...
echo.
goto :End
)


:choice
cls
echo.
echo *******************************************************************************
echo *                                                                             *
echo *                 Watch Dogs    ~    Build DDS FileLists                      *
echo *                                                                             *
echo *       Creates 2 listfiles containing Alpha graphic files and any            *
echo *       other graphic files (DXTx)                                            *
echo *       Resp. [WD_AlphaList.txt] and [WD_FinalList.txt]                       *
echo *                                                                             *
echo *                                                                             *
echo *       Press [1] to start the building procedure...                          *
echo *                                                                             *
echo *                                                                             *
echo *       Remark:                                                               *
echo *       Do NOT delete these 2 files as they are used                          *
echo *       for the other procedures!                                             *
echo *                                                                             *
echo *                                                                             *
echo *  Current Folder:                                                            *
echo *  [%~dp0]
echo *                                                                             *
echo *                                                                             *
echo *******************************************************************************
echo.
set /p userinp=Type a number (0,1): 
set userinp=%userinp:~0,1%
echo.
echo.
if "%userinp%"=="0" (
echo Quiting...
goto :END
)
if "%userinp%"=="1" goto Start
echo invalid choice
goto choice



:Start
set WD_FList=WD_FListGraph.txt
set WD_Alist=WD_AlphaList.txt
set WD_FinalList=WD_FinalList.txt
set WD_Tinfo=WD_tempFileInfo.txt
set WD_Temp2=WD_tempfile2.txt
set WD_EList1=WD_ErrorList1.txt

dir *.dds /b>WD_FListGraph.txt 2>nul

type nul > %WD_EList1%

%~d0
cd %~dp0

setlocal EnableDelayedExpansion
Set Count=0
set FourCode=UNDF
for /F "delims=~" %%i in (%WD_FList%) do (
set /a count=!count!+1


crunch_x64 /info -file "%%i"> %WD_Tinfo%

rem *** check if not a DDS file...
if not !ErrorLevel! equ 0 ( 
    type %WD_Tinfo% >> %WD_EList1%
    del "%%i" "%%~ni.hdr"
) ELSE ( call :Proc_Fourcc "%%i")

rem type %WD_Temp3% >> %WD_Finallist%


)

goto END



REM ********************************************
REM *** Extracting Fourcc code   # Proc_Fourcc #
REM ********************************************
rem *** %1= "Filename"
:Proc_Fourcc

rem *** get~add Fourcc codes  ~ info seperated by comma...
findstr /L /C:"Source texture dimensions:" %WD_Tinfo%> %WD_Temp2%
for /F "tokens=1-4 delims=," %%a in (%WD_Temp2%) do (set FourCode=%%d)

rem *** code length can be between 2-9  ~  use one space as seperator...
set FourCode=xxxxx!FourCode:~1!
set FourCode=!FourCode:~-15!
echo !FourCode!> %WD_Temp2%
for /F "tokens=1-2 delims= " %%a in (%WD_Temp2%) do (set FourCode=%%b)

rem echo [!FourCode!]

rem *** DXT1A not supported by crunch
if !FourCode!==DXT1A (
echo Not Supported: %1~+~XXXX~+~!FourCode! >> %WD_EList1%
)

if not "!FourCode!"=="!FourCode:A=!" (
      >>%WD_Alist% echo %1~+~!FourCode!
) ELSE (
      >>%WD_FinalList% echo %1~+~!FourCode!)

echo !count!   ~  [!FourCode!]

goto :EOF
rem *** End of :Proc_Fourcc procedure...

:END
 
del %WD_FList% >nul 2>nul
del %WD_Tinfo% >nul 2>nul
del %WD_Temp2% >nul 2>nul

pause