#!/bin/bash

# TaiwanStar Android 部署腳本
# 自動執行：編譯 -> 安裝 -> 啟動

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
ADB_PATH="/mnt/c/Users/User/AppData/Local/Android/Sdk/platform-tools/adb.exe"
APK_PATH="app/build/outputs/apk/S_12_XY_U_Standard_/debug/app-S_12_XY_U_Standard_-10_08_2_R-debug.apk"
PACKAGE_NAME="com.unibuy.smartdevice"
LAUNCHER_ACTIVITY=".HomeActivity"

echo "🚀 開始編譯 APK..."
./gradlew :app:assembleS_12_XY_U_Standard_Debug

if [ $? -eq 0 ]; then
    echo "✅ 編譯成功，正在安裝至手機..."
    $ADB_PATH install -r -d -t $APK_PATH
    
    if [ $? -eq 0 ]; then
        echo "🎉 安裝成功，正在啟動 App..."
        $ADB_PATH shell am start -n $PACKAGE_NAME/$LAUNCHER_ACTIVITY
    else
        echo "❌ 安裝失敗，請檢查手機連線狀態。"
    fi
else
    echo "❌ 編譯失敗，請檢查程式碼錯誤。"
fi
