name: Android Build

on:
  push:
    branches: [ main, master, develop ]
  pull_request:
    branches: [ main, master ]
  release:
    types: [ published ]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  generate-port-o2r:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true
    - name: Install dependencies
      run: sudo apt-get install gcc g++ git cmake ninja-build lsb-release
    - name: ccache
      uses: hendrikmuhs/ccache-action@v1.2.18
      with:
        key: ${{ runner.os }}-o2r-ccache-${{ github.ref }}-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-o2r-ccache-${{ github.ref }}
          ${{ runner.os }}-o2r-ccache-
    - name: Cache build folders
      uses: actions/cache@v4
      with:
        key: ${{ runner.os }}-o2r-build-${{ github.ref }}-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-o2r-build-${{ github.ref }}
          ${{ runner.os }}-o2r-build-
        path: |
          tools/Torch/cmake-build-release
    - name: Generate starship.o2r
      run: |
        export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
        make -C tools/Torch type=release -j3
        tools/Torch/cmake-build-release/torch pack port starship.o2r o2r
    - uses: actions/upload-artifact@v4
      with:
        name: starship.o2r
        path: starship.o2r
        retention-days: 1

  build-android:
    needs: generate-port-o2r
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        build-type: [debug, release]
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true
        
    - name: Set up JDK 17
      uses: actions/setup-java@v4
      with:
        java-version: '17'
        distribution: 'temurin'
        
    - name: Setup Android SDK
      uses: android-actions/setup-android@v3
      
    - name: Install NDK 28
      run: |
        echo "y" | ${ANDROID_HOME}/cmdline-tools/latest/bin/sdkmanager --install "ndk;28.2.12604990" || \
        echo "y" | ${ANDROID_HOME}/cmdline-tools/latest/bin/sdkmanager --install "ndk;28.0.12433566"
        
    - name: Install dependencies
      run: sudo apt-get install -y ninja-build
      
    - name: Download starship.o2r
      uses: actions/download-artifact@v4
      with:
        name: starship.o2r
        path: android/app/src/main/assets 
        
    - name: Configure Gradle permissions
      run: chmod +x android/gradlew
        
    - name: Build Debug APK
      if: matrix.build-type == 'debug'
      run: |
        cd android/
        ./gradlew assembleDebug -P elfBuildType=RelWithDebInfo
        mv app/build/outputs/apk/debug/app-debug.apk ../starship-debug.apk
        
    - name: Setup signing keystore
      if: matrix.build-type == 'release' && env.KEYSTORE_FILE != ''
      run: |
        echo "${{ secrets.KEYSTORE_FILE }}" | base64 -d > android/app/starship-release-key.keystore
        echo "STARSHIP_STORE_FILE=starship-release-key.keystore" >> android/keystore.properties
        echo "STARSHIP_STORE_PASSWORD=${{ secrets.KEYSTORE_PASSWORD }}" >> android/keystore.properties
        echo "STARSHIP_KEY_ALIAS=${{ secrets.KEY_ALIAS }}" >> android/keystore.properties
        echo "STARSHIP_KEY_PASSWORD=${{ secrets.KEY_PASSWORD }}" >> android/keystore.properties
      env:
        KEYSTORE_FILE: ${{ secrets.KEYSTORE_FILE }}
        
    - name: Build Release APK
      if: matrix.build-type == 'release'
      run: |
        cd android/
        ./gradlew assembleRelease -P elfBuildType=RelWithDebInfo
        mv app/build/outputs/apk/release/app-release.apk ../starship-release.apk
        
    - name: Upload Debug APK
      if: matrix.build-type == 'debug'
      uses: actions/upload-artifact@v4
      with:
        name: starship-debug-apk
        path: starship-debug.apk
        retention-days: 7
        
    - name: Upload Release APK
      if: matrix.build-type == 'release'
      uses: actions/upload-artifact@v4
      with:
        name: starship-release-apk
        path: starship-release.apk
        retention-days: 30
        

        
