@echo Ihx2Wav is running...
@
@rem ! Need: hex2bin 1.0.1 or 2.5 from Jacques Pelletier
@rem download from http://hex2bin.sourceforge.net/ !
@
@rem open console,
@rem change to directory and start batch with "Ihx2wav FILENAM"
@rem if exist "FILENAM.CFG" some parameters are read from there
@rem FileToOpen needed: http://www.horstmuc.de/win/wfile.zip

@echo off
if not exist GetFName.cmd (
	%~d0
	cd %~dp0
)
if not exist GetFName.cmd if exist ..\GetFName.cmd cd ..
call SHARPSET
set fnamex=.IHX
set fnamedp=%BINDIR%\
set fnamen=%1
set fnamef=%fnamedp%%fnamen%%fnamex%

Rem Note: Filenames with 1-7 characters are recommended!
if not exist %fnamef% call GetFName %fnamedp% "*%fnamex%";*.HEX;*.ASC "%1"
if not exist %fnamef% (
             echo Error: File %fnamef% does not exist! - Ctrl+C
             goto end
)

echo %fnamen% | find "[" > NUL
if errorlevel 1 goto namelse
	set fnamenS=%fnamen%
	echo Name with bracket for char replacement: Check the name length manually.
goto namend
:namelse
	set fnamenS=%fnamen:~0,7%
	set sharpc1=%SHARPC:~0,1%
	set sharpc2=%SHARPC:~0,2%
	if "%sharpc1%" == "E"  set fnamenS=%fnamen:~0,8%
	if "%sharpc2%" == "G8" set fnamenS=%fnamen:~0,8%
	if "%sharpc2%" == "15" set fnamenS=%fnamen:~0,16%
	if "%sharpc2%" == "16" set fnamenS=%fnamen:~0,16%
	if not "%fnamenS%" == "%fnamen%" (
        echo Note: Filename with less characters is recommended!
        rem goto end
	)
	set sharpc1=
	set sharpc2=
:namend

if "%HEX2BIN%"=="" (
	if exist ..\hex2bin.exe set HEX2BIN=..\hex2bin
	if exist hex2bin.exe set HEX2BIN=hex2bin
) 
if not exist %HEX2BIN%.exe goto endhb
if exist %TEMP%\%fnamen%.BIN del %TEMP%\%fnamen%.BIN
if exist %TEMP%\%fnamen%.CFG del %TEMP%\%fnamen%.CFG
copy %fnamef% %TEMP%\ /Y > nul

echo.
rem For hex2bin v2.x recommended parameter: -b 
%HEX2BIN% %fnamef% %TEMP%\%fnamen%%fnamex% > %TEMP%\%fnamen%.CFG
del %TEMP%\%fnamen%%fnamex%
type %TEMP%\%fnamen%.CFG
if not exist %TEMP%\%fnamen%.BIN goto endh
pause

echo.
REM start address
if exist %fnamedp%%fnamen%.ADR (
		set /p ADDR=<%fnamedp%%fnamen%.ADR
) else (
 if not "%ORG%"=="" (
		set ADDR=%ORG%
 ) else  (
        set ADDR=
))
if not "%ADDR%"=="" set ADDR=--addr=%ADDR%

REM entry address
 if exist %fnamedp%%fnamen%.CAL (
		set /p ADDR2=<%fnamedp%%fnamen%.CAL
 ) 
 if not "%ADDR2%"=="" (
		if not "%ADDR%"=="" set ADDR=%ADDR% --addr=%ADDR2%
 )
set ADDR2=

set fnamec=
if exist %fnamedp%%fnamen%.CFG type %fnamedp%%fnamen%.CFG >> %TEMP%\%fnamen%.CFG
if exist %TEMP%\%fnamen%.CFG set fnamec=-m%TEMP%\%fnamen%.CFG
if exist %WAVDIR%\%fnamen%.wav if not exist %WAVDIR%\%fnamen%.bak ren %WAVDIR%\%fnamen%.wav *.bak

rem @echo on
bin2wav --type=bin %fnamec% --pc=%SHARPC% -d%SHARPDEV% --name=%fnamenS% %ADDR% %TEMP%\%fnamen%.BIN %WAVDIR%\%fnamen%.wav %BIN2OPT% %2 %3
@echo off
if errorlevel 1 goto end

echo.
echo Start CLOAD M and press a key ....
pause
start "%fnamen%" %PLAYER% %WAVDIR%\%fnamen%.wav

del %TEMP%\%fnamen%.BIN
del %TEMP%\%fnamen%.CFG
goto :EOF
:endhb
@echo Can not found hex2bin.exe! 
:endh
@echo Error An error occurred while running hex2bin. 
:end
pause