name: CI

on: [push, pull_request]

jobs:

  Build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-20.04]
        gcc_v: [7,8,9,10,11] # Version of GFortran we want to use.
        python-version: [3.11]
    env:
      FC: gfortran-${{matrix.gcc_v}}
      GCC_V: ${{matrix.gcc_v}}

    steps:

    - name: Checkout code
      uses: actions/checkout@v4.1.6
      with:
        submodules: recursive

    - name: Setup cmake
      if: contains( matrix.gcc_v, 9 )
      uses: jwlawson/actions-setup-cmake@v2.0.2
      with:
        cmake-version: '3.28.x'

    - name: Install Python
      uses: actions/setup-python@v5.1.0 # Use pip to install latest CMake, & FORD/Jin2For, etc.
      with:
        python-version: ${{ matrix.python-version }}

    - name: Setup Graphviz
      uses: ts-graphviz/setup-graphviz@v2.0.2

    - name: Install Python dependencies
      if: contains( matrix.os, 'ubuntu')
      run: |
        python -m pip install --upgrade pip
        pip install ford FoBiS.py pygooglechart fpm
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

    - name: Install GFortran Linux
      if: contains( matrix.os, 'ubuntu')
      run: |
        sudo add-apt-repository ppa:ubuntu-toolchain-r/test
        sudo apt-get update
        sudo apt-get install -y gcc-${GCC_V} gfortran-${GCC_V}
        sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-${GCC_V} 100 \
        --slave /usr/bin/gfortran gfortran /usr/bin/gfortran-${GCC_V} \
        --slave /usr/bin/gcov gcov /usr/bin/gcov-${GCC_V}

    - name: Compile_with_build
      if: matrix.gcc_v != 7 && matrix.gcc_v != 11
      run: |
        GFORTRAN=gfortran-${{matrix.gcc_v}}
        GCOV=gcov-${{matrix.gcc_v}}
        # build with build.sh, run unit tests
        ./build.sh --skip-documentation
        ./build.sh --skip-documentation --enable-unicode

    - name: Compile_with_FPM
      if: matrix.gcc_v == 11
      run: |
        GFORTRAN=gfortran-${{matrix.gcc_v}}
        GCOV=gcov-${{matrix.gcc_v}}
        sudo apt update && sudo apt install -y valgrind
        fpm test jf_test_01 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_02 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_03 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_04 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_05 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_06 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_07 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_08 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_09 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_10 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_11 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_12 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_13 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_14 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_15 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_16 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_17 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_18 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_19 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_20 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_21 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_22 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_23 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_24 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_25 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_26 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_27 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_28 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_29 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_30 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_31 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_32 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_33 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_34 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_35 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_36 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_37 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_38 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_39 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_40 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_41 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_42 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_43 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_44 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_45 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_46 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_47 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_48 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_49 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_50 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"
        fpm test jf_test_51 --runner "valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1"

    - name: Compile_with_cmake
      # CMake build with unit tests, no documentation, with coverage analysis
      # No unicode so that coverage combined with the build script will cover unicode
      # and non-unicode code paths
      if: matrix.gcc_v == 9
      run: |
        GFORTRAN=gfortran-${{matrix.gcc_v}}
        GCOV=gcov-${{matrix.gcc_v}}
        mkdir cmake-build
        cd cmake-build
        cmake -D ENABLE_TESTS=ON ..
        make -j 4 check

    - name: Compile_with_build_mkdocs
      # build with build.sh, make documentation, run unit tests
      # and perform coverage analysis - used for doc deployment
      if: matrix.gcc_v == 7
      run: |
        GFORTRAN=gfortran-${{matrix.gcc_v}}
        GCOV=gcov-${{matrix.gcc_v}}
        ./build.sh --coverage --skip-documentation
        ./build.sh --coverage --enable-unicode

    - name: Deploy Documentation for master
      if: matrix.gcc_v == 7 && github.ref == 'refs/heads/master'
      uses: JamesIves/github-pages-deploy-action@v4.7.3
      with:
        branch: gh-pages # The branch the action should deploy to.
        folder: doc  # The folder the action should deploy.
        clean: true
        clean-exclude: |
          prev

    - name: Rebuild documentation for tagged release
      env:
        TAGNAME: ${{github.ref_name}}
      if: matrix.gcc_v == 7 && startsWith(github.ref, 'refs/tags/')
      run: |
        echo ${TAGNAME}
        rm -rf doc
        sed "2 s/^/version: ${TAGNAME}\n/" json-fortran.md > json-fortran.tagged.md
        ford --debug json-fortran.tagged.md

    - name: Deploy documentation for tagged release
      if: matrix.gcc_v == 7 && startsWith(github.ref, 'refs/tags/')
      uses: JamesIves/github-pages-deploy-action@v4.7.3
      with:
        branch: gh-pages # The branch the action should deploy to.
        folder: doc  # The folder the action should deploy.
        target-folder: prev/${{github.ref_name}} # deploy to a version-specific folder
        single-commit: true

    - name: Upload coverage
      if: matrix.gcc_v == 7
      run: |
        rm json_*.F90-*unicode.gcov || true
        mv json_*.F90.gcov src/
        mv jf_test*.[fF]90.gcov src/tests/
        bash <(curl -s https://codecov.io/bash) -v -X $GCOV
