name: "ubuntu"

on:
  push:
    branches: [ '**']
  pull_request:
    # The branches below must be a subset of the branches above
    branches: [ master ]
  schedule:
    - cron: '27 4 * * 2'
  workflow_dispatch: ~

jobs:
  ubuntu:
    name: ubuntu Build
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - IMAGE: 'focal'
            SCRIPT: './tools/build_and_test_cmake.sh'
          - IMAGE: 'jammy'
            CMAKE_PREFIX_PATH: '/usr/lib/x86_64-linux-gnu/cmake/Qt5'
            SCRIPT: './tools/build_and_test_cmake.sh'
          - IMAGE: 'jammy'
            CMAKE_PREFIX_PATH: '/usr/lib/x86_64-linux-gnu/cmake/Qt6'
            SCRIPT: './tools/build_and_test_cmake.sh'
          - IMAGE: 'jammy'
            CMAKE_PREFIX_PATH: '/usr/lib/x86_64-linux-gnu/cmake/Qt6'
            SCRIPT: './tools/build_and_test_cmake.sh'
            TOOLS: 'clang'
          - IMAGE: 'jammy'
            CMAKE_PREFIX_PATH: '/usr/lib/x86_64-linux-gnu/cmake/Qt5'
            SCRIPT: './tools/build_extra_tests.sh'
    container:
      image: gpsbabel-docker.jfrog.io/tsteven4/gpsbabel_build_environment_${{ matrix.IMAGE }}
      env:
        LC_ALL: 'C.UTF-8'
        JOB_CMAKE_PREFIX_PATH: ${{ matrix.CMAKE_PREFIX_PATH }}
        JOB_TOOLS: ${{ matrix.TOOLS }}
        JOB_SCRIPT: ${{ matrix.SCRIPT }}

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: build_and_test
      run: |
        # when using containers manually whitelist the checkout directory to allow git commands to work
        git config --global --add safe.directory "${GITHUB_WORKSPACE}"
        if [ -n "${JOB_CMAKE_PREFIX_PATH}" ]; then
          CMAKE_PREFIX_PATH="${JOB_CMAKE_PREFIX_PATH}"
          export CMAKE_PREFIX_PATH
        fi
        if [ "${JOB_TOOLS}" = "clang" ]; then
          CC=clang
          export CC
          CXX=clang++
          export CXX
        fi
        "${JOB_SCRIPT}"

  coverage:
    name: coverage Build
    runs-on: ubuntu-20.04
    steps:
    - name: install
      run: |
        sudo apt-get update
        sudo apt-get install gcovr lcov libusb-1.0-0-dev qt5-default  qtwebengine5-dev libqt5serialport5-dev ninja-build

    - name: Checkout repository
      uses: actions/checkout@v4

    - name: script
      env:
        CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
      run: |
        ./tools/travis_script_linux_coverage

  snap:
    name: snap Build
    runs-on: ubuntu-latest
    outputs:
      snap-file: ${{ steps.build-snap.outputs.snap }}
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Build
      uses: snapcore/action-build@v1
      id: build-snap

    # Make sure the snap is installable
    - name: Test
      run: |
        sudo snap install --dangerous ${{ steps.build-snap.outputs.snap }}
        export GBTEMP=$(pwd)/gbtemp
        mkdir -p "$GBTEMP"
        /snap/bin/gpsbabel -D3 || true
        ./testo -p /snap/bin/gpsbabel 

    - name: Deploy
      # This only handles continous releases now, for other events artifacts may be saved in
      # the 'Upload Artifacts' step.
      if: ( github.event_name == 'push' ) && ( github.ref == 'refs/heads/master' )
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        RELEASE_NAME: Continuous-Linux
      run: |
        ./tools/uploadtool/upload_github.sh ${{ steps.build-snap.outputs.snap }}

    - name: Upload Artifacts
      uses: actions/upload-artifact@v3
      with:
        name: ${{ steps.build-snap.outputs.snap }}
        path: ${{ steps.build-snap.outputs.snap }}
        retention-days: 7
