name: Build OpenCV.js

on:
  # push:
  #   branches:
  #     - build-opencv
  #     - main
  workflow_dispatch:
    inputs: {}

jobs:
  build-opencv:
    runs-on: ubuntu-latest
    steps:
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libv4l-dev

      - name: Checkout emsdk
        uses: actions/checkout@v4
        with:
          repository: emscripten-core/emsdk
          path: emsdk
      - name: Install an emsdk version
        run: |
          cd emsdk
          ./emsdk install 2.0.10
          ./emsdk activate 2.0.10

      - name: Checkout opencv
        uses: actions/checkout@v4
        with:
          repository: opencv/opencv
          ref: 4.10.0
          path: opencv
      - name: Build opencv.js
        run: |
          source emsdk/emsdk_env.sh
          emcmake python opencv/platforms/js/build_js.py build_js --build_flags="-s WASM_ASYNC_COMPILATION=0"

      - name: Upload opencv_js
        uses: actions/upload-artifact@v4
        with:
          name: opencv.js
          path: build_js/bin/opencv.js
          retention-days: 30

      # - name: Check out repository code
      #   uses: actions/checkout@v4
