name: Build openfec

on:
  workflow_dispatch:

  push:
    branches:
      - main
    tags:
      - v*

  pull_request:
    branches:
      - main

  schedule:
    - cron: '0 0 * * 1'

jobs:

  default-build:
    strategy:
      matrix:
        image: [ubuntu-latest, macos-latest]

    runs-on: ${{ matrix.image }}
    name: ${{ matrix.image }}

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

      - name: CMake
        run: |
          mkdir build
          cd build
          cmake -DCMAKE_INSTALL_PREFIX=/usr ..

      - name: Make
        run: |
          cd build
          make -j2

      - name: Make test
        run: |
          cd build
          make test

      - name: Make install
        run: |
          cd build
          make DESTDIR=../dist install

  minimal-build:
    runs-on: ubuntu-latest
    name: minimal-build

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

      - name: CMake
        run: |
          mkdir build
          cd build
          cmake \
            -DOF_USE_REED_SOLOMON_CODEC=OFF \
            -DOF_USE_REED_SOLOMON_2_M_CODEC=ON \
            -DOF_USE_LDPC_STAIRCASE_CODEC=OFF \
            -DOF_USE_2D_PARITY_MATRIX_CODEC=OFF \
            -DOF_USE_LDPC_FROM_FILE_CODEC=OFF \
            ..

      - name: Make
        run: |
          cd build
          make -j2

      - name: Make test
        run: |
          cd build
          make test

      - name: Make install
        run: |
          cd build
          make DESTDIR=../dist install

  release:
    if: startsWith(github.ref, 'refs/tags/v')
    needs: [default-build, minimal-build]

    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Version
      id: ver
      run: |
        echo "GIT_TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT

    - name: Debian package
      id: deb
      uses: gavv/debianise@HEAD
      with:
        image: debian:stable
        package: openfec
        create_changelog: true
        install_build_depends: true

    - name: Publish
      uses: softprops/action-gh-release@v2
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        files: ${{ steps.deb.outputs.files }}
        name: ${{ steps.ver.outputs.GIT_TAG }}
        tag_name: ${{ steps.ver.outputs.GIT_TAG }}
        fail_on_unmatched_files: true
        draft: true
        prerelease: true
