name: Breezy Weather push CI

on:
  push:
    branches:
      - main
    tags:
      - v*

jobs:
  build:
    runs-on: 'ubuntu-24.04'

    steps:
      - name: Checkout
        uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0

      - name: Set up JDK
        uses: actions/setup-java@dded0888837ed1f317902acf8a20df0ad188d165 # v5.0.0
        with:
          java-version: 17
          distribution: temurin

      - name: Set up gradle
        uses: gradle/actions/setup-gradle@4d9f0ba0025fe599b4ebab900eb7f3a1d93ef4c2 # v5.0.0

      - name: Lint
        run: ./gradlew spotlessCheck

      - name: Clean build
        run: ./gradlew clean

      - name: Assemble freenet flavor
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'breezy-weather/breezy-weather'
        run: ./gradlew assembleFreenetRelease

      - name: Sign APK (free networks)
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'breezy-weather/breezy-weather'
        uses: r0adkll/sign-android-release@f30bdd30588842ac76044ecdbd4b6d0e3e813478
        with:
          releaseDirectory: app/build/outputs/apk/freenet/release
          signingKeyBase64: ${{ secrets.SIGNING_KEY }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: '35.0.1'

      - name: Save version tag in env
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'breezy-weather/breezy-weather'
        run: |
          set -x
          echo "VERSION_TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV

      - name: Prepare APKs with checksums, archive mapping.txt file (freenet)
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'breezy-weather/breezy-weather'
        run: |
          set -e
          mv app/build/outputs/apk/freenet/release/app-freenet-universal-release-unsigned-signed.apk breezy-weather-${{ env.VERSION_TAG }}_freenet.apk
          sha256=`sha256sum breezy-weather-${{ env.VERSION_TAG }}_freenet.apk | awk '{ print $1 }'`
          echo "APK_UNIVERSAL_FREENET_SHA256=$sha256" >> $GITHUB_ENV
          tar -czvf mapping-${{ env.VERSION_TAG }}_freenet.tar.gz --directory=app/build/outputs/mapping/freenetRelease/ mapping.txt

      - name: Clean build
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'breezy-weather/breezy-weather'
        run: ./gradlew clean

      - name: Add secrets to local.properties
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'breezy-weather/breezy-weather'
        run: |
          set -e
          echo "breezy.accu.key=$ACCU_WEATHER_KEY" >> local.properties
          echo "breezy.aemet.key=$AEMET_KEY" >> local.properties
          echo "breezy.atmoaura.key=$ATMO_AURA_KEY" >> local.properties
          echo "breezy.atmofrance.key=$ATMO_FRANCE_KEY" >> local.properties
          echo "breezy.atmograndest.key=$ATMO_GRAND_EST_KEY" >> local.properties
          echo "breezy.atmohdf.key=$ATMO_HDF_KEY" >> local.properties
          echo "breezy.atmosud.key=$ATMO_SUD_KEY" >> local.properties
          echo "breezy.baiduip.key=$BAIDU_IP_LOCATION_AK" >> local.properties
          echo "breezy.bmkg.key=$BMKG_KEY" >> local.properties
          echo "breezy.cwa.key=$CWA_KEY" >> local.properties
          echo "breezy.eccc.key=$ECCC_KEY" >> local.properties
          echo "breezy.geonames.key=$GEO_NAMES_KEY" >> local.properties
          echo "breezy.metie.key=$MET_IE_KEY" >> local.properties
          echo "breezy.mf.jwtKey=$MF_WSFT_JWT_KEY" >> local.properties
          echo "breezy.mf.key=$MF_WSFT_KEY" >> local.properties
          echo "breezy.openweather.key=$OPEN_WEATHER_KEY" >> local.properties
        env:
          ACCU_WEATHER_KEY: ${{ secrets.ACCU_WEATHER_KEY }}
          AEMET_KEY: ${{ secrets.AEMET_KEY }}
          ATMO_AURA_KEY: ${{ secrets.ATMO_AURA_KEY }}
          ATMO_FRANCE_KEY: ${{ secrets.ATMO_FRANCE_KEY }}
          ATMO_GRAND_EST_KEY: ${{ secrets.ATMO_GRAND_EST_KEY }}
          ATMO_HDF_KEY: ${{ secrets.ATMO_HDF_KEY }}
          ATMO_SUD_KEY: ${{ secrets.ATMO_SUD_KEY }}
          BAIDU_IP_LOCATION_AK: ${{ secrets.BAIDU_IP_LOCATION_AK }}
          BMKG_KEY: ${{ secrets.BMKG_KEY }}
          CWA_KEY: ${{ secrets.CWA_KEY }}
          ECCC_KEY: ${{ secrets.ECCC_KEY }}
          GEO_NAMES_KEY: ${{ secrets.GEO_NAMES_KEY }}
          MET_IE_KEY: ${{ secrets.MET_IE_KEY }}
          MF_WSFT_JWT_KEY: ${{ secrets.MF_WSFT_JWT_KEY }}
          MF_WSFT_KEY: ${{ secrets.MF_WSFT_KEY }}
          OPEN_WEATHER_KEY: ${{ secrets.OPEN_WEATHER_KEY }}

      - name: Assemble basic flavor
        run: ./gradlew assembleBasicRelease

      - name: Delete local.properties
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'breezy-weather/breezy-weather'
        run: |
          set -e
          rm local.properties

      - name: Sign APK (basic)
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'breezy-weather/breezy-weather'
        uses: r0adkll/sign-android-release@f30bdd30588842ac76044ecdbd4b6d0e3e813478
        with:
          releaseDirectory: app/build/outputs/apk/basic/release
          signingKeyBase64: ${{ secrets.SIGNING_KEY }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: '35.0.1'

      - name: Prepare APKs with checksums, archive mapping.txt file (basic)
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'breezy-weather/breezy-weather'
        run: |
          set -e
          mv app/build/outputs/apk/basic/release/app-basic-universal-release-unsigned-signed.apk breezy-weather-${{ env.VERSION_TAG }}_standard.apk
          sha256=`sha256sum breezy-weather-${{ env.VERSION_TAG }}_standard.apk | awk '{ print $1 }'`
          echo "APK_UNIVERSAL_SHA256=$sha256" >> $GITHUB_ENV
          mv app/build/outputs/apk/basic/release/app-basic-arm64-v8a-release-unsigned-signed.apk breezy-weather-arm64-v8a-${{ env.VERSION_TAG }}_standard.apk
          sha256=`sha256sum breezy-weather-arm64-v8a-${{ env.VERSION_TAG }}_standard.apk | awk '{ print $1 }'`
          echo "APK_ARM64_V8A_SHA256=$sha256" >> $GITHUB_ENV
          mv app/build/outputs/apk/basic/release/app-basic-armeabi-v7a-release-unsigned-signed.apk breezy-weather-armeabi-v7a-${{ env.VERSION_TAG }}_standard.apk
          sha256=`sha256sum breezy-weather-armeabi-v7a-${{ env.VERSION_TAG }}_standard.apk | awk '{ print $1 }'`
          echo "APK_ARMEABI_V7A_SHA256=$sha256" >> $GITHUB_ENV
          mv app/build/outputs/apk/basic/release/app-basic-x86-release-unsigned-signed.apk breezy-weather-x86-${{ env.VERSION_TAG }}_standard.apk
          sha256=`sha256sum breezy-weather-x86-${{ env.VERSION_TAG }}_standard.apk | awk '{ print $1 }'`
          echo "APK_X86_SHA256=$sha256" >> $GITHUB_ENV
          mv app/build/outputs/apk/basic/release/app-basic-x86_64-release-unsigned-signed.apk breezy-weather-x86_64-${{ env.VERSION_TAG }}_standard.apk
          sha256=`sha256sum breezy-weather-x86_64-${{ env.VERSION_TAG }}_standard.apk | awk '{ print $1 }'`
          echo "APK_X86_64_SHA256=$sha256" >> $GITHUB_ENV
          tar -czvf mapping-${{ env.VERSION_TAG }}_standard.tar.gz --directory=app/build/outputs/mapping/basicRelease/ mapping.txt

      - name: Create release
        if: startsWith(github.ref, 'refs/tags/') && github.repository == 'breezy-weather/breezy-weather'
        uses: softprops/action-gh-release@5be0e66d93ac7ed76da52eca8bb058f665c3a5fe # v2.4.2
        with:
          tag_name: ${{ env.VERSION_TAG }}
          name: Breezy Weather ${{ env.VERSION_TAG }}
          body: |
            **If you don’t know what to do, download and install `breezy-weather-${{ env.VERSION_TAG }}_standard.apk`, or check [INSTALL instructions](https://github.com/breezy-weather/breezy-weather/blob/main/INSTALL.md) for more details.**

            ---

            ### Checksums
            | Flavor   | Variant     | SHA-256                                |
            |----------|-------------|----------------------------------------|
            | Standard | Universal   | ${{ env.APK_UNIVERSAL_SHA256 }}        |
            | Standard | arm64-v8a   | ${{ env.APK_ARM64_V8A_SHA256 }}        |
            | Standard | armeabi-v7a | ${{ env.APK_ARMEABI_V7A_SHA256 }}      |
            | Standard | x86         | ${{ env.APK_X86_SHA256 }}              |
            | Standard | x86_64      | ${{ env.APK_X86_64_SHA256 }}           |
            | Freenet  | Universal   | ${{ env.APK_UNIVERSAL_FREENET_SHA256 }} |
          files: |
            breezy-weather-${{ env.VERSION_TAG }}_standard.apk
            breezy-weather-arm64-v8a-${{ env.VERSION_TAG }}_standard.apk
            breezy-weather-armeabi-v7a-${{ env.VERSION_TAG }}_standard.apk
            breezy-weather-x86-${{ env.VERSION_TAG }}_standard.apk
            breezy-weather-x86_64-${{ env.VERSION_TAG }}_standard.apk
            mapping-${{ env.VERSION_TAG }}_standard.tar.gz
            breezy-weather-${{ env.VERSION_TAG }}_freenet.apk
            mapping-${{ env.VERSION_TAG }}_freenet.tar.gz
          draft: true
          prerelease: false
