@echo off
chcp 65001 >nul
title Pegadaian Update v1.0.3

echo.
echo === Pegadaian Update v1.0.3 ===
echo.

:: Deteksi folder instalasi dari registry atau default
set "INSTALL_DIR=C:\Program Files\Pegadaian"

for /f "skip=2 tokens=2,*" %%A in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{8E3C9F2A-7B1D-4A5E-9C0D-1F2E3A4B5C6D}_is1" /v "Inno Setup: App Path" 2^>nul') do set "INSTALL_DIR=%%B"

if not exist "%INSTALL_DIR%" (
    echo [ERROR] Folder instalasi tidak ditemukan di:
    echo        %INSTALL_DIR%
    echo.
    echo Pastikan aplikasi Pegadaian sudah terinstall.
    pause
    exit /b 1
)

echo Folder instalasi: %INSTALL_DIR%
echo.

:: Backup folder lama
set "BACKUP_DIR=%INSTALL_DIR%\backup-%date:~-4,4%%date:~-7,2%%date:~-10,2%"
echo Backup folder lama ke: %BACKUP_DIR%
mkdir "%BACKUP_DIR%" 2>nul
xcopy "%INSTALL_DIR%\*.dll" "%BACKUP_DIR%\" /y /q >nul
xcopy "%INSTALL_DIR%\*.exe" "%BACKUP_DIR%\" /y /q >nul
xcopy "%INSTALL_DIR%\*.json" "%BACKUP_DIR%\" /y /q >nul
echo Backup selesai.
echo.

:: Copy file update
echo Mengupdate file aplikasi...
xcopy "%~dp0publish-update\*" "%INSTALL_DIR%\" /e /y /q >nul
xcopy "%~dp0Fonts\*" "%INSTALL_DIR%\Fonts\" /e /y /q >nul
echo Update selesai.
echo.

:: Jalankan aplikasi
echo Menjalankan aplikasi...
start "" "%INSTALL_DIR%\Pegadaian.WPF.exe"

echo.
echo Update berhasil! Aplikasi akan terbuka.
timeout /t 3 >nul
