# This starter workflow is for a CMake project running on a single platform. There is a different starter workflow if you need cross-platform coverage.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-multi-platform.yml
name: CI Build

permissions:
  checks: write

on:
  push:
    branches: ["master"]
  pull_request:
    branches: ["master"]

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-22.04] # , macos-12, ubuntu-20.04,
    runs-on: ${{ matrix.os }}

    # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
    # You can convert this to a matrix build if you need cross-platform coverage.
    # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix

    steps:
      - uses: actions/checkout@v3

      - name: Install dependencies linux
        if: runner.os == 'Linux'
        run: sudo apt update && sudo apt install libxerces-c-dev libsundials-dev libginac-dev ginac-tools libginac-dev libginac11 libhdf5-dev libismrmrd-dev mpi-default-bin libboost-all-dev

      - name: Install dependencies MacOS
        if: runner.os == 'macOS'
        run: |
          /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

      #- name: Copy ginac excompiler # This is highly fragile
      #  run: cp /lib/*/ginac/ginac-excompiler /usr/bin/ginac-excompiler

      - name: Configure CMake
        # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
        # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
        run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DSKIP_CONDA=1

      - name: Build
        # Build your program with the given configuration
        run: cmake --build ${{github.workspace}}/build --parallel 3 --config ${{env.BUILD_TYPE}}

      - name: Test
        env:
          OMPI_ALLOW_RUN_AS_ROOT: 1 # allows running mpi as root
          OMPI_ALLOW_RUN_AS_ROOT_CONFIRM: 1 # allows running mpi as root
          OMPI_MCA_rmaps_base_oversubscribe: true # allows oversubscription for mpi test
        working-directory: ${{github.workspace}}/build
        #run: chmod u+x ./sanityck && ./sanityck ../share/examples 2
        # Execute tests defined by the CMake configuration.
        # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
        run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure --output-junit ${{github.workspace}}/build/jemris-sanitycheck-test.xml

      # - name: Publish Test Report
      #   uses: mikepenz/action-junit-report@v4
      #   if: success() || failure() # always run even if the previous step fails
      #   with:
      #     report_paths: '${{github.workspace}}/build/test.xml'
      - name: Copy Test Results
        if: always()
        run: |
          cp -Lpr ${{github.workspace}}/build/jemris-sanitycheck-test.xml test-results.xml
        shell: bash

      - name: Publish Test Results
        uses: EnricoMi/publish-unit-test-result-action@v2
        if: always()
        with:
          files: |
            test-results.xml

      - name: Generate package
        working-directory: ${{github.workspace}}/build
        run: |
          mkdir -p final_packages && cpack -G TBZ2 -B final_packages

      - name: Store results as artifacts
        uses: actions/upload-artifact@v3
        with:
          name: jemris-package
          path: |
            ${{github.workspace}}/build/final_packages/*.tar.bz2
