@echo off

if "%1" == "--update_2" (
   goto update_2
)

echo ---------------------------------------
echo "|  __ \     /\   | \ | |  ____/ ____|"
echo "| |__) |   /  \  |  \| | |__ | (___  "
echo "|  _  /   / /\ \ | . ` |  __| \___ \ "
echo "| | \ \  / ____ \| |\  | |    ____) |"
echo "|_|  \_\/_/    \_\_| \_|_|   |_____/ "
echo ---------------------------------------

rem set http_proxy=socks5://127.0.0.1:2080
set root_url=http://dl.ranfs.com/pub
set app_name=rftd
set dl_dir=tmp
set z=7z

title %app_name% Update batch script
echo Check permissions ....
rd "%WinDir%\system32\test_permissions" >NUL 2>NUL
md "%WinDir%\System32\test_permissions" 2>NUL
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
echo Request admin rights...
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B

:gotAdmin
rd "%WinDir%\System32\test_permissions" 2>NUL

setlocal EnableDelayedExpansion
cd /d %~dp0
rem conf url...

rem init var...
for /f "tokens=1,2* delims==" %%i in ( '%app_name% -q -s "show_app_info=1"' ) do if "%%i"=="plat" set plat=%%j>NUL
for /f "tokens=1,2* delims==" %%i in ( '%app_name% -q -s "show_app_info=1"' ) do if "%%i"=="arch" set arch=%%j>NUL
set ver_url=%root_url%/%app_name%/all/update.ini
set pkg_url=%root_url%/%app_name%/allz/%app_name%-%plat%-%arch%.7z
set z_url=%root_url%/tools/7zdec/7zdec-%plat%-%arch%.exe

rem echo %rftd_url%
rem echo %uz_url%
rem echo %ver_url%

echo Update check ....
%app_name% -q -s "update_check_url=%ver_url%" -s "update_stability_minute=60"
if %errorlevel% == 2 (
  echo start update ...
  call :Update
) ^
else if %errorlevel% == 0 (
  echo This is the latest version.
  rem call :Update
) ^
else (
  rem echo Get [%ver_url%] Failed
)
if "%1"=="" ( pause )
exit /B

:Update
mkdir %dl_dir% 2>NUL

echo get %z_url% ...
%app_name% -q -s "if=%z_url%" -s "of=%dl_dir%\%z%.exe"
if %errorlevel% NEQ 0 (
    echo get %z_url% failed, Please try again
    goto :eof
)

echo get %pkg_url% ...
%app_name% -q -s "if=%pkg_url%" -s "of=%dl_dir%\%app_name%.7z"
if %errorlevel% NEQ 0 (
    echo get %pkg_url% failed, Please try again
    goto :eof
)

cd %dl_dir%
%z% x %APP_NAME%.7z 1>NUL
if %errorlevel% NEQ 0 (
    echo unpack %APP_NAME%.7z error, Please try again
    goto :eof
)
del %z%.exe %app_name%.7z
rem save old ini crt key
del %app_name%.ini %app_name%.crt %app_name%.key

net stop %app_name% >NUL
rem echo start copy file ...
cd ..
copy update.bat %TMP%\%app_name%-update.bat >NUL
if %errorlevel% NEQ 0  (
    echo Failed to copy files. Please make sure there is space in the temporary directory
    goto :eof
)
call %TMP%\%app_name%-update.bat --update_2
goto :eof

:update_2
cd tmp
xcopy * .. /e /v /k /c /y >NUL
if %errorlevel% NEQ 0  (
    echo Failed to copy files. Please reinstall.
    goto update_2_exit
)

cd ..
rmdir /s /q %dl_dir% >NUL
net start %app_name% >NUL
echo Update completed successfully...

:update_2_exit
pause
rem not /b must exit!!
exit 
goto :eof

