name: Build App

on:
  pull_request:
  push:
    branches:
      - main
      - release

env:
  # Our build metadata
  BUILD_USER: android-builder
  BUILD_HOST: github.syncthing.net

jobs:
  build:
    name: Debug Build
    runs-on: ubuntu-latest
    container: ghcr.io/catfriend1/syncthing-android-builder
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
          fetch-depth: 0

      - name: Get app version from build.gradle.kts
        id: get_version
        run: |
          set -eu
          VERSION_MAJOR=$(grep 'set("versionMajor"' build.gradle.kts | grep -o '[0-9]\+')
          VERSION_MINOR=$(grep 'set("versionMinor"' build.gradle.kts | grep -o '[0-9]\+')
          VERSION_PATCH=$(grep 'set("versionPatch"' build.gradle.kts | grep -o '[0-9]\+')
          VERSION_WRAPPER=$(grep 'set("versionWrapper"' build.gradle.kts | grep -o '[0-9]\+')
          VERSION="$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH.$VERSION_WRAPPER"
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Get commit hash
        id: get_commit_hash
        run: |
          set -eu
          git config --system --add safe.directory '*'
          COMMIT_HASH=$(git rev-parse --short --verify HEAD)
          echo "COMMIT_HASH=$COMMIT_HASH" >> $GITHUB_ENV

      - name: setup-debug-signing
        run: |
          set -eu
          mkdir -p "/root/.android"
          if [ -n "${{ secrets.DEBUG_KEYSTORE_B64 }}" ]; then
            echo "Using TRUSTED debug.keystore for builds from branches corresponding to this repository"
            echo '${{ secrets.DEBUG_KEYSTORE_B64 }}' | base64 -d > "/root/.android/debug.keystore"
          else
            cp "scripts/debug/debug.keystore.pub" "/root/.android/debug.keystore"
            echo "Using UNTRUSTED PUBLIC debug.keystore for builds from forked repositories"
          fi

      - name: build
        run: |
          set -eu
          git config --system --add safe.directory '*'
          java -version
          ./gradlew --no-daemon buildNative lintDebug assembleDebug

      - name: prepare-artifacts
        shell: bash
        run: |
          set -eu
          mv "app/build/outputs/apk/debug/app-debug.apk" "app/build/outputs/apk/debug/com.github.catfriend1.syncthingandroid_debug_v${{ env.VERSION }}_${{ env.COMMIT_HASH }}.apk"

      - uses: actions/upload-artifact@v4
        with:
          name: com.github.catfriend1.syncthingandroid_debug_v${{ env.VERSION }}_${{ env.COMMIT_HASH }}.apk
          path: app/build/outputs/apk/debug/com.github.catfriend1.syncthingandroid_debug_v${{ env.VERSION }}_${{ env.COMMIT_HASH }}.apk

      - uses: actions/upload-artifact@v4
        with:
          name: reports
          path: |
            app/build/reports/**

      - if: false
        uses: actions/upload-artifact@v4
        with:
          name: reports-and-libs
          path: |
            app/build/reports/**
            app/src/main/jniLibs/**
