@echo off
:: ===============================================================================
:: AppFileName:	ChoiceMenu.bat
:: AppFileName:	WinSetup123.bat
:: Date:	2025-04-21 00:31:54
:: Version:	1.25.04-21
:: Author:	lsl
:: WinSetup123
:: ===============================================================================
::
rem ====----	EndCmd
if "%~1"=="" (
	set "%~n0EndCmd=k"
) else (
	set "%~n0EndCmd=c"
)

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 "%~n0Driver=%~d0"
:: set "%~n0FilePath=%~p0"
set "%~n0ConfFile=%~n0"
set "%~n0FileExtension=%~x0"
set "%~n0FullPath=%~dp0"
:: set "%~n0FullPathFile=%~f0"

set "existFileName=rsync.exe"

cd /D "!%~n0FullPath!"

title %~nx0

rem ====	IpConfigRenew
call :IpConfigRenew

rem ====	Download7z
set "Download=https://www.7-zip.org/a/7z2501-x64.exe"
set "FileName=7z2501-x64.exe"
call :Download7z

set "AppRoot=%HOMEDRIVE%\PortableApps\Shortcut\"
rem ====	DownloadShortcut
set "Download=https://optimumx.com/download/Shortcut.zip"
set "FileName=Shortcut.zip"
set "UnZipFile=*"
call :DownloadShortcut

set "AppRoot=%HOMEDRIVE%\PortableApps\"
rem ====	Downloadcwrsync
set "Download=https://github.com/itefixnet/download/releases/download/cwrsync-client-v6.4.5/cwrsync_6.4.5_x64_free.zip"
set "FileName=cwrsync_6.4.5_x64_free.zip"
set "UnZipFile=*"
call :Downloadcwrsync

call :MakeShortcut
call :Make7zFM
call :MakeMenuCmd

rem ====	RunMenuCmd
call :RunMenuCmd

call :RunAnswer "Do you want your computer to run Explorer?"  RunExplorer
echo.[92m!%~n0ConfFile!!%~n0FileExtension![0m

goto End

:IpConfigRenew
	if exist "%SystemDrive%\Windows\System32\ipconfig.exe" (
		echo	ipconfig.exe /renew
		%SystemDrive%\Windows\System32\ipconfig.exe /renew >nul
	)
	exit /b

:Download7z
	if not exist "%ProgramFiles%\7-Zip\7z.exe" (
		if not exist "%USERPROFILE%\Downloads\%FileName%" (
			echo|set /p="[92m"
			echo curl.exe -LJR --url "%Download%" -o "%USERPROFILE%\Downloads\%FileName%" -k --show-error --fail
			echo|set /p="[0m"
			curl.exe -LJR --url "%Download%" -o "%USERPROFILE%\Downloads\%FileName%" -k --show-error --fail
		)

		if exist "%USERPROFILE%\Downloads\%FileName%" (
			echo|set /p="[92m"
			echo "%USERPROFILE%\Downloads\%FileName%"  /S
			echo|set /p="[0m"
			"%USERPROFILE%\Downloads\%FileName%"  /S
		)
	)
	exit /b

:DownloadShortcut
	if not exist "%SystemDrive%\PortableApps\Shortcut\Shortcut.exe" (
		if not exist "%USERPROFILE%\Downloads\%FileName%" (
			echo|set /p="[92m"
			echo curl.exe -LJR --url "%Download%" -o "%USERPROFILE%\Downloads\%FileName%" -k --show-error --fail
			echo|set /p="[0m"
			curl.exe -LJR --url "%Download%" -o "%USERPROFILE%\Downloads\%FileName%" -k --show-error --fail
		)

		if exist "%USERPROFILE%\Downloads\%FileName%" (
			echo|set /p="[92m"
			echo call :7z x "%USERPROFILE%\Downloads\%FileName%" "%AppRoot%" "%UnZipFile%"
			echo|set /p="[0m"
			call :7z x "%USERPROFILE%\Downloads\%FileName%" "%AppRoot%" "%UnZipFile%"
		)
	)
	exit /b

:Downloadcwrsync
	if not exist "%AppRoot%bin\%existFileName%" (
		call :Checkdir %AppRoot%
		if not exist "%USERPROFILE%\Downloads\%FileName%" (
			echo|set /p="[92m"
			echo curl.exe -LJR --url "%Download%" -o "%USERPROFILE%\Downloads\%FileName%" -k --show-error --fail
			echo|set /p="[0m"
			curl.exe -LJR --url "%Download%" -o "%USERPROFILE%\Downloads\%FileName%" -k --show-error --fail
		)
		if exist "%USERPROFILE%\Downloads\%FileName%" (
			echo|set /p="[92m"
			echo call :7z x "%USERPROFILE%\Downloads\%FileName%" "%AppRoot%" "%UnZipFile%"
			echo|set /p="[0m"
			call :7z x "%USERPROFILE%\Downloads\%FileName%" "%AppRoot%" "%UnZipFile%"
		)
	)
	exit /b

:RunMenuCmd

	For /F "tokens=1 delims=" %%I in ( 'dir *_Menu.cmd /S /B  ^| findstr /R /C:"%~n0_Menu.cmd" ' ) do (
		set "%~n0_Menu=%%I"
	)

	if not defined %~n0_Menu (
		exit /b
	)

	set "%~n0_Menu_Path=!%~n0_Menu:%~n0_Menu.cmd=!"
	cd /D "!%~n0_Menu_Path!"

	call %~n0_Menu.cmd c
	rem :: call %~n0_Menu.cmd c y
 
	exit /b

:MakeShortcut
	::	MakeShortcut.bat
	::
	set "ShortCutRoot=%HOMEDRIVE%\PortableApps\ShortCut"
	set "ShortCutName=ShortCut.exe"
	::
	set "AppNameRoot=%HOMEDRIVE%\PortableApps\ShortCut"
	set "AppName=ShortCut.cmd"
	::
	set "AppLinkRoot=%PUBLIC%\desktop\PortableApps"
	set "AppLinkFile=ShortCut.exe.lnk"
	::
	set "AppICON=%AppNameRoot%\Shortcut.exe,0"
	::

	::	2024-08-31
	call :Checkdir "%AppLinkRoot%"
	::
	if not exist "%AppLinkRoot%\%AppLinkFile%" (
		"%ShortCutRoot%\%ShortCutName%" /T:"%AppNameRoot%\%AppName%" /F:"%AppLinkRoot%\%AppLinkFile%" /I:"%AppICON%" /A:c
		echo "%ShortCutRoot%\%ShortCutName%" /T:"%AppNameRoot%\%AppName%" /F:"%AppLinkRoot%\%AppLinkFile%" /I:"%AppICON%" /A:c
	)

	exit /b

:Make7zFM
	::	Make7zFM.bat
	::
	set "ShortCutRoot=%HOMEDRIVE%\PortableApps\ShortCut"
	set "ShortCutName=ShortCut.exe"
	::
	set "AppNameRoot=%ProgramFiles%\7-Zip"
	set "AppName=7zFM.exe"
	::
	set "AppLinkRoot=%PUBLIC%\Desktop\Desktop"
	set "AppLinkFile=7-Zip File Manager.lnk"
	::
	set "AppICON=%AppNameRoot%\%AppName%,0"
	::

	::	2024-08-31
	call :Checkdir "%AppLinkRoot%"
	::
	if not exist "%AppLinkRoot%\%AppLinkFile%" (
		"%ShortCutRoot%\%ShortCutName%" /T:"%AppNameRoot%\%AppName%" /F:"%AppLinkRoot%\%AppLinkFile%" /I:"%AppICON%" /A:c
		echo "%ShortCutRoot%\%ShortCutName%" /T:"%AppNameRoot%\%AppName%" /F:"%AppLinkRoot%\%AppLinkFile%" /I:"%AppICON%" /A:c
	)
	exit /b

:MakeMenuCmd
	::
	::	MakersyncSamba.bat
	::
	set "ShortCutRoot=%HOMEDRIVE%\PortableApps\ShortCut"
	set "ShortCutName=ShortCut.exe"
	::
	set "AppNameRoot=%FullPath%"
	set "AppName=%~n0.bat"
	::
	set "AppLinkRoot=%PUBLIC%\Desktop\PortableApps"
	set "AppLinkFile=%~n0.bat.lnk"
	::
	set "AppICON=%AppNameRoot%\%AppName:.bat=%.ico"
	::

	::	2024-08-31
	call :Checkdir "%AppLinkRoot%"
	::
	if not exist "%AppLinkRoot%\%AppLinkFile%" (
		"%ShortCutRoot%\%ShortCutName%" /T:"%AppNameRoot%\%AppName%" /F:"%AppLinkRoot%\%AppLinkFile%" /I:"%AppICON%" /A:c
		echo "%ShortCutRoot%\%ShortCutName%" /T:"%AppNameRoot%\%AppName%" /F:"%AppLinkRoot%\%AppLinkFile%" /I:"%AppICON%" /A:c
	)
	exit /b

:7z
	rem ====----	parameters 1 2 3 4
	rem ====	mode file outpath file
	rem 2025-03-30
	call :FileExist %~2

	if %FileIsExist% EQU 0 (
		goto End
	)

	echo|set /p="[92m"
	echo "%ProgramFiles%\7-Zip\7z.exe" %1 -y %2 -o%3 %4 -p%password% -aoa
	echo|set /p="[0m"
	"%ProgramFiles%\7-Zip\7z.exe" %1 -y %2 -o%3 %4 -p%password% -aoa > nul

	exit /b

:RunAnswer
	set Input_run=
	set /p Input_run=[45m%~1 Enter yes or no (y/n^):[0m [0m
	if not '%Input_run%'=='' set Input_run=%Input_run:~0,1%
	if '%Input_run%'=='' (
		exit /b
	)
	if /i '%Input_run%'=='y' (
		goto %~2
	) else (
		exit /b
	)

	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

:RunExplorer
	call Explorer.exe /root,!%~n0FilePath!
	goto End

: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

:FileExist
	set /a FileIsExist=0
	if exist "%*" (
		set /a FileIsExist=1
		rem echo.	%FileIsExist%
	) else (
		set /a FileIsExist=0
		rem echo.	%FileIsExist%
		exit /b
	)
	exit /b

:DelFile
	echo.[92mrd /Q /S %*[0m
	rd /Q /S %*
	exit /b

:RunCall
	%*
	exit /b

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

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