name: 🧪 Android packages

on:
  push:
    branches:
      - master
      - release-**
  pull_request:
  release:
    types: ['published', 'released']

jobs:
  # Build Android packages
  build:
    runs-on: ubuntu-20.04
    env:
      TX_TOKEN: ${{ secrets.TX_TOKEN }}
    strategy:
      matrix:
        arch: [armv7, arm64_v8a, x86, x86_64]
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup signing key
        env:
          SIGNINGKEY: ${{ secrets.PLAYSTORE_SIGNINGKEY }}
        run: |
          echo "$SIGNINGKEY" | base64 --decode > ./keystore.p12

      - name: Setup build environment
        run: |
          echo ::group::install packages
          sudo apt update && sudo apt install -y qttools5-dev-tools qt5-default transifex-client
          echo ::endgroup::
          sed -i 's/git@github.com:/https:\/\/github.com\//' .gitmodules
          git submodule update --init --recursive
          ./scripts/ci/env_gh.sh
          pip install wheel

      - name: "🌍 Pull Translations"
        run: |
          if [[ -z "${TX_TOKEN}" ]]; then
            echo "TX_TOKEN not set, skip tx pull"
          else
            ./scripts/ci/pull_translations.sh
          fi

#      - uses: actions/cache@v2
#        with:
#          path: |
#            ~/cache/.gradle/caches
#            ~/cache/.gradle/wrapper
#          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
#          restore-keys: |
#            ${{ runner.os }}-gradle-

      - name: Free additional space
        run: |
          df -h
          rm -rf /tmp/workspace
          rm -rf /usr/share/dotnet/sdk
          sudo apt remove llvm-* ghc-* google-chrome-* dotnet-sdk-*
          dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -n 100
          du -a /usr/share | sort -n -r | head -n 10
          du -a /usr/local/share | sort -n -r | head -n 10
          df -h
          sudo apt clean
          df -h

      - name: Build
        env:
          ARCH: ${{ matrix.arch }}
          KEYNAME: qfield
          KEYPASS: ${{ secrets.KEYPASS }}
          STOREPASS: ${{ secrets.STOREPASS }}
          CACHE_DIR: ${{ env.GITHUB_WORKSPACE }}/cache
        run: |
          ./scripts/ci/docker_pull.sh
          source ./scripts/version_number.sh
          source ./scripts/ci/generate-version-details.sh
          ./scripts/build.sh
          ls $CACHE_DIR

      - name: 🍺 Deploy
        run: |
          sudo apt install -y s3cmd
          ./scripts/ci/upload_artifacts.sh
        env:
          S3CFG: ${{ secrets.S3CFG }}
          ARCH: ${{ matrix.arch }}

      - name: Upload release assets
        uses: AButler/upload-release-assets@v2.0
        if: ${{ github.event_name == 'release' }}
        with:
          files: /tmp/qfield-*.apk
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          release-tag: ${{ env.CI_TAG }}

  deploy_to_playstore:
    runs-on: ubuntu-20.04
    needs: build
    if: ${{ github.event_name == 'released' || ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Vars
        id: vars
        run: |
          ./scripts/ci/env_gh.sh

      - name: Download apks
        run: |
          wget https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-arm64_v8a.apk
          wget https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-armv7.apk
          wget https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-x86_64.apk
          wget https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-x86.apk

      - name: Upload to Google Play Store
        run: |
          pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client

          ./scripts/basic_upload_apks_service_account.py ch.opengis.${{ steps.vars.outputs.APP_PACKAGE_NAME }} beta "Update from commit ${GITHUB_SHA}" \
              ${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-arm64_v8a.apk \
              ${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-armv7.apk \
              ${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-x86_64.apk \
              ${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-x86.apk
        env:
          GOOGLE_SERVICE_ACCOUNT: ${{ secrets.GOOGLE_SERVICE_ACCOUNT }}


  comment_pr:
    runs-on: ubuntu-20.04
    needs: build
    if: ${{ github.event_name == 'pull_request' }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Vars
        id: vars
        run: |
          ./scripts/ci/env_gh.sh
      - uses: kanga333/comment-hider@master
        name: Hide outdated comments from the default github user
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          hide_user_name: github-actions[bot]
      - uses: kanga333/comment-hider@master
        name: Hide outdated comments from qfield-fairy
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          hide_user_name: qfield-fairy
      - name: Comment PR
        uses: thollander/actions-comment-pull-request@main
        with:
          GITHUB_TOKEN: ${{ secrets.FAIRY_TOKEN }}
          message: |
            🎉 Ta-daaa, freshly created APKs are available for ${{ github.event.pull_request.head.sha }}:
              - [**arm64_v8a**](https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-arm64_v8a.apk)

            Other architectures: [armv7](https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-armv7.apk), [x86_64](https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-x86_64.apk), [x86](https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-x86.apk)

  comment_commit:
    runs-on: ubuntu-20.04
    needs: build
    if: ${{ github.event_name == 'push' }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Vars
        id: vars
        run: |
          ./scripts/ci/env_gh.sh
      - name: Comment commit
        uses: peter-evans/commit-comment@v1
        with:
          token: ${{ secrets.FAIRY_TOKEN }}
          body: |
            🎉 Ta-daaa, freshly created APKs are available:
              - [**arm64_v8a**](https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-arm64_v8a.apk)

            Other architectures: [armv7](https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-armv7.apk), [x86_64](https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-x86_64.apk), [x86](https://sos-ch-dk-2.exo.io/qfieldapks/ci-builds/${{ steps.vars.outputs.CI_PACKAGE_FILE_BASENAME }}-x86.apk)
