@echo off
if exist temp rmdir /s /q .\temp
if not exist .\[EFE_IN] mkdir [EFE_IN]
cls
echo ###############################################################
echo # Ensoniq EFA/EFE to IMG and HFE (ASR series) Converter v0.70 # (ASR 5100K)
echo ###############################################################
echo.
echo Input files must be Ensoniq instruments (EFE/EFA/INS) placed within the
echo [EFE_IN] folder. Files extracted by various Ensoniq utilities such as
echo EpsLin, EPSDisk, EnsoniqFS, and Translator are accepted.
echo.
echo Sub-folders are supported and will be preserved in the output structure.
echo.
echo New HFE files with corresponding names appear in the [HFE_ASR_OUT] folder when
echo the encoding task is finished. In order to maximize storage, there will be no
echo operating system, so the images will not be bootable.
echo.
echo The created HFEs will only work in ASR systems with a floppy emulator, as
echo standard floppy drives are not capable of using SuperDisk images.
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
)
if not exist .\tools\ASR10_HD_5100KB.xml (
	echo Critical Error: ASR SuperDisk XML cannot be found. Aborting!
	goto ENDITALL
)

rem check for any present INS files -- 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 INS files present, so no need to check for additional types
	goto CONVERT
) else (
	rem not surprising when there are no INS files, so continue with alternate checks
)

rem check for any present EFA files -- 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 EFA files present, so no need for addition check for EFEs
	goto CONVERT
) else (
	rem not surprising when there are no EFA files, so continue with alternate checks
)

rem check for any present EFE files -- 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% neq 0 (
	rem no EFE files present
	echo Critical Error: No EFA, EFE, or INS files found to convert. Aborting!
	goto ENDITALL
)

:CONVERT

rem -- create temporary folder for EFA/EFE/INS processing
mkdir .\temp

rem -- duplicate all EFA/EFE/INS files so that processing will not change originals!
rem -- retains sub-directory structure
echo Preparing conversion. Please wait...
xcopy ".\[EFE_IN]" ".\temp" /sy >NUL

rem check for any present INS files -- silences normal command output and also any errors produced by next "dir" command
dir /S /B /A-D .\temp\*.ins >NUL 2>&1
if %ERRORLEVEL% equ 0 (
	rem scan entire temp folder for all INS files present
	rem needs delims== so for loop does not cut off at first space generated by DIR
	for /F "delims==" %%a in ('dir /S /B /A-D .\temp\*.ins') do (
		rem -- renames INS to EFE for convenience
		ren "%%a" "%%~Na.efe" >NUL
	)
)

rem check for any present EFA files -- silences normal command output and also any errors produced by next "dir" command
dir /S /B /A-D .\temp\*.efa >NUL 2>&1
if %ERRORLEVEL% equ 0 (
	rem scan entire temp folder for all EFA files present
	rem needs delims== so for loop does not cut off at first space generated by DIR
	for /F "delims==" %%b in ('dir /S /B /A-D .\temp\*.efa') do (
		rem -- renames EFA to EFE for convenience
		ren "%%b" "%%~Nb.efe" >NUL
	)
)

rem check for any present EnsoniqFS extracted files -- silences normal command output and also any errors produced by next "dir" command
dir /S /B /A-D .\temp\*.[??].efe >NUL 2>&1
if %ERRORLEVEL% equ 0 (
	rem -- EpsLin extracted EFEs do not need any naming fixes, but...
	rem -- EnsoniqFS extracted EFEs need to be renamed properly.
	echo Optimizing EnsoniqFS based naming...
	rem scan entire temp folder for all EnsoniqFS files present
	rem needs delims== so for loop does not cut off at first space generated by DIR
	for /F "delims==" %%c in ('dir /S /B /A-D .\temp\*.[??].efe') do (
		rem -- change any [xx].efe files to only [xx]
		ren "%%c" "%%~Nc." >NUL
	)
	for /F "delims==" %%d in ('dir /S /B /A-D .\temp\*.[??]') do (
		rem -- change any [xx] files to only efe
		ren "%%d" "%%~Nd.efe" >NUL
	)
)

rem -- create a variable to represent maximum file size which can fit on one disk image
SET /A maxsize = 5189632

rem -- EFEs which are too large for ASR 5100K disks will be auto-split into multiple 5100K ready volumes
for /F "delims==" %%s in ('dir /S /B /A-D .\temp\*.efe') do (
	if %%~zs gtr %maxsize% (
		.\tools\epslin -s "%%s" asrsd >NUL
	)
)

rem -- since any large EFEs are now split, remove any of the large originals
for /F "delims==" %%s in ('dir /S /B /A-D .\temp\*.efe') do (
	if %%~zs gtr %maxsize% (
		del "%%s" >NUL
	)
)

echo Storing all EFAs and EFEs to new IMGs...
echo.

rem -- allows for dynamic variables within for loops when using !variable! instead of %variable%
setlocal ENABLEDELAYEDEXPANSION
rem scan entire temp folder for all EFE files present
rem needs delims== so for loop does not cut off at first space generated by DIR
for /F "delims==" %%e in ('dir /S /B /A-D .\temp\*.efe') do (
	rem -- create new blank disk image in ASR 5100K format using filename of EFE
	.\tools\epslin -q -l DISK000 -fi "%%~Pe\%%~Ne.img" asrsd >NUL
	rem -- store EFE to newly created disk image
	.\tools\epslin -p1 "%%~Pe\%%~Ne.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.
		echo.
	) else (
		echo %%~Ne was added to the disk image.
	)
)
rem -- disable delayed expansion for the rest of the batch (return ability to use exclamations and carets)
ENDLOCAL
echo.

echo Generating ASR-type HFEs. This may take some time, please wait...
rem scan entire temp folder for all IMG files present
rem needs delims== so for loop does not cut off at first space generated by DIR
for /F "delims==" %%t in ('dir /S /B /A-D .\temp\*.img') do (
	rem -- convert any found IMG to ASR 5100K type HFE
	.\tools\hxcfe -finput:"%%t" -conv -uselayout:.\tools\ASR10_HD_5100KB.xml -ifmode:IBMPC_HD_FLOPPYMODE -foutput:"%%~Pt\%%~Nt.hfe" >NUL
)

echo Finalizing disk images. Please wait...

rem -- should ASR HFE output folder already exist then merge temp into it
rem -- otherwise, simply cleanup and rename to save time and disk usage
if exist .\[HFE_ASR_OUT] (
	xcopy ".\temp\*.hfe" ".\[HFE_ASR_OUT]" /sy >NUL
	rmdir /s /q temp
) else (
	rem -- remove all temporary EFEs and IMGs since conversion is complete
	del /s /q .\temp\*.efe >NUL 2>&1
	del /s /q .\temp\*.img >NUL 2>&1
	rem -- rename temporary directory structure to ASR output
	ren temp [HFE_ASR_OUT]
)

echo.
echo Process complete.
echo.
echo ASR SuperDisk compatible HFE files should now be present within the
echo [HFE_ASR_OUT] folder.

:ENDITALL
rem -- shared routine for exit
echo.
pause
echo.
EXIT /B
