@echo Bas2tap is running...
@rem open console,
@rem change to directory and start batch with "bas2tap FILENAM"
@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=.BAS
set fnamedp=%BASDIR%\
set fnamen=%1
set fnamef=%fnamedp%%fnamen%%fnamex%
set tapex=.tap

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

set sharpc2=%SHARPC:~0,2%
if "%sharpc2%" == "15" set tapex=_B.tap
Rem PC15..tap bas: _B.tap, bin: _LM.tap, dat: _D.tap, rsv: _R.tap

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%
	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=
:namend
set sharpc2=

bas2img --pc=%SHARPC% %fnamef% %BAS2OPT%
if errorlevel 1 goto end
rem pause
echo.

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

REM PC-1500 or newer Entry word
 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 fnamenT=%fnamenS%
setlocal enabledelayedexpansion 
rem for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") do ( 
rem     set "fnamenS=!fnamenS:%%~a!" 
rem )
rem @echo on
bin2wav --type=img --pc=%SHARPC% --tap --name=%fnamenS% %ADDR% %fnamedp%%fnamen%.img %TAPDIR%\%fnamenS%%tapex% %2 %3
@echo off
rem pause
if errorlevel 1 goto end

for %%a in ("_= " ) do ( 
    set "fnamenT=!fnamenT:%%~a!" 
)
if not "%fnamenT%" == "%fnamenS%"  (
	copy "%TAPDIR%\%fnamenS%%tapex%" "%TAPDIR%\%fnamenT%%tapex%"
	del  "%TAPDIR%\%fnamenS%%tapex%"
)

echo.
del %fnamedp%%fnamen%.img
copy "%TAPDIR%\%fnamenT%%tapex%" %ANDDIR%\
pause
echo Copy %TAPDIR%\%fnamenT%%tapex%
echo to your Android device into the app directory
echo %ANDDIR% and import it!
echo.
echo NOTE: 
echo 1. Import %fnamenT%%tapex% 
echo    from %ANDDIR%
echo    (%TAPDIR%),
echo    use the menu inside of your emulator application
echo 2. Start CLOAD "%fnamenT%" inside the emulated BASIC Interpreter
endlocal
pause
goto :EOF
:end
pause