name: BE20_API CI Ubuntu and Mac
on:
  pull_request:
    branches: [ main ]
  push:
    branches: [ main ]


jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: ['macos-latest','ubuntu-latest']

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive

      # ----------------------------
      # MacOS
      # ----------------------------
      - name: Install MacOS dependencies and run bootstrap
        if: startsWith( matrix.os, 'macos')
        run: |
          brew install autoconf automake libtool abseil pkg-config pcre re2
          autoreconf -fi

      # ----------------------------
      # Ubuntu
      # ----------------------------
      - name: Install Ubuntu dependencies and run bootstrap
        if: startsWith( matrix.os, 'ubuntu')
        run: |
          sudo apt update -y
          sudo apt install -y autoconf automake g++ lcov libtool libssl-dev libabsl-dev libre2-dev pkg-config make pkg-config zlib1g-dev
          autoreconf -fi

      # ----------------------------
      # Configure for each OS
      # ----------------------------
      - name: configure for ubuntu with codecov
        if: startsWith( matrix.os, 'ubuntu')
        run: |
          ./configure --disable-opt --enable-address-sanitizer \
            CFLAGS='-g -O0 -fprofile-arcs -ftest-coverage' \
            CXXFLAGS='-g -O0 -fprofile-arcs -ftest-coverage' \
            LIBS='-lgcov'

      - name: configure for macOS
        if: startsWith( matrix.os, 'macos')
        run: |
          ./configure --enable-maintainer-mode --enable-address-sanitizer --disable-opt --enable-silent-rules

      # ----------------------------
      # Common build + test - not windows
      # ----------------------------
      - name: make check
        run: |
          make test_be20_api
          ./test_be20_api
          make check  || (for fn in test*.log ; do echo ""; echo $fn ; cat $fn ; done; exit 1)

      - uses: ammaraskar/gcc-problem-matcher@master
        name: GCC Problem Matcher

      - name: list files
        run: |
          find . -ls

      - name: Generate coverage report for ubuntu
        if: startsWith( matrix.os, 'ubuntu')
        run: |
          lcov --capture --directory . --output-file coverage.info
          #lcov --remove linux-coverage.info '/usr/*' --output-file linux-coverage.info
          lcov --list coverage.info

      - name: Upload coverage to Codecov
        if: startsWith( matrix.os, 'ubuntu')
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: false
          files: coverage.info
          flags: unittests
          name: sleuthkit-codecov
