name: cgns

# Controls when the action will run. 
#Triggers the workflow on push or pull requests.
on: 
  push:
    branches: [ develop, master ]
  pull_request:
    branches: [ develop, master ]
#on: 
#  push:
#  pull_request:
env:
  hdf5_vers: 1_14_4

# A workflow run is made up of one or more jobs that
# can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    strategy:
      matrix:
        name: ["Ubuntu Latest GCC", "macOS Latest Clang"]
        include:
          - name: "Ubuntu Latest GCC"
            artifact: "Linux.tar.xz"
            os: ubuntu-latest
            fortran: with
            parallel: enable
            hdf5: with
            tools: enable
          - name: "macOS Latest Clang"
            artifact: "macOS.tar.xz"
            os: macos-latest
            parallel: disable
            fortran: with
            hdf5: with
            tools: disable

    name: ${{ matrix.name }}
    # The type of runner that the job will run on.
    runs-on: ${{ matrix.os }}
    if: "!contains(github.event.head_commit.message, 'skip-ci')"

    # Steps represent a sequence of tasks that will be executed 
    # as part of the job.
    steps:
    - name: Install Dependencies (Linux)
      if: matrix.os == 'ubuntu-latest'
      run: |
        sudo apt update
        sudo apt upgrade
        sudo apt-get install openmpi-bin openmpi-common mpi-default-dev
        # sudo apt-get install mpich
        sudo apt-get install -y libxmu-dev tk-dev tcl-dev
        sudo apt install libgl1-mesa-dri mesa-utils libgl1-mesa-dev libglu1-mesa-dev
        # Set env vars
        echo "OS_NAME=linux" >> $GITHUB_ENV
        echo "CC=mpicc" >> $GITHUB_ENV
        echo "FC=mpif90" >> $GITHUB_ENV
        echo "F77=mpif90" >> $GITHUB_ENV
        mpiexec_cmd="mpiexec --use-hwthread-cpus -n 4"
        echo "MPIEXEC=$mpiexec_cmd" >> $GITHUB_ENV

    - name: Install Dependencies (macOS)
      if: matrix.os == 'macos-latest'
      run: |
        # Set env vars
        unset HOMEBREW_NO_INSTALL_FROM_API
        unset HOMEBREW_NO_AUTO_UPDATE
        echo "OS_NAME=macOS" >> $GITHUB_ENV
        # Use the newest stable gcc compiler
        brew update-reset
        brew cleanup
        #brew upgrade gcc
        brew install gcc
        brew unlink gcc && brew link gcc
        echo "FC=gfortran" >> $GITHUB_ENV
        echo "F77=gfortran" >> $GITHUB_ENV
        # Set CC version to latest gcc based on gfortran main version
        #gfortranversion=$( echo "__GNUC__" | gfortran -E -P - | sed 's/ //g' )
        #echo "CC=gcc-$gfortranversion" >> $GITHUB_ENV
        echo "CC=gcc" >> $GITHUB_ENV

 # Checks-out the repository under $GITHUB_WORKSPACE so the job can access it.
    - name: Get Sources
      uses: actions/checkout@v3

##################################
# INSTALL HDF5
##################################
                 
    - name: install HDF5
      run: |
        git clone https://github.com/HDFGroup/hdf5.git --branch hdf5_${{ env.hdf5_vers }} --single-branch hdf5_${{ env.hdf5_vers }}
        export HOME_DIR=$(echo ~)
        cd hdf5_${{ env.hdf5_vers }}
        ./configure --without-zlib --without-szlib --disable-fortran --disable-hl \
        --disable-tests --disable-tools \
        --prefix=$HOME_DIR/hdf5 --disable-nonstandard-feature-float16 \
        --${{ matrix.parallel }}-parallel 
        make -j 8 install
      shell: bash

##################################
# DO CMAKE FIRST SINCE AUTOTOOLS 
# BUILDS IN-PLACE, WHICH AFFECTS
# THE CMAKE BUILD
##################################

##################################
# CONFIGURE CGNS (CMake)
##################################

    - name: configure CGNS
      run: |
        bash .github/workflows/config-cgns.sh cmake \
            "--${{ matrix.parallel }}-parallel --${{ matrix.fortran }}-fortran --${{ matrix.hdf5 }}-hdf5 --${{ matrix.tools }}-cgnstools"
      shell: bash

##################################
# TEST CGNS (CMAKE)
##################################

    - name: CMake test CGNS
      run: |
        cd cbuild
        cmake --build . --config "Debug" --parallel 8
        ctest -C "Debug" -VV --output-on-failure
      shell: bash

##################################
# CONFIGURE CGNS (Autotools)
##################################

    - name: configure CGNS
      run: |
        bash .github/workflows/config-cgns.sh autotools  \
            "--${{ matrix.parallel }}-parallel --${{ matrix.fortran }}-fortran --${{ matrix.hdf5 }}-hdf5 --${{ matrix.tools }}-cgnstools"
      shell: bash

##################################
# TEST CGNS
##################################

    - name: test CGNS
      run: |
        cd src
        make -j 8
        make test
      shell: bash

#    - name: Setup tmate session
#      if: ${{ failure() }}
#      uses: mxschmitt/action-tmate@v3
