name: Android CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Set up build environment
        run: |
          sudo apt-get update
          sudo apt-get -y install ccache libcurl4-openssl-dev ninja-build

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          submodules: true
          
      - uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: 'gradle'

      - uses: actions/cache@v3
        with:
          path: ~/.ccache
          key: android-ccache-${{ github.sha }}
          restore-keys: android-ccache-

      - name: Gradle
        working-directory: shell/android-studio
        run: ./gradlew assembleRelease --parallel
        env:
          SENTRY_UPLOAD_URL: ${{ secrets.SENTRY_UPLOAD_URL }}

      - uses: actions/upload-artifact@v3
        with:
          name: flycast-release.apk
          path: shell/android-studio/flycast/build/outputs/apk/release/flycast-release.apk

      - name: Build dump_syms
        working-directory: core/deps/breakpad
        run: |
          ./configure --prefix=$PWD
          make -j2
          make install

      - name: Extract symbols
        run: |
          DUMP_SYMS=./core/deps/breakpad/bin/dump_syms
          SO_DIR=shell/android-studio/flycast/build/intermediates/merged_native_libs/release/out/lib
          for arch in arm64-v8a armeabi-v7a x86 x86_64 ; do
            $DUMP_SYMS $SO_DIR/$arch/libflycast.so > libflycast.so.sym
            BUILD_ID=`head -1 libflycast.so.sym | awk '{ print $4 }'`
            mkdir -p symbols/libflycast.so/$BUILD_ID
            mv libflycast.so.sym symbols/libflycast.so/$BUILD_ID
          done

      - name: Configure AWS Credentials
        id: aws-credentials
        uses: aws-actions/configure-aws-credentials@v2
        with:
          aws-access-key-id: AKIAJOZQS4H2PHQWYFCA
          aws-secret-access-key: ${{ secrets.S3_SECRET_KEY }}
          aws-region: us-east-2
        if: github.repository == 'flyinghead/flycast' && github.event_name == 'push'

      - name: Upload to S3
        run: aws s3 sync shell/android-studio/flycast/build/outputs/apk/release s3://flycast-builds/android/${GITHUB_REF#refs/}-$GITHUB_SHA --acl public-read --exclude='*.json' --follow-symlinks
        if: ${{ steps.aws-credentials.outputs.aws-account-id != '' }}

      - name: Setup Sentry CLI
        uses: mathieu-bour/setup-sentry-cli@v1
        env:
          SENTRY_TOKEN: ${{ secrets.SENTRY_TOKEN }}
        with:
          url: https://sentry.io
          token: ${{ env.SENTRY_TOKEN }}
          organization: flycast
          project: minidump
        if: ${{ env.SENTRY_TOKEN != '' }}
      
      - name: Upload symbols to Sentry
        run: |
          VERSION=$(git describe --tags --always)
          sentry-cli releases new "$VERSION"
          sentry-cli releases set-commits "$VERSION" --auto
          sentry-cli debug-files upload symbols
        shell: bash
        env:
          SENTRY_TOKEN: ${{ secrets.SENTRY_TOKEN }}
        if: ${{ env.SENTRY_TOKEN != '' }}
