@echo off
:: ===============================================================================
:: AppFileName:	InstallApp.bat
:: Date:		2025-04-09 12:04:33
:: Version:		2025-04-09
:: Author:		lsl
:: ===============================================================================
::
rem ====----	EndCmd
if "%~1"=="" (
	set "%~n0EndCmd=k"
) else (
	set "%~n0EndCmd=c"
)

if "%~2"=="" (
	exit /b
) else (
	set "AppFolder=%~2"
)

if "%~3"=="" (
	set "InstallType=curl"
) else (
	set "InstallType=%~3"
)

rem ====----	IsAdmin
rem ====	基本系統設定 set 一定要加""避免空白
set "Driver=%~d0"
set "FilePath=%~p0"
set "ConfFile=%~n0"
set "FullPath=%~dp0"
set "FullPathFile=%~f0"
rem ====----	IsAdmin
call :IsAdmin
set "InstallAppPath=%FullPath%"

TITLE %ConfFile%

setlocal enabledelayedexpansion

call :FindiniFile
rem echo !Apps_n!

if /I %InstallType% EQU Shortcut (
 	call :FindShortcutFile
)

rem ====----	 Apps_n
if /I %3 EQU all (
	if !Apps_n! NEQ 0 (
		echo ini : !Apps_n!
	)
)

if /I %3 EQU Desktop (
	if !Apps_n! NEQ 0 (
		echo Desktop : !Apps_n!
	)
)

if /I %3 EQU Shortcut (
	if !Apps_n! NEQ 0 (
		echo Shortcut : !Apps_n!
	)
)

goto End

:FindiniFile
	set /a Apps_n=0
	For /F "tokens=1* delims=" %%I in (' dir *. /B ') do (

		rem == RunDownload_cmd
		if /I %%I EQU %AppFolder% (
			if /I exist "%%I\ini" (
				rem echo."[92m%%I[0m"
				call :RunDownload_cmd %%I
			)
		)
	)
	exit /b

:RunDownload_cmd
	For /F "tokens=1* delims=" %%I in (' dir %*\ini\*.ini /B ') do (
		rem == ReadiniFile
		call :ReadiniFile %*\ini\%%I

		set "II=%%I"
		set "II=!II:.ini=!"

		if defined InstallType (
			rem echo %InstallType%
			if /I %InstallType% EQU curl (
				call download.cmd %*\ini\%%I  curl c auto
			)

			if /I %InstallType% EQU all (
				call download.cmd %*\ini\%%I  all c auto
			)

			if /I %InstallType% EQU Desktop (
				call download.cmd %*\ini\%%I  Desktop c auto
			)
		)
		if exist "%*\Downloads\!AppName!" (
			set /a Apps_n+=1
		)

	)
	exit /b

:ReadiniFile
	rem  修path
	For /F "tokens=1* delims==" %%I in ('findstr /i "^[A-z]" "%*"') do (
		set "%%I=%%J"
		rem ====	2024-08-28	()
		if defined %%I (
			if defined %%J (
				rem echo defined
				set JJ=%%J
				set "%%I=!JJ:^)=^^)!"
			)
		)
	)
	exit /b

:FindShortcutFile
	set /a Apps_n=0
	For /F "tokens=1* delims=" %%I in (' dir *. /B ') do (
		rem == RunDownload_cmd
		rem == 找到
		if /I %%I EQU %AppFolder% (
			if /I exist "%%I\Shortcut" (
				rem echo."[92m%%I[0m"

				call :RunShortcut_bat %%I
			)
		)
	)
	exit /b

:RunShortcut_bat
	set /a Apps_n=0
	if /I %InstallType% EQU Shortcut (
		For /F "tokens=1* delims=" %%I in (' dir %*\Shortcut\*.bat /B ') do (
			call %*\Shortcut\%%I c
			if  %errorlevel% EQU 0 (
				set /a Apps_n+=1
			)
		)
	)
	exit /b

:Checkdir
	rem ====----	https://stackoverflow.com/questions/18639663/how-to-pass-command-line-parameters-with-space-in-batch-file

	if exist "%*" (
		exit /b
	)
	if not exist "%*" (
		mkdir "%*"
	)
	if exist "%*" (
		echo "%*" Created
	)

	exit /b

:IsAdmin
	reg.exe query "HKU\S-1-5-19\Environment" >nul
	if not %ERRORLEVEL% EQU 0 (
		echo 必須「以系統管理員身分執行」
		echo You must have administrator rights to continue ...
		pause & exit
	)
	exit /b

:End

	cd /D "%InstallAppPath%"

	FOR /f "tokens=1-2 delims==" %%A in ('set %~n0EndCmd') do (
		set EndCmd=%%B
	)
	%ComSpec% /%EndCmd%
