name: Build and Test on Linux with netcdf-c github master
on: [push, pull_request]
jobs:
  build-linux:
    name: Python (${{ matrix.python-version }})
    runs-on: ubuntu-latest
    env:
      NETCDF_DIR: ${{ github.workspace }}/..
      CC: mpicc.mpich
      #NO_NET: 1
    strategy:
      matrix:
        python-version: ["3.12"]
    steps:

    - uses: actions/checkout@v4
      with:
        submodules: true

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}

    - name: Install Ubuntu Dependencies
      run: |
        sudo apt-get update
        sudo apt-get install mpich libmpich-dev libhdf5-mpich-dev libcurl4-openssl-dev bzip2 libsnappy-dev libblosc-dev libzstd-dev
        echo "Download and build netCDF github master"
        git clone https://github.com/Unidata/netcdf-c
        pushd netcdf-c
        export CPPFLAGS="-I/usr/include/hdf5/mpich -I${NETCDF_DIR}/include"
        export LDFLAGS="-L${NETCDF_DIR}/lib"
        export LIBS="-lhdf5_mpich_hl -lhdf5_mpich -lm -lz"
        autoreconf -i
        ./configure --prefix $NETCDF_DIR --enable-netcdf-4 --enable-shared --enable-dap --enable-parallel4 
        make -j 2
        sudo make install
        popd

#   - name: The job has failed
#     if: ${{ failure() }}
#     run: |
#       cd netcdf-c-${NETCDF_VERSION}
#       cat config.log 

    - name: Install python dependencies via pip
      run: |
        python -m pip install --upgrade pip
        pip install numpy cython cftime pytest twine wheel check-manifest mpi4py mypy types-setuptools typing-extensions

    - name: Install netcdf4-python
      run: |
        export PATH=${NETCDF_DIR}/bin:${PATH} 
        export NETCDF_PLUGIN_DIR=${{ github.workspace }}/netcdf-c/plugins/plugindir
        python setup.py install

    - name: Test
      run: |
        export PATH=${NETCDF_DIR}/bin:${PATH} 
        #export HDF5_PLUGIN_PATH=${NETCDF_DIR}/plugins/plugindir
        python checkversion.py
        # serial
        cd test
        python run_all.py
        # parallel
        cd ../examples
        mpirun.mpich -np 4 python mpi_example.py
        if [ $? -ne 0 ] ; then
          echo "hdf5 mpi test failed!"
          exit 1
        else
          echo "hdf5 mpi test passed!"
        fi
        mpirun.mpich -np 4 python mpi_example_compressed.py
        if [ $? -ne 0 ] ; then
          echo "hdf5 compressed mpi test failed!"
          exit 1
        else
          echo "hdf5 compressed mpi test passed!"
        fi

    - name: Stubtest
      run: |
        stubtest netCDF4 --allowlist .github/stubtest-allowlist --mypy-config-file=pyproject.toml
        mypy test
        mypy examples
