name: flutter-windows-x64

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

  workflow_dispatch:

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

jobs:
  asr:
    name: asr ${{ matrix.index }}/${{ matrix.total }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [windows-latest]
        total: ["3"]
        index: ["0", "1", "2"]

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

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

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

      - 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
          dart --version
          flutter doctor

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

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

          ./generate-streaming-asr.py --total $total --index $index

          chmod +x *.sh
          ./build-windows-streaming-asr.sh
          cd ../../
          ls -lh *.tar.bz2

      - 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/asr/windows/$SHERPA_ONNX_VERSION
            mkdir -p $dst
            cp -v ../*.tar.bz2 $dst

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

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

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

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

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

      - 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
          dart --version
          flutter doctor

      - 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-windows-tts.sh
          cd ../../
          ls -lh *.tar.bz2

      - 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/windows/$SHERPA_ONNX_VERSION
            mkdir -p $dst
            cp -v ../*.tar.bz2 $dst

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