name: build
on:
  workflow_dispatch:
    inputs:
      build_type:
        type: choice
        description: "Build type"
        required: true
        default: debug
        options:
          - debug
          - prerelease
          - nightly
          - release
    secrets:
      SIGNING_KEY_ALIAS:
        required: false
      SIGNING_KEY_PASSWORD:
        required: false
      SIGNING_STORE_PASSWORD:
        required: false
      SERVICE_ACCOUNT_JSON:
        required: false
      KEYSTORE:
        required: false
  workflow_call:
    inputs:
      build_type:
        type: string
        description: "Build type"
        required: true
        default: debug
    secrets:
      SIGNING_KEY_ALIAS:
        required: false
      SIGNING_KEY_PASSWORD:
        required: false
      SIGNING_STORE_PASSWORD:
        required: false
      SERVICE_ACCOUNT_JSON:
        required: false
      KEYSTORE:
        required: false
env:
  UPLOAD_DIR_ANDROID: android_artifacts

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
      SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
      SIGNING_STORE_PASSWORD: ${{ secrets.SIGNING_STORE_PASSWORD }}
      KEY_STORE_FILE: 'android_keystore.jks'
      KEY_STORE_LOCATION: ${{ github.workspace }}/app/keystore/
    outputs:
      UPLOAD_DIR_ANDROID: ${{ env.UPLOAD_DIR_ANDROID }}
    steps:
      - uses: actions/checkout@v4
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: gradle
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      # Here we need to decode keystore.jks from base64 string and place it
      # in the folder specified in the release signing configuration
      - name: Decode Keystore
        id: decode_keystore
        uses: timheuer/base64-to-file@v1.2
        with:
          fileName: ${{ env.KEY_STORE_FILE }}
          fileDir: ${{ env.KEY_STORE_LOCATION }}
          encodedString: ${{ secrets.KEYSTORE }}

      # create keystore path for gradle to read
      - name: Create keystore path env var
        if: ${{ inputs.build_type != 'debug' }}
        run: |
          store_path=${{ env.KEY_STORE_LOCATION }}${{ env.KEY_STORE_FILE }}
          echo "KEY_STORE_PATH=$store_path" >> $GITHUB_ENV

      - name: Create service_account.json
        if: ${{ inputs.build_type != 'debug' }}
        id: createServiceAccount
        run: echo '${{ secrets.ANDROID_SERVICE_ACCOUNT_JSON }}' > service_account.json

      - name: Build Fdroid Release APK
        if: ${{ inputs.build_type == 'release' }}
        run: ./gradlew :app:assembleFdroidRelease --info

      - name: Build Fdroid Prerelease APK
        if: ${{ inputs.build_type == 'prerelease' }}
        run: ./gradlew :app:assembleFdroidPrerelease --info

      - name: Build Fdroid Nightly APK
        if: ${{ inputs.build_type == 'nightly' }}
        run: ./gradlew :app:assembleFdroidNightly --info

      - name: Build Debug APK
        if: ${{ inputs.build_type == 'debug' }}
        run: ./gradlew :app:assembleFdroidDebug --stacktrace

      # bump versionCode for nightly and prerelease builds
      - name: Commit and push versionCode changes
        if: ${{ inputs.build_type == 'nightly' || inputs.build_type == 'prerelease' }}
        run: |
          git config --global user.name 'GitHub Actions'
          git config --global user.email 'actions@github.com'
          git add versionCode.txt
          git commit -m "Automated build update"

      - name: Get release apk path
        id: apk-path
        run: echo "path=$(find . -regex '^.*/build/outputs/apk/fdroid/${{ inputs.build_type }}/.*\.apk$' -type f | head -1 | tail -c+2)" >> $GITHUB_OUTPUT

      - name: Upload release apk
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.UPLOAD_DIR_ANDROID }}
          path: ${{github.workspace}}/${{ steps.apk-path.outputs.path }}
          retention-days: 1
