name: flutter-android

on:
  push:
    branches:
      - flutter
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+*'

  workflow_dispatch:

concurrency:
  group: flutter-android-${{ github.ref }}
  cancel-in-progress: true

jobs:
  tts:
    name: tts ${{ matrix.index }}/${{ matrix.total }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest]
        total: ["15"]
        index: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"]

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Display NDK HOME
        shell: bash
        run: |
          echo "ANDROID_NDK_LATEST_HOME: ${ANDROID_NDK_LATEST_HOME}"
          ls -lh ${ANDROID_NDK_LATEST_HOME}

      - name: Setup build tool version variable
        shell: bash
        run: |
          echo "---"
          ls -lh /usr/local/lib/android/
          echo "---"

          ls -lh /usr/local/lib/android/sdk
          echo "---"

          ls -lh /usr/local/lib/android/sdk/build-tools
          echo "---"

          BUILD_TOOL_VERSION=$(ls /usr/local/lib/android/sdk/build-tools/ | tail -n 1)
          echo "BUILD_TOOL_VERSION=$BUILD_TOOL_VERSION" >> $GITHUB_ENV
          echo "Last build tool version is: $BUILD_TOOL_VERSION"

      - name: Install Python dependencies
        shell: bash
        run: |
          python3 -m pip install --upgrade pip jinja2 iso639-lang

      - name: Install deps
        shell: bash
        run: |
          sudo apt-get update -y
          sudo apt-get install -y build-essential jq git cmake
          sudo apt-get install -y curl

      - name: Setup Flutter SDK
        uses: flutter-actions/setup-flutter@v3
        with:
          channel: stable
          version: latest

      - name: Install ninja
        shell: bash
        run: |
          sudo apt-get install -y ninja-build

      - name: Display ninja version
        shell: bash
        run: |
          ninja --version
          ninja --help || true
          which ninja

      - name: Display PWD
        shell: bash
        run: |
          echo "pwd: $PWD"
          ls -lh

      - name: Display machine info
        shell: bash
        run: |
          uname -a

      - name: Display flutter info
        shell: bash
        run: |
          which flutter
          which dart

          flutter --version

          git config --global --add safe.directory /__t/flutter-Linux-*/flutter || true

          flutter --version

          dart --version
          flutter doctor

      - name: Install libgtk-3-dev
        shell: bash
        run: |
          sudo apt install -y libgtk-3-dev tree clang pkg-config

      - name: Display flutter info (2)
        shell: bash
        run: |
          which flutter
          which dart

          flutter --version
          dart --version
          flutter doctor

          cd ..

      - name: Build flutter
        shell: bash
        run: |
          cd scripts/flutter

          total=${{ matrix.total }}
          index=${{ matrix.index }}

          ./generate-tts.py --total $total --index $index

          chmod +x *.sh
          ./build-android-tts.sh

          cd ../../

          ls -lh *.apk

      - name: Display generated files
        shell: bash
        run: |
          ls -lh *.apk

          mkdir apks

          mv -v *.apk ./apks

      # https://github.com/marketplace/actions/sign-android-release
      - uses: r0adkll/sign-android-release@v1
        name: Sign app APK
        with:
          releaseDirectory: ./apks
          signingKeyBase64: ${{ secrets.ANDROID_SIGNING_KEY }}
          alias: ${{ secrets.ANDROID_SIGNING_KEY_ALIAS }}
          keyStorePassword: ${{ secrets.ANDROID_SIGNING_KEY_STORE_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: ${{ env.BUILD_TOOL_VERSION }}

      - name: Display APK after signing
        shell: bash
        run: |
          ls -lh ./apks/
          du -h -d1 .

      - name: Rename APK after signing
        shell: bash
        run: |
          cd apks
          rm -fv signingKey.jks
          rm -fv *.apk.idsig
          rm -fv *-aligned.apk

          all_apks=$(ls -1 *-signed.apk)
          echo "----"
          echo $all_apks
          echo "----"
          for apk in ${all_apks[@]}; do
            n=$(echo $apk | sed -e s/-signed//)
            mv -v $apk $n
          done

          cd ..

          ls -lh ./apks/
          du -h -d1 .

      - name: Display APK after rename
        shell: bash
        run: |
          ls -lh ./apks/
          du -h -d1 .

      - name: Publish to huggingface
        if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa' || github.repository_owner == 'csu-fangjun') && ((github.event_name == 'push' || github.event_name == 'workflow_dispatch') || contains(github.ref, 'refs/tags/'))
        env:
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
        uses: nick-fields/retry@v3
        with:
          max_attempts: 20
          timeout_seconds: 200
          shell: bash
          command: |
            git config --global user.email "csukuangfj@gmail.com"
            git config --global user.name "Fangjun Kuang"

            rm -rf huggingface
            export GIT_LFS_SKIP_SMUDGE=1
            export GIT_CLONE_PROTECTION_ACTIVE=false

            SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt  | cut -d " " -f 2  | cut -d '"' -f 2)
            echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION"

            git clone https://huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface
            cd huggingface
            git fetch
            git pull
            git merge -m "merge remote" --ff origin main

            dst=flutter/tts/android/$SHERPA_ONNX_VERSION
            mkdir -p $dst

            cp -v ../apks/*.apk $dst

            git status
            git lfs track "*.apk"
            git add .
            git commit -m "add more files"
            git push https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-flutter main
