name: Deploy All Android to github

on:
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'

## 2
jobs:
  ## 3
  build:
    runs-on: ubuntu-latest
    steps:
      # 1
      - uses: actions/checkout@v4
      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: 21
      # 2

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3

      - name: Accept Android licenses
        run: yes | "$ANDROID_HOME"/cmdline-tools/latest/bin/sdkmanager --licenses || true

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v4
        with:
          build-scan-publish: true
          build-scan-terms-of-use-url: "https://gradle.com/terms-of-service"
          build-scan-terms-of-use-agree: "yes"

      - name: Build  google play release variant apk
        run: ./gradlew assembleGooglePlayRelease

      - name: Sign google play APK
        uses: r0adkll/sign-android-release@v1.0.4
        with:
          releaseDirectory: app/build/outputs/apk/googlePlay/release
          signingKeyBase64: ${{ secrets.SIGNING_KEY2 }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: "34.0.0"

      - name: Remove Firebase References
        run: |
          sed -i -e '/maven(url = "https:\/\/maven.pkg.jetbrains.space/d' -e '/maven(url = "https:\/\/androidx.dev\/storage\/compose-compiler/d' settings.gradle.kts
          sed -i -e '/with(target) {/,/^        }/d' -e '/CrashlyticsExtension/d' build-logic/convention/src/main/kotlin/AndroidApplicationFirebaseConventionPlugin.kt
          sed -i -e '/libs.firebase/d' build-logic/convention/build.gradle.kts
          sed -i -e '/alias(libs.plugins.firebase./d' build.gradle.kts

      - name: Build foss reliant release variant apk
        run: ./gradlew assembleFossReliantRelease

      - name: Sign foss reliant APK
        uses: r0adkll/sign-android-release@v1.0.4
        with:
          releaseDirectory: app/build/outputs/apk/fossReliant/release
          signingKeyBase64: ${{ secrets.SIGNING_KEY2 }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: "34.0.0"

      # 4
      - name: Draft Release
        uses: ncipollo/release-action@v1.15.0
        with:
          allowUpdates: true
          artifacts: "app/build/outputs/apk/**/**/**/*.apk,app/build/outputs/mapping/**/**/*.txt"
          token: ${{ secrets.TOKEN }}
          generateReleaseNotes: true
          bodyFile: distribution/whatsnew/whatsnew-en-US
          prerelease: false
