@echo off
pushd "%~dp0"
set appPath=%~dp0

set URL=https://raw.githubusercontent.com/cjx82630/admuncher/master/CustomStrings.dat

for /f "usebackq tokens=*" %%A in (`wget -t 1 -S --spider --no-check-certificate %URL% 2^>^&1 ^| FINDSTR ^/i "ETag: "`) do (
    set SIZE=%%A
)

echo %SIZE% >log.txt

for /f "tokens=2 delims= " %%B in (log.txt) do (
     set B=%%B
)

for /f "tokens=2 delims= " %%C in (log_prv.txt) do (
     set C=%%C
)

if %B% neq %C% (goto update) else (goto end)

:update
cls
echo.
echo 尝试更新
echo.
copy /y CustomStrings.dat temp.dat >nul
wget -t 1 --no-check-certificate %URL% -O temp_update.dat 2>&1 | FINDSTR "saved" &&goto endR

echo.
cls
echo.
echo 存在网络等问题。
echo.
echo 3秒后退出。
ping -n 3 127.1 >nul
exit

:end
cls
echo.
echo 无需更新，3秒后退出。
echo.
ping -n 3 127.1 >nul
exit

:endR
cls
copy /y log.txt log_prv.txt >nul
copy /y temp_update.dat CustomStrings.dat >nul
copy /y temp.dat CustomStrings备份.dat >nul
echo.
echo 已更新，5秒后重开奶牛。
echo.
echo 安装目录里有旧规则的备份。
echo.
ping -n 5 127.1 >nul
taskkill /f /im AdMunch.exe >nul
start "" "%appPath%AdMunch.exe"
exit
