@echo off
if exist temp_asr rmdir /s /q .\temp_asr
if not exist .\[EFE_IN] mkdir [EFE_IN]
cls
echo ##################################################
echo # Ensoniq MultiEFE to IMG and HFE Converter v0.7 # (ASR 1600K)
echo ##################################################
echo.
echo Input files must be Ensoniq instruments (EFE) with either *.efa, *.efe, or
echo *.ins file extensions, as generated by various Ensoniq utilities such as
echo EPSDisk, Awave, EpsLin, EnsoniqFS, and Translator.
echo.
echo EFEs must be placed in the [EFE_IN] folder. Multiple disks can be created at
echo once by using subfolders within the root of the [EFE_IN] folder.
echo.
echo In order to maximize storage, there will be no operating system, so the
echo resulting disk images will not be bootable.
echo.
echo Additionally, any created IMGs will automatically be converted into ASR
echo compatible HFEs which can be used with floppy emulators.
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\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
)
if not exist .\tools\epslin.exe (
	echo Critical Error: epslin cannot be found. Aborting!
	goto ENDITALL
)

rem -- check if any instruments exist for input within any subfolders
rem check for any present EFA disk images -- silences normal command output and also any errors produced by next "dir" command
dir /S /B /A-D .\[EFE_IN]\*.EFA >NUL 2>&1
if %ERRORLEVEL% EQU 0 (
	rem -- at least one EFE was found, so attempt disk creation
	goto CONVERT
)
rem check for any present EFE disk images -- silences normal command output and also any errors produced by next "dir" command
dir /S /B /A-D .\[EFE_IN]\*.EFE >NUL 2>&1
if %ERRORLEVEL% EQU 0 (
	rem -- at least one EFE was found, so attempt disk creation
	goto CONVERT
)
rem check for any present INS disk images -- silences normal command output and also any errors produced by next "dir" command
dir /S /B /A-D .\[EFE_IN]\*.INS >NUL 2>&1
if %ERRORLEVEL% EQU 0 (
	rem -- at least one INS was found, so attempt disk creation
	goto CONVERT
) else (
	echo Critical Error: No EFA, EFE, or INS files found to convert. Aborting!
	goto ENDITALL
)

:CONVERT

rem -- generate list of directories to processing (includes [EFE_IN] and all subfolders within it)
echo ROOT>PROCESS_LIST.TXT
DIR [EFE_IN] /AD /B /ON >>PROCESS_LIST.TXT

REM -- ### TOP OF THE DIRECTORY PROCESSING LOOP ###
for /F "usebackq delims==" %%D in (PROCESS_LIST.TXT) do (

	rem -- allows for dynamic variables within for loops when using !variable! instead of %variable%
	setlocal ENABLEDELAYEDEXPANSION

	rem -- create temporary folder for EFE processing
	mkdir .\temp_asr

	rem -- duplicate all EFEs so that processing will not change originals!
	rem -- handle INS as EFE for convenience
	if "%%D" == "ROOT" (
		rem -- handle any EFEs place in the root folder
		if exist .\[EFE_IN]\*.efe copy .\[EFE_IN]\*.efe .\temp_asr\*.efe >NUL
		if exist .\[EFE_IN]\*.ins copy .\[EFE_IN]\*.ins .\temp_asr\*.efe >NUL
		if exist .\[EFE_IN]\*.efa copy .\[EFE_IN]\*.efa .\temp_asr\*.efe >NUL
	) else (
		rem -- handle current subfolder EFEs
		if exist .\[EFE_IN]\"%%D"\*.efe copy .\[EFE_IN]\"%%D"\*.efe .\temp_asr\*.efe >NUL
		if exist .\[EFE_IN]\"%%D"\*.ins copy .\[EFE_IN]\"%%D"\*.ins .\temp_asr\*.efe >NUL
		if exist .\[EFE_IN]\"%%D"\*.efa copy .\[EFE_IN]\"%%D"\*.efa .\temp_asr\*.efe >NUL
	)

	rem -- EpsLin extracted EFEs do not need any naming fixes, but...
	rem -- EnsoniqFS extracted EFEs need to be renamed properly.
	if exist .\temp_asr\*.[??].efe (
		echo Optimizing EnsoniqFS based naming...
		rem -- change any [03].efe files to only [03]
		ren .\temp_asr\*.[??].efe *. >NUL
		rem -- change any [03] files to only efe
		ren .\temp_asr\*.[??] *.efe >NUL
	)

	rem -- check that EFE files do not exceed ASR blank capacity
	set imgsize=0
	rem -- add size of each file to total expected image size
	for %%S in (.\temp_asr\*.efe) do set /a imgsize+=%%~zS
	rem -- if root or subfolder contains EFEs then process normally
	if !imgsize! NEQ 0 (
		rem -- check if total size of EFEs can fit within one disk image
		if !imgsize! GEQ 1626112 (
			set /a oversize=!imgsize!-1626112
			echo Warning: EFE files are too large to fit on a single ASR HD disk.
			echo Only 1,626,112 bytes allowed: !oversize! bytes too many.
			echo.
			echo Process will continue, but at least one EFE will be skipped.
			echo.
		)

		echo Storing EFAs and EFEs to IMG...
		echo.

		rem -- use directory path as basename for output files
		set currentImage=%%~nxD

		if not exist [IMG_ASR_OUT] mkdir .\[IMG_ASR_OUT]
		rem -- create new blank disk image in ASR 1600K format
		.\tools\epslin -q -l DISK000 -fi ".\[IMG_ASR_OUT]\!currentImage!.IMG" asr >NUL

		rem -- stores all EFEs to single disk image (up to total capacity)
		for /r .\temp_asr %%E in (*.efe) do (
			.\tools\epslin -p1 ".\[IMG_ASR_OUT]\!currentImage!.IMG" "%%E" >NUL
			rem -- give warning if EFE could not be stored to IMG
			if !ERRORLEVEL! NEQ 0 (
				echo Warning: %%~NE could not be written to the disk image.
			) else (
				echo %%~NE was added to the disk image.
			)
		)

		rem -- show results of disk creation
		.\tools\epslin -g0 ".\[IMG_ASR_OUT]\!currentImage!.IMG"

		if not exist [HFE_ASR_OUT] mkdir .\[HFE_ASR_OUT]
		echo Generating ASR-type HFE...
		.\tools\hxcfe -finput:".\[IMG_ASR_OUT]\!currentImage!.IMG" -conv -uselayout:ENSONIQ_HD_1600KB -ifmode:IBMPC_HD_FLOPPYMODE -foutput:".\[HFE_ASR_OUT]\!currentImage!.hfe" >NUL
	rem -- otherwise just skip the empty folder
	) else (
		echo Empty folder found. Skipping...
	)
	echo.
	rem -- cleaning up temp files
	if exist temp_asr rmdir /s /q .\temp_asr

	rem -- disable delayed expansion for the rest of the batch (return ability to use exclamations and carets)
	ENDLOCAL

)
REM -- ### END OF THE DIRECTORY PROCESSING LOOP ###

if exist PROCESS_LIST.TXT del PROCESS_LIST.TXT >NUL

echo Process complete.
echo.
echo ASR compatible IMG and HFE files should now be present within the
echo [IMG_ASR_OUT] and [HFE_ASR_OUT] folders.

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