---
name: 🐧 Linux
on:
  push:
    branches:
      - master
      - release-**
  pull_request:
  release:
    types: ['published']

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: build (linux)
    runs-on: ubuntu-22.04

    steps:
      - name: 🐣 Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: 🌾 Prepare variables
        id: vars
        run: |
          ./scripts/ci/env_gh.sh

          BUILD_ROOT="/home/runner"
          echo "BUILD_ROOT=${BUILD_ROOT}" >> $GITHUB_ENV
          echo "BUILD_TYPE=Release" >> $GITHUB_ENV

      - name: 💎 Setup ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.2' # Not needed with a .ruby-version file
          bundler-cache: true # runs 'bundle install' and caches installed gems automatically

      - name: 🐩 Install CMake and Ninja
        uses: lukka/get-cmake@latest
        with:
          cmakeVersion: 3.29.0

      - name: 🔨 Prepare build env
        run: |
          sudo apt-get update
          sudo apt-get install -y gperf autopoint '^libxcb.*-dev' libx11-xcb-dev libegl1-mesa libegl1-mesa-dev libgl1-mesa-dev libglu1-mesa-dev mesa-common-dev libxrender-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev libxrandr-dev libxxf86vm-dev autoconf-archive libgstreamer-gl1.0-0 libgstreamer-plugins-base1.0-0 libfuse2 libpulse-dev libcups2-dev nasm python3-tk
          # Required to run unit tests on linux

      - name: Install linuxdeploy
        uses: miurahr/install-linuxdeploy-action@v1
        with:
          dir: ${{ env.BUILD_ROOT }}
          plugins: qt appimage

      - name: 🌱 Install dependencies and generate project files
        run: |
          source ./scripts/version_number.sh
          source ./scripts/ci/generate-version-details.sh

          cmake -S "${{ github.workspace }}" \
                -B "build" \
                -G Ninja \
                -D CMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
                -D WITH_VCPKG=ON \
                -D WITH_SPIX=ON \
                -D WITH_NFC=OFF \
                -D APP_VERSION="${APP_VERSION}" \
                -D APP_VERSION_STR="${APP_VERSION_STR}" \
                -D APP_PACKAGE_NAME="${APP_PACKAGE_NAME}" \
                -D ENABLE_TESTS=ON \
                -D NUGET_USERNAME=opengisch \
                -D NUGET_TOKEN=${{ secrets.GITHUB_TOKEN }} \
                -D SENTRY_DSN=${{ secrets.SENTRY_DSN }} \
                -D SENTRY_ENV="${APP_ENV}" \
                -D CMAKE_INSTALL_PREFIX=/usr \
                -D CMAKE_EXPORT_COMPILE_COMMANDS=ON \
                -D CMAKE_COMPILE_WARNING_AS_ERROR=ON \
                -D LINUXDEPLOY_EXECUTABLE=${{ env.BUILD_ROOT }}/linuxdeploy-x86_64.AppImage

      - name: 📑 Upload dep build logs
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: build-logs-x64-linux
          path: |
            build/**/*.log

      - name: Initialize CodeQL
        uses: github/codeql-action/init@v3
        with:
          languages: 'c-cpp'

      - name: 🌋 Build
        run: |
          xvfb-run cmake --build build --config ${{ env.BUILD_TYPE }}

      - uses: ZedThree/clang-tidy-review@v0.20.1
        id: review
        if: github.event_name == 'pull_request'
        with:
          build_dir: "build"
          lgtm_comment_body: ''
          clang_tidy_checks: '-*,performance-*,bugprone-*,clang-analyzer-*,mpi-*,-bugprone-narrowing-conversions,-bugprone-easily-swappable-parameters,-clang-analyzer-optin.core.EnumCastOutOfRange,-performance-enum-size'

      - name: Package
        run: |
          export LD_LIBRARY_PATH="${{ github.workspace }}/build/vcpkg_installed/x64-linux/lib/":${LD_LIBRARY_PATH}
          cmake --build  "build" --target bundle --config ${{ env.BUILD_TYPE }}
          echo "ARTIFACT_PATHNAME=build/QField-x86_64.AppImage" >> $GITHUB_ENV
          echo "ARTIFACT_NAME=qfield-${{ env.CI_PACKAGE_FILE_SUFFIX }}-linux-x64.AppImage" >> $GITHUB_ENV

      - name: 📦 Upload package
        if: ${{ env.ARTIFACT_NAME != null }}
        uses: actions/upload-artifact@v4
        with:
          name: "QField-dev-x64-linux-${{ env.BUILD_TYPE }}"
          path: ${{ env.ARTIFACT_PATHNAME }}

      - name: 🧫 Test
        env:
          PROJ_LIB: ${{ github.workspace }}/build/vcpkg_installed/x64-linux/share/proj
        run: |
          pip install -r "${{ github.workspace }}/test/spix/requirements.txt"
          docker compose -f "${{ github.workspace }}/test/docker-compose.yaml" up -d
          echo "[test-clientssl]" >> ~/.pg_service.conf
          echo "host=localhost" >> ~/.pg_service.conf
          echo "port=5440" >> ~/.pg_service.conf
          echo "dbname=gis" >> ~/.pg_service.conf
          echo "user=docker" >> ~/.pg_service.conf
          echo "password=docker" >> ~/.pg_service.conf
          echo "sslmode=require" >> ~/.pg_service.conf
          echo "sslcert=${{ github.workspace }}/test/postgis_docker/cert_keys/client.crt" >> ~/.pg_service.conf
          echo "sslkey=${{ github.workspace }}/test/postgis_docker/cert_keys/client.key" >> ~/.pg_service.conf
          echo "sslrootcert=${{ github.workspace }}/test/postgis_docker/cert_keys/server.crt" >> ~/.pg_service.conf
          chown $USER ${{ github.workspace }}/test/postgis_docker/cert_keys/client.crt
          chmod 700 ${{ github.workspace }}/test/postgis_docker/cert_keys/client.crt
          chown $USER ${{ github.workspace }}/test/postgis_docker/cert_keys/client.key
          chmod 700 ${{ github.workspace }}/test/postgis_docker/cert_keys/client.key
          chown $USER ${{ github.workspace }}/test/postgis_docker/cert_keys/server.crt
          chmod 700 ${{ github.workspace }}/test/postgis_docker/cert_keys/server.crt

          cd build
          xvfb-run --server-args="-screen 0 640x480x24" ctest --output-on-failure -C ${{ env.BUILD_TYPE }}

          docker compose -f "${{ github.workspace }}/test/docker-compose.yaml" down

      - name: 📑 Upload package logs
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: package-logs-x64-linux
          path: build/_CPack_Packages/**/*.log

      - name: 📊 Upload test report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: "test-report-x64-linux-${{ env.BUILD_TYPE }}"
          path: "build/report"

      - name: 🚀 Upload release asset
        if: github.event_name == 'release' && startsWith(github.ref, 'refs/tags/v') && env.ARTIFACT_NAME != null
        uses: shogo82148/actions-upload-release-asset@v1
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ${{ env.ARTIFACT_PATHNAME }}
          asset_name: ${{ env.ARTIFACT_NAME }}
          overwrite: true

      - name: 📮 Upload debug symbols
        if: github.event_name == 'release' && startsWith(github.ref, 'refs/tags/v') && env.ARTIFACT_NAME != null
        env:
          SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
          SENTRY_ORG_SLUG: opengisch
          SENTRY_PROJECT_SLUG: qfield
        run: |
          bundle exec fastlane run sentry_debug_files_upload path:build

      - name: Perform CodeQL Analysis
        uses: github/codeql-action/analyze@v3
        with:
          category: "/language:c-cpp"
