name: Build Shamrock APK

on:
  workflow_dispatch:
  push:
    branches: [ master ]
    paths-ignore:
      - '**.md'
      - '**.txt'
      - '.github/**'
      - '.idea/**'
      - '!.github/workflows/**'

jobs:
  build:
    name: Build APK
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v3
        with:
          distribution: "temurin"
          java-version: 17
          cache: 'gradle'

      - name: Setup cmake
        run: |
          echo "y" | sudo ${ANDROID_HOME}/tools/bin/sdkmanager --install "cmake;3.22.1" --sdk_root=${ANDROID_SDK_ROOT} &> /dev/null
          echo "sdk.dir=${ANDROID_HOME}" > local.properties

      - name: Setup Gradle
        uses: gradle/gradle-build-action@v2.9.0

      - name: Build with Gradle
        run: |
          echo ${{ secrets.SIGN_KEYSTORE_BASE64 }} | base64 -d > keystore.jks
          chmod +x ./gradlew
          ./gradlew :app:assembleRelease --build-cache --parallel --daemon --warning-mode all --stacktrace
          echo "APK_FILE_ALL=$(find app/build/outputs/apk/app/release -name '*.apk')" >> $GITHUB_ENV
          echo "APK_FILE_ARM64=$(find app/build/outputs/apk/arm64/release -name '*.apk')" >> $GITHUB_ENV
          echo "APK_FILE_X86_64=$(find app/build/outputs/apk/x64/release -name '*.apk')" >> $GITHUB_ENV
        env:
          KEYSTORE_PATH: "../keystore.jks"
          KEYSTORE_PASSWORD: ${{ secrets.SIGN_KEYSTORE_PASSWORD }}
          KEY_ALIAS: ${{ secrets.SIGN_ALIAS }}
          KEY_PASSWORD: ${{ secrets.SIGN_KEY_PASSWORD }}

      - name: Install aapt
        run: sudo apt-get update && sudo apt-get install -y aapt

      - name: Set Shamrock Version
        run: |
          apk_file=${{ env.APK_FILE_ALL }}
          apk_dump=$(aapt dump badging "$apk_file")
          version_name=$(sed -n "s/.*versionName='\([^']*\)'.*/\1/p" <<< "$apk_dump")
          echo "SHAMROCK_VERSION=$version_name" >> $GITHUB_ENV

      - name: Show Artifacts SHA256
        run: |
          echo "### Build Success :rocket:" >> $GITHUB_STEP_SUMMARY
          echo "|ABI|SHA256|" >> $GITHUB_STEP_SUMMARY
          echo "|:--------:|:----------|" >> $GITHUB_STEP_SUMMARY
          all=($(sha256sum ${{ env.APK_FILE_ALL }}))
          echo "|all|$all" >> $GITHUB_STEP_SUMMARY
          arm64=($(sha256sum ${{ env.APK_FILE_ARM64 }}))
          echo "|arm64|$arm64" >> $GITHUB_STEP_SUMMARY
          x86_64=($(sha256sum ${{ env.APK_FILE_X86_64 }}))
          echo "|x86_64|$x86_64" >> $GITHUB_STEP_SUMMARY

      - name: Upload ALL APK RELEASE
        uses: actions/upload-artifact@v3
        with:
          name: Shamrock-v${{ env.SHAMROCK_VERSION }}-all
          path: ${{ env.APK_FILE_ALL }}

      - name: Upload ARM64 APK RELEASE
        uses: actions/upload-artifact@v3
        with:
          name: Shamrock-v${{ env.SHAMROCK_VERSION }}-arm64
          path: ${{ env.APK_FILE_ARM64 }}

      - name: Upload X86_64 APK RELEASE
        uses: actions/upload-artifact@v3
        with:
          name: Shamrock-v${{ env.SHAMROCK_VERSION }}-x86_64
          path: ${{ env.APK_FILE_X86_64 }}
