@echo off
title WINGI Cashier - update / reinstall
echo.
echo  Updating the WINGI Cashier desktop app...

rem 1) close the running app
echo  Closing the current app...
taskkill /f /im "WINGI Cashier.exe" >nul 2>nul
taskkill /f /im "wingi-cashier.exe" >nul 2>nul
timeout /t 2 >nul

rem 2) silently uninstall the old version (found via the registry, per-user or machine)
echo  Removing the old version...
powershell -NoProfile -Command "$roots=@('HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall'); foreach($r in $roots){ Get-ChildItem $r -ErrorAction SilentlyContinue | ForEach-Object { $p=Get-ItemProperty $_.PSPath -ErrorAction SilentlyContinue; if($p.DisplayName -like '*WINGI Cashier*'){ $u=$p.QuietUninstallString; if(-not $u){ if($p.UninstallString){ $u=$p.UninstallString+' /S' } }; if($u){ Start-Process cmd -ArgumentList '/c',$u -Wait -WindowStyle Hidden } } } }" >nul 2>nul
timeout /t 3 >nul

rem 3) download the latest installer
echo  Downloading the latest installer...
set "SETUP=%TEMP%\WINGI-Cashier-Setup.exe"
powershell -NoProfile -Command "$ProgressPreference='SilentlyContinue'; try{ [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12 }catch{}; try{ Invoke-WebRequest -UseBasicParsing 'https://wingi.ahmedfahd.com/download/WINGI-Cashier-Setup.exe' -OutFile '%SETUP%' }catch{}"
if not exist "%SETUP%" (
  echo.
  echo  ERROR: could not download the installer. Check the internet and retry.
  echo.
  pause
  exit /b
)

rem 4) install the new version (NSIS one-click silent install)
echo  Installing the new version...
"%SETUP%" /S
echo.
echo  ============================================
echo   WINGI Cashier (latest) is installed.
echo   It will start shortly.
echo  ============================================
echo.
timeout /t 6 >nul
exit /b
