name: windows-x64-jni

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

  workflow_dispatch:

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

jobs:
  windows_x64_jni:
    name: windows x64 jni
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [windows-2019]

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

      - uses: actions/setup-java@v4
        with:
          distribution: 'temurin' # See 'Supported distributions' for available options
          java-version: '21'

      - name: Configure CMake
        shell: bash
        run: |
          mkdir build
          cd build
          cmake \
            -A x64 \
            -DBUILD_SHARED_LIBS=ON \
            -D SHERPA_ONNX_ENABLE_JNI=ON \
            -DCMAKE_INSTALL_PREFIX=./install \
            -DCMAKE_BUILD_TYPE=Release \
            -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
            -DBUILD_ESPEAK_NG_EXE=OFF \
            -DSHERPA_ONNX_BUILD_C_API_EXAMPLES=OFF  \
            -DSHERPA_ONNX_ENABLE_BINARY=ON \
            ..

      - name: Build sherpa-onnx for windows
        shell: bash
        run: |
          cd build
          cmake --build . --config Release -- -m:2
          cmake --build . --config Release --target install -- -m:2

          ls -lh ./bin/Release/sherpa-onnx.exe
          rm -rf install/share
          rm -rf install/lib/share
          rm -rf install/lib/pkgconfig
          rm -rf install/lib/sherpa-onnx-c-api.*

      - uses: actions/upload-artifact@v4
        with:
          name: release-jni-windows-x64
          path: build/install/*

      - name: Copy files
        shell: bash
        run: |
          SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt  | cut -d " " -f 2  | cut -d '"' -f 2)

          dst=sherpa-onnx-${SHERPA_ONNX_VERSION}-win-x64-jni
          mkdir -p $dst

          cp -a build/install/bin $dst/
          cp -a build/install/lib $dst/
          cp -a build/install/include $dst/

          tar cjvf ${dst}.tar.bz2 $dst

      # https://huggingface.co/docs/hub/spaces-github-actions
      - name: Publish to huggingface
        if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
        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_CLONE_PROTECTION_ACTIVE=false
            GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface

            cd huggingface
            mkdir -p jni

            cp -v ../sherpa-onnx-*.tar.bz2 ./jni

            git status
            git lfs track "*.bz2"

            git add .

            git commit -m "upload sherpa-onnx-${SHERPA_ONNX_VERSION}"

            git push https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs main

      - name: Release pre-compiled binaries and libs for Windows x64
        if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/')
        uses: svenstaro/upload-release-action@v2
        with:
          file_glob: true
          overwrite: true
          file: sherpa-onnx-*.tar.bz2
