name: CI

on:
- push
- pull_request

jobs:
  build:
    name: ${{ matrix.os }} build

    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
        - ubuntu-16.04
        - ubuntu-18.04
        - ubuntu-20.04
      fail-fast: false

    steps:
    - uses: actions/checkout@v2
    - name: Get dependencies
      run: |
        sudo apt-get update -y
        sudo apt-get install -y wget git gawk findutils
        xargs -a <(awk '! /^ *(#|$)/' ".github/workflows/apt.txt") -r -- \
            sudo apt-get install -y --no-install-recommends --no-install-suggests
    - name: Create installation directory
      run: |
        mkdir $HOME/install
    - name: Ensure one core for compilation
      run: |
        echo "MAKEFLAGS=-j1" >> $GITHUB_ENV
    - name: Set LD_LIBRARY_PATH for compilation
      run: |
        echo "LD_LIBRARY_PATH=$HOME/install/lib" >> $GITHUB_ENV
    - name: Build
      run: .github/workflows/build.sh $HOME/install

  test:
    name: ${{ matrix.os }} tests

    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
        - ubuntu-16.04
        - ubuntu-18.04
        - ubuntu-20.04
      fail-fast: false

    steps:
    - uses: actions/checkout@v2
    - name: Get dependencies
      run: |
        sudo apt-get update -y
        sudo apt-get install -y wget git gawk findutils
        xargs -a <(awk '! /^ *(#|$)/' ".github/workflows/apt.txt") -r -- \
            sudo apt-get install -y --no-install-recommends --no-install-suggests
    - name: Create installation directory
      run: |
        mkdir $HOME/install
    - name: Set number of cores for compilation
      run: |
        echo "MAKEFLAGS=-j$(nproc)" >> $GITHUB_ENV
    - name: Set LD_LIBRARY_PATH for compilation
      run: |
        echo "LD_LIBRARY_PATH=$HOME/install/lib" >> $GITHUB_ENV
    - name: Build
      run: .github/workflows/build.sh $HOME/install
    - name: Add the bin directory to PATH
      run: |
        echo "$HOME/install/bin" >> $GITHUB_PATH
    - name: Test executing of the grass command
      run: .github/workflows/test_simple.sh
    - name: Run tests
      run: .github/workflows/test_thorough.sh
