@echo off
:: https://stackoverflow.com/questions/67539331/what-am-i-doing-wrong-using-the-choice-command-with-batch
::
:: ===============================================================================
:: AppFileName:	ChoiceMenu.cmd
:: AppFileName:	InstallApp_Menu.cmd
:: Date:	2025-03-27 20:32:50
:: Date:	2025-04-21 00:31:54
:: Version:	1.25.04.21
:: Author:	lsl
:: ===============================================================================
::
rem ====----	EndCmd
if "%~1"=="" (
	set "%~n0EndCmd=k"
) else (
	set "%~n0EndCmd=c"
)

if "%~2"=="" (
	set "%~n0YN=n"
) else (
	set "%~n0YN=y"
)

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

rem ====----	IsAdmin
call :IsAdmin

rem ====----	setlocal
setlocal EnableDelayedExpansion

set "%~n0FullPath=%~dp0"
cd /D "!%~n0FullPath!"

title %~nx0  Dynamic menu system
set "%~n0_ConfFile=%Driver%%FilePath%%ConfFile%.ini"

call :CreateiniFile

call :ReadiniFile

if defined CmdStrAll (
	call :SelectMenu %CmdStrAll%
) else (
	call :SelectMenu "End"
)

rem :: call :ChkWbem_WMIC
rem :: cd /D "!%~n0FullPath!"

call :GetChose

goto :End

:CreateiniFile
	if not exist "!%~n0_ConfFile!" (
		dir *.bat /B /ON >"%temp%\%ConfFile%.txt"
		dir *.cmd /B /ON >>"%temp%\%ConfFile%.txt"

		echo.#>"!%~n0_ConfFile!"
		echo.END>>"!%~n0_ConfFile!"

		For /F "tokens=1* delims=" %%I in ( 'findstr /i "^[0-z]" "%temp%\%ConfFile%.txt" ' ) do (
			set "II=%%I"
			set "II=!II:~0,-4!"
			if !II! NEQ %ConfFile% (
				echo.%%I=!II!>>"!%~n0_ConfFile!"
			)
		)
	)
	exit /b

:ReadiniFile
	echo|set /p="[45m"
	echo ReadiniFile[0m
	echo|set /p="!%~n0_ConfFile!"[0m
	echo|set /p="[0m"

	if not exist "!%~n0_ConfFile!" (
		exit /b
	)

	set CmdStr=
	set OptionsStr=
	
	For /F "tokens=1,2 delims==" %%I in ( 'findstr /I "^[0-z]" "%Driver%%FilePath%%ConfFile%.ini" ' ) do (
		set "CmdStr=!CmdStr! "%%I" "
		set "OptionsStr=!OptionsStr! %%J , "
	)

	set CmdStrAll=!CmdStr!
	set OptionsStrAll=!OptionsStr!
	exit /b

:SelectMenu
	cd /D "!%~n0FullPath!"
	echo.
	echo.[104m!%~n0_ConfFile![0m
	echo.[92m %~0 [0m

	set /a "OptionNo=0"
	set "Selections="
	For %%I in (%*) do (
		set /a OptionNo+=1
		call :ResolveChar !OptionNo!
		set "Option!ReturnValue!=%%~I"
		set "Selections=!Selections!!ReturnValue!"

		if exist "!%~n0_ConfFile!" (
			For /F "tokens=1* delims==" %%I in ( 'findstr /I /B /C:"%%~I" "!%~n0_ConfFile!" ' ) do (
				set "%~n0_Promp=%%J"
			)
		) else (
			set "%~n0_Promp=END"
		)

		if defined %~n0_Promp (
			echo [!ReturnValue!] [93m !%~n0_Promp! [0m
		) else (
			echo [!ReturnValue!] [93m %%~I [0m
		)
	)
	goto :Eof

:ResolveChar
	setlocal
	set "keys=_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	set "_StartChar=%1"
	call set "GetkeyInChar=%%keys:~%_StartChar%,1%%"
	endlocal & set "ReturnValue=%GetkeyInChar%"
	goto :Eof

:GetChose
	choice /c !Selections! /m "Select Your Option: " /n
	call :ResolveChar %errorlevel%
	set "Option=!Option%ReturnValue%!"
	echo You Selected: %ReturnValue% - [92m%Option%[0m
	echo.

	if %ReturnValue% EQU 0 (
		exit /b
	) else (

	rem :: if /I %%~n0YN% EQU y (
		rem :: call :InputYN %Option%
	rem :: )
		rem == 跳離或重讀選單 2025-04-13
		rem :: if /I '!Input_YN!'=='y' (
			call :RunCall call %Option% c
		rem ::	exit /b
		rem ::	rem call :ReadiniFile
		rem :: )

		rem :::: call :SelectMenu %CmdStrAll%
		if defined CmdStrAll (
			call :SelectMenu %CmdStrAll%
		) else (
			call :SelectMenu "End"
		)
	)

	goto :GetChose

:ChkWbem_WMIC
	if not exist "%SystemRoot%\System32\wbem\WMIC.exe" (
		if exist "%SystemDrive%\PortableApps\wbem\WMIC.exe" (
			cd /D "%SystemDrive%\PortableApps\wbem\"
			exit /b
		) else (
			call :Checkdir %SystemDrive%\PortableApps\wbem\
			if not exist "%Driver%%FilePath%wbem\WMIC.exe" (
				curl.exe -LJR --url "https://www.maes.idv.tw/download/Files/Win10_Programs/Win_11/wbem_WMIC/Windows24H2_wbem_WMIC.zip" --output "%SystemDrive%\PortableApps\Windows24H2_wbem_WMIC.zip"  -k --show-error --fail --no-progress-meter
				%ComSpec% /C powershell "Expand-Archive -Path %SystemDrive%\PortableApps\Windows24H2_wbem_WMIC.zip -DestinationPath '%SystemDrive%\PortableApps\'" -Force
				cd /D "%SystemDrive%\PortableApps\wbem\"
			)
		)
	)
	exit /b

:InputYN
	set Input_YN=
	set /p Input_YN=[45m		Do you want to run %Option%? Enter yes or no (y/n^):[0m [0m
	if defined Input_YN (
		set "Input_YN=!Input_YN:~0,1!"
	)
	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

:RunCall
	%*
	exit /b

:Eof

:End
	cd /D "!%~n0FullPath!"

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