@echo off &setlocal EnableExtensions

::::::::::::::::  GENERATE BATCH  ::::::::::::::::

if "%~1"=="" exit /b

:: vars
set appname=avs to x264
set "preset_txt=%~1"
set "presets_path=%~dp0..\..\presets\%preset_txt%"
set "encode_path=%~dp0encode.bat"
set varlist=container ratecontrol crf video_bitrate x264_params audio_codec audio_mode audio_bitrate audio_quality downmix_mono downmix_stereo audio_bitrate_1c audio_bitrate_2c audio_bitrate_6c audio_quality_1c audio_quality_2c audio_quality_6c

setlocal EnableDelayedExpansion

:: config parser
if not exist "!presets_path!" (goto errpreset)

for /f "usebackq eol=# tokens=*" %%a in (`type "!presets_path!"^| find "="`) do (
	for /f "tokens=1 delims=#" %%b in ("%%a") do (
		for /f "tokens=1* delims==" %%c in ("%%b") do (
			set "var=%%c"
			for %%i in (!varlist!) do (
				if /i "!var!"=="%%i" (
					set "data=%%d"
					call :trim var
					call :trim data
					for /f "useback tokens=*" %%x in ('!data!') do set "data=%%~x"
					set "%%i=!data!"
				)
			)
		)
	)
)
if /i "%container%" NEQ "mp4" (set container=mkv)

setlocal DisableDelayedExpansion

:: loop label
:begin

:: files
set "avs_file=%~2"
if "%avs_file%"=="" goto :eof
set "filename_ext=%~x2"
if /i "%filename_ext%" NEQ ".avs" goto lshift
set "avs_filename=%~nx2"
set "avs_base=%~dpn2"
set "outp=%avs_base%.bat"

:: create batch
REM >nul chcp 1252
>"%outp%" (
	echo @echo off ^&setlocal EnableExtensions DisableDelayedExpansion ^&color 03
	echo.
	echo :: --------------------------------------------------
	echo ::    config. x264 / audio
	echo ::    fichier       :  "%avs_filename%"
)

REM >nul chcp 850
>>"%outp%" (
	echo :: --------------------------------------------------
	echo ::  - container     :  mkv / mp4
	echo ::  - ratecontrol   :  crf / 2p / 3p / skip     ^(skip = passer l'étape vidéo^)
	echo ::  - crf [0-^>51]   :  si ratecontrol = crf
	echo ::  - video_bitrate :  si ratecontrol = 2p ou 3p
	echo ::  - x264_params   :  paramètres supplémentaires du x264
	echo :: --------------------------------------------------
	echo ::  - audio_codec   :  mp3 / ac3 / aac / skip   ^(skip = passer l'étape audio^)
	echo ::  - audio_mode    :  crb / abr / vbr    ^(si audio_codec = mp3 / aac^)
	echo ::  - audio_bitrate :  si audio_mode = cbr / abr
	echo ::  - audio_quality :  si audio_mode = vbr
	echo ::  - downmix_mono  :  yes / no
	echo ::  - downmix_stereo:  yes / no
	echo :: --------------------------------------------------
	echo ::  - audio_bitrate_1c, 2c, 6c :  bitrates audio privilégiés si le nombre de canaux audio correspond ^(c = canal audio^)
	echo ::  - audio_quality_1c, 2c, 6c :  idem pour la qualité audio
	echo :: --------------------------------------------------
	echo ::  - shutdownPC    :   yes / no    --^> éteint le PC en fin de traitement ^(ignoré pour la file d'attente^)
	echo :: --------------------------------------------------
	echo.

	setlocal EnableDelayedExpansion
	set "avs_file_esc=!avs_file:%%=%%%%!"
	for %%a in (!varlist!) do (echo set %%a=!%%a!)
	echo set shutdownPC=no
	echo.
	echo :: --------------------------------------------------

	for /L %%i in (1,1,20) do echo.

	echo :: --------------------------------------------------
	echo :: Encodage    [ne pas modifier]
	echo :: --------------------------------------------------
	echo set "avs_file=!avs_file_esc!"
	echo set "encode_path=!encode_path!"
	echo call "%%%%encode_path%%%%" "%%~1"
	endlocal
)

:: shift
:lshift
shift /2
if "%~2" NEQ "" goto :begin
exit /b 0

:: errors
:errpreset
rem   mode silencieux = pas de message!
exit /b 1

:: functions
:trim
set var_trim=%~1
call :trim2 %%%~1%%
goto :eof

:trim2
set "%var_trim%=%*"
goto :eof
