name: wasm-simd

on:
  push:
    branches:
      - master
    paths:
      - '.github/workflows/build-wasm-simd.yaml'
      - 'CMakeLists.txt'
      - 'cmake/**'
      - 'sherpa-ncnn/csrc/*'
      - 'wasm/*'
  pull_request:
    branches:
      - master
    paths:
      - '.github/workflows/build-wasm-simd.yaml'
      - 'CMakeLists.txt'
      - 'cmake/**'
      - 'sherpa-ncnn/csrc/*'
      - 'wasm/*'

  release:
    types:
      - published

  workflow_dispatch:
    inputs:
      release:
        description: "Whether to release"
        type: boolean

env:
  RELEASE:
    |- # Release if there is a release tag name or a release flag in workflow_dispatch
    ${{ github.event.release.tag_name != '' || github.event.inputs.release == 'true' }}

concurrency:
  group: build-wasm-simd-${{ github.ref }}
  cancel-in-progress: true

jobs:
  wasm-simd:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest]

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Install emsdk
        uses: mymindstorm/setup-emsdk@v14

      - name: View emsdk version
        shell: bash
        run: |
          emcc -v
          echo "--------------------"
          emcc --check

      - name: Download model files
        shell: bash
        run: |
          cd wasm/assets
          ls -lh
          echo "----------"
          wget -q https://github.com/k2-fsa/sherpa-ncnn/releases/download/models/sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2
          tar xvf sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2
          mv -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13/*pnnx.ncnn.param .
          mv -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13/*pnnx.ncnn.bin .
          mv -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13/tokens.txt .

          rm -rf sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13
          rm -v sherpa-ncnn-streaming-zipformer-bilingual-zh-en-2023-02-13.tar.bz2

          ls -lh

      - name: Build sherpa-ncnn for WebAssembly
        shell: bash
        run: |
          ./build-wasm-simd.sh

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

          mv build-wasm-simd/install/bin/wasm sherpa-ncnn-wasm-simd-${SHERPA_NCNN_VERSION}
          ls -lh sherpa-ncnn-wasm-simd-${SHERPA_NCNN_VERSION}
          tar cjfv sherpa-ncnn-wasm-simd-${SHERPA_NCNN_VERSION}.tar.bz2 ./sherpa-ncnn-wasm-simd-${SHERPA_NCNN_VERSION}


      - name: Upload wasm files
        uses: actions/upload-artifact@v4
        with:
          name: sherpa-ncnn-wasm-simd
          path: ./sherpa-ncnn-wasm-simd-*.tar.bz2

      - name: Release wasm files
        if: env.RELEASE == 'true'
        uses: svenstaro/upload-release-action@v2
        with:
          file_glob: true
          overwrite: true
          file: ./sherpa-ncnn-wasm-simd*.tar.bz2
