name: Build & test node-ffmpeg

on:
  workflow_call:
    inputs:
      platform:
        type: string
        required: true
      debug:
        type: boolean
        default: false
        description: Debug build
      codecov:
        type: boolean
        default: false
        description: codecov build
      asan:
        type: boolean
        default: false
        description: ASAN build
      publish:
        type: string
        default: ''
        description: publish the binaries to the current release


jobs:
  build:
    name: Build & test on ${{ inputs.platform }}
    runs-on: ${{ inputs.platform }}

    steps:
    - uses: actions/checkout@v5
      with:
        fetch-depth: 0
        submodules: true
    - name: Use Node.js 18
      uses: actions/setup-node@v4
      with:
        node-version: 18
    - uses: actions/setup-python@v5
      with:
        python-version: "3.11"
    - name: (if on Ubuntu) install fonts
      run: sudo apt-get install -y fonts-ubuntu || true
      if: ${{ contains(inputs.platform, 'ubuntu') }}

    - name: Enable verbose output in debug mode
      run: |
        echo "TSC_DEBUG=1" >> $GITHUB_ENV
        #echo "DEBUG_FFMPEG=1" >> $GITHUB_ENV
        #echo "DEBUG_ALL=1" >> $GITHUB_ENV
      shell: bash
      if: inputs.debug

    - run: npm install magickwand.js
    - run: npm install --ignore-scripts
    - run: npx xpm install

    - name: Get conan home
      shell: bash
      id: conan_home
      run: |
        npx xpm run -q conan -- version
        echo path=`npx xpm run -q conan -- config home` >> $GITHUB_OUTPUT
    - name: Cache conan artifacts
      id: conan-artifacts
      uses: actions/cache@v4
      with:
        path: ${{ steps.conan_home.outputs.path }}
        key: conan-${{ inputs.platform }}-${{ inputs.debug && 'Debug' || 'Release' }}${{ inputs.codecov && 'codecov' || '' }}${{ inputs.asan && 'asan' || '' }}

    - run: npm install --build-from-source --verbose --foreground-scripts
      env:
        PKG_CONFIG_PATH:

    - run: npx xpm run prepare --config ${{ inputs.debug && 'Debug' || 'Release' }}
      if: inputs.asan || inputs.codecov || inputs.debug
      env:
        PKG_CONFIG_PATH:

    - name: (ASAN) build and run the TS generator
      run: npx xpm run build --config ${{ inputs.debug && 'Debug' || 'Release' }}
      if: inputs.asan
    - name: (ASAN) configure
      run: npx xpm run configure -- -Db_sanitize=address
      if: inputs.asan
    - name: (codecov) configure
      run: npx xpm run configure -- -Db_coverage=true -Dbuildtype=debugoptimized '-Dcpp_args="--coverage -ftest-coverage"'
      if: inputs.codecov

    - run: npx xpm run build --config ${{ inputs.debug && 'Debug' || 'Release' }}
      if: inputs.asan || inputs.codecov || inputs.debug

    - run: npm test
      if: (!inputs.asan) && (!inputs.codecov)

    - name: (ASAN) Find library
      id: asan-lib
      run: echo LD_PRELOAD=$(gcc -print-file-name=libasan.so) >> $GITHUB_OUTPUT
      if: inputs.asan
    - name: (ASAN) npm test
      run: node node_modules/mocha/lib/cli/cli.js
      if: inputs.asan
      env:
        LSAN_OPTIONS: suppressions=${{ github.workspace }}/test/napi-leaks-suppression.txt
        LD_PRELOAD: ${{ steps.asan-lib.outputs.LD_PRELOAD }}

    - name: (codecov) npm test
      run: |
        npx c8 npm test
        mkdir -p coverage && cd coverage && gcov -o ../build/node-ffmpeg.node.p/* ../src/binding/* && cd ..
        npx c8 report --reporter=text-lcov > coverage/tests.lcov
      if: inputs.codecov

    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v5
      if: inputs.codecov
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        directory: ${{ github.workspace }}/coverage

    - name: Upload artifact ${{ inputs.publish }}
      if: inputs.publish
      uses: actions/upload-artifact@v4
      with:
        name: ${{ inputs.publish }}
        path: lib/binding/*
