@echo off
chcp 65001 >nul
title TU DONG CAP NHAT UPDATE ZERO ONLINE REBORN
color 0A
cd /d "%~dp0"

echo =======================================================================
echo         CONG CU TU DONG CAP NHAT BAN PATCH TRUC TIEP TREN VPS         
echo =======================================================================
echo.
echo [*] Dang tien hanh tu dong quet va cap nhat he thong Update...
echo.

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ^
    "$ErrorActionPreference = 'Stop'; " ^
    "try { " ^
    "    $scriptDir = '%~dp0'.TrimEnd('\'); " ^
    "    Write-Host '[-] Thu muc dang chay:' $scriptDir -ForegroundColor Yellow; " ^
    "    $searchDirs = @($scriptDir, (Split-Path $scriptDir -Parent), 'C:\nginx\html\update', 'C:\nginx\html'); " ^
    "    $xmlFile = $null; " ^
    "    foreach ($d in $searchDirs) { " ^
    "        if (Test-Path (Join-Path $d 'resources.xml')) { $xmlFile = Join-Path $d 'resources.xml'; break; } " ^
    "    } " ^
    "    if (-not $xmlFile) { " ^
    "        $found = Get-ChildItem -Path 'C:\nginx' -Recurse -Filter 'resources.xml' -ErrorAction SilentlyContinue | Select-Object -First 1; " ^
    "        if ($found) { $xmlFile = $found.FullName; } " ^
    "    } " ^
    "    if (-not $xmlFile) { " ^
    "        Write-Host '[!] Khong tim thay resources.xml tren o dia C:\nginx. Dang thu tai tu local server...' -ForegroundColor Yellow; " ^
    "        $xmlFile = 'C:\nginx\html\update\resources.xml'; " ^
    "        Invoke-WebRequest -Uri 'http://127.0.0.1:8080/resources.xml' -OutFile $xmlFile -UseBasicParsing; " ^
    "    } " ^
    "    Write-Host '[+] Da tim thay file resources.xml tai:' $xmlFile -ForegroundColor Green; " ^
    "    $md5 = [System.Security.Cryptography.MD5]::Create(); " ^
    "    function Get-FileHashMD5($p) { " ^
    "        if (Test-Path $p) { " ^
    "            $b = [System.IO.File]::ReadAllBytes($p); " ^
    "            return [BitConverter]::ToString($md5.ComputeHash($b)).Replace('-', '').ToLower(); " ^
    "        } " ^
    "        return $null; " ^
    "    } " ^
    "    $updateExePath = Join-Path $scriptDir 'Update.exe'; " ^
    "    $batPath = Join-Path $scriptDir 'TaoIconDesktop.bat'; " ^
    "    $hashUpdate = Get-FileHashMD5 $updateExePath; " ^
    "    $hashBat = Get-FileHashMD5 $batPath; " ^
    "    if (-not $hashUpdate) { $hashUpdate = '80d123fdde585e82e9a797e86c013a15'; } " ^
    "    if (-not $hashBat) { $hashBat = '2465efd4fdd2b1475bb278684addc055'; } " ^
    "    Write-Host '[+] Hash Update.exe:' $hashUpdate -ForegroundColor Cyan; " ^
    "    Write-Host '[+] Hash TaoIconDesktop.bat:' $hashBat -ForegroundColor Cyan; " ^
    "    [xml]$xml = Get-Content $xmlFile -Encoding UTF8; " ^
    "    if ($xml.RemoteSettings.Settings.UpdateHash -ne $null) { $xml.RemoteSettings.Settings.UpdateHash = $hashUpdate; } " ^
    "    $upNode = $xml.RemoteSettings.Hashes.Hash | Where-Object { $_.file -eq 'Update.exe' }; " ^
    "    if ($upNode) { $upNode.InnerText = $hashUpdate; } " ^
    "    $batNode = $xml.RemoteSettings.Hashes.Hash | Where-Object { $_.file -eq 'TaoIconDesktop.bat' }; " ^
    "    if ($batNode) { $batNode.InnerText = $hashBat; } else { " ^
    "        $elem = $xml.CreateElement('Hash'); " ^
    "        $elem.SetAttribute('file', 'TaoIconDesktop.bat'); " ^
    "        $elem.InnerText = $hashBat; " ^
    "        $xml.RemoteSettings.Hashes.AppendChild($elem) | Out-Null; " ^
    "    } " ^
    "    $xml.RemoteSettings.Hashes.SetAttribute('count', $xml.RemoteSettings.Hashes.Hash.Count.ToString()); " ^
    "    $xml.Save($xmlFile); " ^
    "    Write-Host '[+] Da cap nhat ma Hash vao resources.xml thanh cong!' -ForegroundColor Green; " ^
    "    $verFiles = @((Join-Path $scriptDir 'version.dat'), 'C:\nginx\html\update\version.dat', 'C:\nginx\html\update\client\version.dat'); " ^
    "    $newVer = 0; " ^
    "    foreach ($vf in $verFiles) { " ^
    "        if (Test-Path $vf) { " ^
    "            $content = (Get-Content $vf -Raw).Trim(); " ^
    "            $vInt = 0; " ^
    "            if ([int]::TryParse($content, [ref]$vInt)) { " ^
    "                if ($newVer -eq 0) { $newVer = $vInt + 1; } " ^
    "                [System.IO.File]::WriteAllText($vf, $newVer.ToString()); " ^
    "                Write-Host ('[+] Da tang version.dat (' + $vf + ') len: ' + $newVer) -ForegroundColor Green; " ^
    "            } " ^
    "        } " ^
    "    } " ^
    "    Write-Host ''; " ^
    "    Write-Host '=======================================================================' -ForegroundColor Green; " ^
    "    Write-Host ' [THANH CONG RUC RO!] DA HOAN TAT CAP NHAT TREN VPS CHI VOI 1 CLICK!' -ForegroundColor Green; " ^
    "    Write-Host ' Version moi da duoc bat len: ' $newVer -ForegroundColor Yellow; " ^
    "    Write-Host ' Nguoi choi gio day chi can bam bieu tuong ngoai Desktop la tu nhan!' -ForegroundColor Green; " ^
    "    Write-Host '=======================================================================' -ForegroundColor Green; " ^
    "} catch { " ^
    "    Write-Host ('[!] CO LOI XAY RA: ' + $_.Exception.Message) -ForegroundColor Red; " ^
    "}"

echo.
echo Nhan phim bat ky de thoat...
pause >nul
exit /b
