name: CI

on:
  push:
    branches:
      - iceraven

jobs:
  release-automation:
    name: Build App
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: 'true'
          fetch-depth: 0

      - name: Setup Java
        uses: actions/setup-java@v3
        with:
          java-version: 17
          distribution: temurin

      - name: Install Android SDK with pieces Gradle skips
        run: ./automation/iceraven/install-sdk.sh

      - name: Inspect memory
        run: free -h

      - name: Create version name
        run: echo "VERSION_NAME=$(git describe --tags HEAD)" >> $GITHUB_ENV

      - name: Patch on the fly
        run: |
          sed -i 's#\.\./version.txt#\./version.txt#g' android-components/plugins/config/src/main/java/ConfigPlugin.kt
          ./automation/iceraven/patch_android_components.sh

      - name: Relpace strings
        run: |
          sed -i 's/Firefox/Iceraven/g' app/src/*/res/values*/*strings.xml
          sed -i '/about_content/s/Mozilla/@forkmaintainers/' app/src/*/res/values*/*strings.xml

      - name: Build forkRelease variant of app
        uses: gradle/gradle-build-action@v2
        env:
          # Try to stop the daemon from magically vanishing by adding random memory-related arguments.
          # See <https://stackoverflow.com/a/70010526> and <https://stackoverflow.com/a/70756876>
          # The runner seems to have ~6 gigs of memory, so we make sure to stay under that.
          # We have Java 11 so we don't have a perm size anymore.
          GRADLE_OPTS: -Dorg.gradle.jvmargs="-XX:MaxMetaspaceSize=2g -Xms1g -Xmx3g -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dev/stderr"
        with:
          gradle-home-cache-cleanup: true
          gradle-executable: /usr/bin/time
          arguments: -v ./gradlew app:assemblefenixForkRelease -x lintVitalFenixForkRelease -PversionName=${{ env.VERSION_NAME }} --stacktrace

      - name: Setup build tool version variable
        shell: bash
        run: |
          BUILD_TOOL_VERSION=$(ls /usr/local/lib/android/sdk/build-tools/ | tail -n 1)
          echo "BUILD_TOOL_VERSION=$BUILD_TOOL_VERSION" >> $GITHUB_ENV

      - name: Create signed APKs
        uses: abhijitvalluri/sign-apks@v0.8
        with:
          releaseDirectory: app/build/outputs/apk/fenix/forkRelease/
          signingKeyBase64: ${{ secrets.DEBUG_SIGNING_KEY }}
          alias: ${{ secrets.DEBUG_ALIAS }}
          keyStorePassword: ${{ secrets.DEBUG_KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.DEBUG_KEY_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: ${{ env.BUILD_TOOL_VERSION }}

      - name: Upload arm64 apk
        uses: actions/upload-artifact@v3
        with:
          path: app/build/outputs/apk/fenix/forkRelease/app-fenix-arm64-v8a-forkRelease.apk
          name: ${{ env.VERSION_NAME }}-browser-arm64-v8a-forkRelease.apk

      - name: Upload armeabi apk
        uses: actions/upload-artifact@v3
        with:
          path: app/build/outputs/apk/fenix/forkRelease/app-fenix-armeabi-v7a-forkRelease.apk
          name: ${{ env.VERSION_NAME }}-browser-armeabi-v7a-forkRelease.apk

      - name: Upload x86 apk
        uses: actions/upload-artifact@v3
        with:
          path: app/build/outputs/apk/fenix/forkRelease/app-fenix-x86-forkRelease.apk
          name: ${{ env.VERSION_NAME }}-browser-x86-forkRelease.apk

      - name: Upload x86_64 apk
        uses: actions/upload-artifact@v3
        with:
          path: app/build/outputs/apk/fenix/forkRelease/app-fenix-x86_64-forkRelease.apk
          name: ${{ env.VERSION_NAME }}-browser-x86_64-forkRelease.apk
