@echo off
rem -- clean any possible remnants or problematic files
if exist *.efe erase *.efe >NUL
if exist .\temp rmdir /s /q .\temp
rem -- create input and output directories
if not exist .\[ENSONIQ_IN] mkdir [ENSONIQ_IN]
if not exist .\[EFE_OUT] mkdir [EFE_OUT]
cls

echo ########################################
echo # Ensoniq ASR/EPS Disk Extractor v0.40 #
echo ########################################
echo.
echo Place all Ensoniq images in the [ENSONIQ_IN] folder before proceding with this 
echo utility. Supported file types are: IMG, GKH, EDE, EDA, ISO and HFE. 
echo.
echo Extracted files from all Ensoniq volumes will be placed in sub-folders within  
echo the [EFE_OUT] folder when complete. Names are inherited from the input disks. 
echo.
pause
echo.

rem -- check for required tools
if not exist .\tools\hxcfe.exe (
	echo Critical Error: hxcfe cannot be found. Aborting!
	goto ENDITALL
)
if not exist .\tools\libhxcfe.dll (
	echo Critical Error: libhxcfe cannot be found. Aborting!
	goto ENDITALL
)
if not exist .\tools\libusbhxcfe.dll (
	echo Critical Error: libusbhxcfe cannot be found. Aborting!
	goto ENDITALL
)
if not exist .\tools\epslin.exe (
	echo Critical Error: EpsLin cannot be found. Aborting!
	goto ENDITALL
)
if not exist .\tools\cygwin1.dll (
	echo Critical Error: Cygwin cannot be found. Aborting!
	goto ENDITALL
)
if not exist .\tools\cyggcc_s-1.dll (
	echo Critical Error: Cygwin cannot be found. Aborting!
	goto ENDITALL
)

rem -- put all supported file types into a temporary directory before extraction
mkdir .\temp

if exist .\[ENSONIQ_IN]\*.hfe (
	for /r .\[ENSONIQ_IN] %%H in (*.HFE) do (
		echo Converting %%~NH HFE before extraction...
		rem convert HFE to raw IMG and copy into temporary directory
		.\tools\hxcfe.exe -finput:".\[ENSONIQ_IN]\%%~NH.HFE" -conv:RAW_LOADER -foutput:".\temp\%%~NH.img" >NUL
	)
	echo.
)

for /r .\[ENSONIQ_IN] %%G in (*.GKH) do (
	rem -- copy all GKH to temporary directory
	copy "%%G" .\temp >NUL
)

for /r .\[ENSONIQ_IN] %%i in (*.IMG) do (
	rem -- copy all IMG to temporary directory
	copy "%%i" .\temp >NUL
)

for /r .\[ENSONIQ_IN] %%I in (*.ISO) do (
	rem -- copy all ISO to temporary directory
	copy "%%I" .\temp >NUL
)

for /r .\[ENSONIQ_IN] %%E in (*.EDA) do (
	rem -- copy all EDA to temporary directory
	copy "%%E" .\temp >NUL

)

for /r .\[ENSONIQ_IN] %%e in (*.EDE) do (
	rem -- copy all EDE to temporary directory
	copy "%%e" .\temp >NUL
)

rem -- if there are no files in the temporary directory at this point then abort
for /f %%T in ('dir /a /b .\temp') do (
	goto EXTRACT
)

echo Critical Error: No Ensoniq images were found. Aborting!
goto ENDITALL

:EXTRACT

echo Extracting instrument, bank, and effect files from all Ensoniq volumes...
echo.

rem -- process all files in temporary folder (IMG, GKH, EDE, EDA, ISO)
for /r .\temp %%A in (*.*) do (
	rem -- get all from root
	echo Processing %%~NA...
	.\tools\epslin -ga "%%A" >NUL
	if not exist ".\[EFE_OUT]\%%~NA" mkdir ".\[EFE_OUT]\%%~NA" >NUL
	if exist *.efe move *.efe ".\[EFE_OUT]\%%~NA" >NUL
	
	rem -- allows for dynamic variables within for loops when using !variable! instead of %variable%
	setlocal ENABLEDELAYEDEXPANSION
	
	rem -- get all from any subdirectories which are one level down
	rem -- get all from any directories found in root (indexes can therefore be 1 to 38 only)
	FOR /l %%x in (1,1,38) DO (
		rem -- attempt to extract all EFEs from specific directory
		.\tools\epslin -d%%x -ga "%%A" >NUL 2>&1
		if !ERRORLEVEL! EQU 0 (
			rem -- if extraction was successful then create unique output folder and move EFEs to proper location
			if not exist ".\[EFE_OUT]\%%~NA\%%x" mkdir ".\[EFE_OUT]\%%~NA\%%x" >NUL
			if exist *.efe move *.efe ".\[EFE_OUT]\%%~NA\%%x" >NUL
		) 
		rem -- end error test
	) 
	rem -- end index for
	rem -- disable delayed expansion for the rest of the batch (return ability to use exclamations and carets)
	ENDLOCAL
) 
rem -- end file for

echo.
echo Ensoniq extraction complete! Look in [EFE_OUT] for all generated files.

:ENDITALL
rem -- shared routine for exit on critical error
echo.
pause
echo.
rem -- cleaning up temp files
if exist .\temp rmdir /s /q .\temp
EXIT /B