@echo off
setlocal

if "%~1"=="" (
  echo Usage: _mp3encode.cmd input-file lame-options...
  exit /b 1
)

set "input=%~1"
set "output=%~dpn1.mp3"
shift /1

set "opts="
:collect_opts
if "%~1"=="" goto encode
set opts=%opts% %1
shift /1
goto collect_opts

:encode
for %%F in ("%input%") do if /I "%%~xF"==".flac" goto encode_flac
lame.exe %opts% "%input%" "%output%"
exit /b %ERRORLEVEL%

:encode_flac
ffmpeg.exe -hide_banner -loglevel error -i "%input%" -f wav - | lame.exe %opts% - "%output%"
exit /b %ERRORLEVEL%
