@echo off
chcp 65001 >nul
title TEM Receiver - Release Build & Install

set "JAVA_HOME=D:\Program Files\Android\Android Studio\jbr"
set "ANDROID_HOME=D:\ProgramData\AndroidSdk"
set "PATH=%JAVA_HOME%\bin;%ANDROID_HOME%\platform-tools;%PATH%"

echo ========================================
echo  TEM Receiver - Release Build
echo ========================================
echo.

echo [1/4] Building Release APK...
cd /d "%~dp0android"
call gradlew.bat assembleRelease
if errorlevel 1 (
    echo.
    echo *** BUILD FAILED ***
    pause
    exit /b 1
)

set "APK=%~dp0android\app\build\outputs\apk\release\app-release.apk"
echo.
echo [2/4] APK built: %APK%

echo.
echo [3/4] Checking connected device...
adb devices
echo.

echo [4/4] Installing...
adb uninstall com.triloop.temreceiver >nul 2>&1
adb install "%APK%"
if errorlevel 1 (
    echo.
    echo *** INSTALL FAILED ***
    echo Try: adb uninstall com.triloop.temreceiver
    pause
    exit /b 1
)

echo.
echo ========================================
echo  Done! APK installed successfully.
echo  APK path: %APK%
echo ========================================
pause
