name: Release Builder
on:
  push:
   branches: 
    - 'release'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
 
jobs:
  build:
    name: Build release app
    runs-on: ubuntu-latest

    steps:
      - name: Clone repo
        uses: actions/checkout@v3

      - name: Validate Gradle Wrapper
        uses: gradle/wrapper-validation-action@v1

      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: 11
          distribution: adopt

      # SY <--
      - name: Write google-services.json
        uses: DamianReeves/write-file-action@v1.2
        with:
          path: app/google-services.json
          contents: ${{ secrets.GOOGLE_SERVICES_TEXT }}
          write-mode: overwrite
      # SY -->

      - name: Build app and run unit tests
        uses: gradle/gradle-command-action@v2
        with:
          arguments: lintKotlin assembleStandardRelease testStandardReleaseUnitTest --stacktrace

      - name: Sign APK
        uses: r0adkll/sign-android-release@v1
        with:
          releaseDirectory: app/build/outputs/apk/standard/release
          signingKeyBase64: ${{ secrets.SIGNING_KEY }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}

      - name: Clean up build artifacts
        run: |
          set -e

          mv app/build/outputs/apk/standard/release/app-standard-universal-release-unsigned-signed.apk TachiyomiSY.apk
          sha=`sha256sum TachiyomiSY.apk | awk '{ print $1 }'`
          echo "APK_UNIVERSAL_SHA=$sha" >> $GITHUB_ENV

          cp app/build/outputs/apk/standard/release/app-standard-arm64-v8a-release-unsigned-signed.apk TachiyomiSY-arm64-v8a.apk
          sha=`sha256sum TachiyomiSY-arm64-v8a.apk | awk '{ print $1 }'`
          echo "APK_ARM64_V8A_SHA=$sha" >> $GITHUB_ENV

          cp app/build/outputs/apk/standard/release/app-standard-armeabi-v7a-release-unsigned-signed.apk TachiyomiSY-armeabi-v7a.apk
          sha=`sha256sum TachiyomiSY-armeabi-v7a.apk | awk '{ print $1 }'`
          echo "APK_ARMEABI_V7A_SHA=$sha" >> $GITHUB_ENV

          cp app/build/outputs/apk/standard/release/app-standard-x86-release-unsigned-signed.apk TachiyomiSY-x86.apk
          sha=`sha256sum TachiyomiSY-x86.apk | awk '{ print $1 }'`
          echo "APK_X86_SHA=$sha" >> $GITHUB_ENV
          
          cp app/build/outputs/apk/standard/release/app-standard-x86_64-release-unsigned-signed.apk TachiyomiSY-x86_64.apk
          sha=`sha256sum TachiyomiSY-x86_64.apk | awk '{ print $1 }'`
          echo "APK_X86_64_SHA=$sha" >> $GITHUB_ENV

      - name: Create release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ github.run_number }}
          name: TachiyomiSY
          body: |
            ---

            ### Checksums

            | Variant | SHA-256 |
            | ------- | ------- |
            | Universal | ${{ env.APK_UNIVERSAL_SHA }} |
            | arm64-v8a | ${{ env.APK_ARM64_V8A_SHA }} |
            | armeabi-v7a | ${{ env.APK_ARMEABI_V7A_SHA }} |
            | x86 | ${{ env.APK_X86_SHA }} |
            | x86_64 | ${{ env.APK_X86_64_SHA }} |
          files: |
            TachiyomiSY.apk
            TachiyomiSY-arm64-v8a.apk
            TachiyomiSY-armeabi-v7a.apk
            TachiyomiSY-x86.apk
            TachiyomiSY-x86_64.apk
          draft: true
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
