@echo off &setlocal EnableExtensions &color 03

::::::::::::::::  INSTALL  ::::::::::::::::

:: debug
REM set DEBUG=yes

:: GUID for KuShellExtension
set CLSID32=1bd41388-d095-4771-a5a8-ac9035850d89
set CLSID64=7a5700d2-9b39-45e1-86d5-59b6549f41d3

:: files
set appname=avs to x264
set "curdir=%~dp0"
set "curfile=%~0"
set "curdir=%curdir:~0,-1%"
set "utils_path=%curdir%\utils"
set "presets_path=%curdir%\presets"
set "genbatch_path=%utils_path%\batch\genbatch.bat"
set "queue_path=%utils_path%\batch\queue.bat"
set "outp=%utils_path%\KuShellExtension\config.xml"
set "dll32=%utils_path%\KuShellExtension\KuShell.dll"
set "dll64=%utils_path%\KuShellExtension\KuShell64.dll"
set "queue_list=%curdir%\queue\queue.txt"
set "logs_path=%curdir%\queue\logs.log"
set "icons_path=%utils_path%\icons"
set "helptxt=%curdir%\aide.txt"
set expreset=____Exemple.txt
set "queue_cnt_path=%curdir%\queue\cnt.bat"
set "proc_cnt_path=%curdir%\queue\proc.bat"

:: vars
set max_indexpos=99999
set pos_unsort=zzzzz
set presets_cnt=0
set presets_disabled=0

set txt_console= console="false"
if /i "%DEBUG%"=="yes" set "txt_console="

:: icons
set ico1=terminal.png
set ico2=go.png
set ico3=update.png
set ico4=folder.png
set ico5=help.png
set ico6=cascade.png
set ico7=wrench.png
set ico8=wrench2.png
set ico9=play.png
set ico10=presets.png
set ico11=logs.png
set ico12=shutdown.png
set ico13=del.png
set ico14=trash.png
set ico15=new.png
set nb_icons=15

:: environment
set regenv_path=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
set reg_shutdown=avstox264_shutdown
set reg_delbat=avstox264_delbat

:: display options
if "%~1"=="update" (title %appname% - Mise a jour) else (title %appname% - Installation)
if /i "%DEBUG%" NEQ "yes" (mode con cols=40 lines=10)

:: config default values
set txt_delbat=NON
set txt_shutdown=NON
set nb_bat=0
if "%~1"=="update" (
	for /f "tokens=3" %%a in ('reg query "%regenv_path%" /v "%reg_delbat%" 2^>nul') do (if /i "%%a"=="yes" set txt_delbat=OUI)
	for /f "tokens=3" %%a in ('reg query "%regenv_path%" /v "%reg_shutdown%" 2^>nul') do (if /i "%%a"=="yes" set txt_shutdown=OUI)
	for /f "usebackq eol=: delims=" %%a in ("%queue_list%") do (set /a nb_bat+=1)
)

:: presests list, read indexpos and check if preset is disabled
for /f "usebackq delims=" %%i in (`dir /B /O:N "%presets_path%\*.txt"`) do (
	set "preset_name=%%~i"

	setlocal EnableDelayedExpansion
	for /f "delims=" %%j in ("!preset_name!") do (
		set "preset=%presets_path%\!preset_name!"
		set skippreset=no

		if /i "!preset_name!"=="%expreset%" (
			set skippreset=yes

		) else (
			set indexpos=0000%pos_unsort%

			for /f "usebackq eol=# tokens=*" %%a in (`type "!preset!"^| find "="`) do (
				for /f "tokens=1 delims=#" %%b in ("%%a") do (
					for /f "tokens=1* delims==" %%c in ("%%b") do (
						set "var=%%c"
						set "data=%%d"
						call :trim var
						call :trim data
						if /i "!var!"=="disable" if /i "!data!"=="yes" (
							set skippreset=yes
							set /a presets_disabled+=1
						)
						if "!skippreset!"=="no" if /i "!var!"=="indexpos" (
							for /f "useback tokens=*" %%x in ('!data!') do set "data=%%~x"
							if !data! EQU +!data! if +!data! GTR +0 if +!data! LEQ +%max_indexpos% (
								set indexpos=0000!data!
							)
						)
					)
				)
			)
		)

		if "!skippreset!"=="no" (
			set indexpos=!indexpos:~-5!
			set cnt=00!presets_cnt!
			set cnt=!cnt:~-3!
			set "varname=_sortlist_!indexpos!_!cnt!"
		)

		for /f "tokens=1-3 delims=," %%v in (""!skippreset!","!presets_disabled!","!varname!"") do (
			endlocal
			set "presets_disabled=%%~w"
			if "%%~v"=="no" (
				set "%%~x=%%~j"
				set /a presets_cnt+=1
			)
		)
	)
)

:: presets sorted list for xml config file
set num_preset=0
if "presets_cnt" NEQ "0" (
	for /f "tokens=1* delims==" %%a in ('set _sortlist_') do (
		set "preset_name=%%~nb"
		set "preset_xml=%%~b"
		set /a "num_preset+=1"

		setlocal EnableDelayedExpansion
		set "preset_name=!preset_name:&=&amp;&amp;!"
		set "preset_xml=!preset_xml:&=&amp;!"
		set "preset_xml=!preset_xml:%%=%%%%!"

		for /f "tokens=1-3 delims=:" %%v in (""!num_preset!":"!preset_name!":"!preset_xml!"") do (
			endlocal
			set "preset_name_%%~v=%%~w"
			set "preset_xml_%%~v=%%~x"
		)
	)
)

setlocal EnableDelayedExpansion

::generate config file
>nul chcp 1252
>"!outp!" (
	echo ^<?xml version="1.0" encoding="utf-8" standalone="yes"?^>
	echo ^<config version="1"^>
	echo   ^<var name="CLSID32"^>{%CLSID32%}^</var^>
	echo   ^<var name="CLSID64"^>{%CLSID64%}^</var^>
	echo   ^<var name="genbatch"^>!genbatch_path!^</var^>
	echo   ^<var name="queue"^>!queue_path!^</var^>
	echo   ^<var name="icons"^>!icons_path!^</var^>

	for /L %%i in (1,1,%nb_icons%) do echo   ^<var name="ico%%i"^>${var:icons}\!ico%%i!^</var^>

	echo.
	echo   ^<menuitem name="----" class="* folder"/^>
	echo   ^<menu name="!appname!" icon="${var:ico1}" class="* folder"^>
)

:: presets list
for /L %%i in (1,1,%presets_cnt%) do (
	>>"!outp!" echo     ^<menuitem name="!preset_name_%%i!" icon="${var:ico2}" class="*" multiple="0"!txt_console!^>^&quot;${var:genbatch}^&quot; ^&quot;!preset_xml_%%i!^&quot; %%*^</menuitem^>
)

:: end of config file
>>"!outp!" (
	echo     ^<menuitem name="----" class="*"/^>
	echo     ^<menu name="Ajouter a la file d'attente" icon="${var:ico6}" class="*"^>
)

:: presets list for queue
for /L %%i in (1,1,%presets_cnt%) do (
	>>"!outp!" echo       ^<menuitem name="!preset_name_%%i!" icon="${var:ico2}" multiple="0"!txt_console!^>^&quot;${var:queue}^&quot; add_end ^&quot;!preset_xml_%%i!^&quot; %%*^</menuitem^>>>"!outp!"
)

>>"!outp!" (
	echo     ^</menu^>
	echo     ^<menuitem name="----" class="*"/^>
	echo     ^<menuitem name="Traiter la file d'attente [ %nb_bat% ]" icon="${var:ico9}" multiple="0"^>^&quot;${var:queue}^&quot; start 1^</menuitem^>
	echo     ^<menu name="Options de file d'attente" icon="${var:ico7}" class="* folder"^>
	echo       ^<menuitem name="Afficher la file d'attente" icon="${var:ico10}" multiple="0"^>^&quot;${var:queue}^&quot; show^</menuitem^>
	echo       ^<menuitem name="Vider la file d'attente" icon="${var:ico14}" multiple="0"!txt_console!^>^&quot;${var:queue}^&quot; empty^</menuitem^>
	echo       ^<menuitem name="----"/^>
	echo       ^<menuitem name="Nouveau processus d'encodage" icon="${var:ico15}" multiple="0"^>^&quot;${var:queue}^&quot; start^</menuitem^>
	echo       ^<menuitem name="----"/^>
	echo       ^<menuitem name="Eteindre le PC apres traitement [ %txt_delbat% ]" icon="${var:ico12}" multiple="0"!txt_console!^>^&quot;${var:queue}^&quot; shutdownpc^</menuitem^>
	echo       ^<menuitem name="Effacer les .bat apres traitement [ %txt_shutdown% ]" icon="${var:ico13}" multiple="0"!txt_console!^>^&quot;${var:queue}^&quot; delbat^</menuitem^>
	echo     ^</menu^>
	echo     ^<menuitem name="----"/^>
	echo     ^<menu name="Autres options" icon="${var:ico8}" class="* folder"^>
	echo       ^<menuitem name="Afficher les logs" icon="${var:ico11}" multiple="0"^>^&quot;notepad^&quot; !logs_path!^</menuitem^>
	echo       ^<menuitem name="----"/^>
	echo       ^<menuitem name="Actualiser les presets" icon="${var:ico3}" multiple="0"^>^&quot;!curfile!^&quot; update^</menuitem^>
	echo       ^<menuitem name="Afficher le dossier presets" icon="${var:ico4}" multiple="0"^>^&quot;explorer^&quot; ^&quot;!presets_path!^&quot;^</menuitem^>
	echo       ^<menuitem name="----"/^>
	echo       ^<menuitem name="Aide" icon="${var:ico5}" multiple="0"^>^&quot;notepad^&quot; ^&quot;!helptxt!^&quot;^</menuitem^>
	echo     ^</menu^>
	echo   ^</menu^>
	echo   ^<menuitem name="----" class="* folder"/^>
	echo.
	echo ^</config^>
)

:: reg dll
if /i "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
	set is64=yes
) else if /i "%PROCESSOR_ARCHITEW6432%"=="AMD64" (
	set is64=yes
)

if "%is64%"=="yes" (
	set regdll_path=HKCR\CLSID\{%CLSID64%}
) else (
	set regdll_path=HKCR\CLSID\{%CLSID32%}
)
for /f "tokens=1" %%a in ('reg query "%regdll_path%" 2^>nul') do (set is_registered=yes)

if "%is_registered%" NEQ "yes" (
	if "%is64%"=="yes" regsvr32 /s "!dll64!"
	regsvr32 /s "!dll32!"
)

:: environment variables
if "%~1" NEQ "update" (
	for %%a in ("%reg_shutdown%" "%reg_delbat%") do (
		>nul reg add "%regenv_path%" /t REG_SZ /v "%%~a" /f /d "no" 2>&1
	)
)

:: queue and logs
if "%~1" NEQ "update" (
	>"%queue_list%" type nul
	>"%logs_path%" type nul
)

:: message
if /i "%DEBUG%" NEQ "yes" cls
echo.
if "%~1"=="update" (echo   Mise … jour effectu‚e.) else (echo   !appname! est install‚.)
echo.
echo   - presets install‚s  : %presets_cnt%
echo   - presets d‚sactiv‚s : %presets_disabled%
echo.
echo.
echo   Appuyez sur une touche pour quitter.
pause>nul
goto :eof

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

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