name: CI - Ubuntu

on: [pull_request, push]

permissions:
  actions: write

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: ${{ github.head_ref != '' }}

jobs:
  ci:
    name: Build
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        libt_version: ["2.0.8", "1.2.18"]
        qbt_gui: ["GUI=ON", "GUI=OFF"]
        qt_version: ["5.15.2", "6.2.0"]
        exclude:
          - libt_version: "1.2.18"
            qt_version: "6.2.0"

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

      - name: Install dependencies
        run: |
          sudo apt update
          sudo apt install \
            build-essential cmake ninja-build pkg-config \
            libboost-dev libssl-dev libxkbcommon-x11-dev zlib1g-dev

      - name: Setup ccache
        uses: Chocobo1/setup-ccache-action@v1
        with:
          update_packager_index: false
          ccache_options: |
            max_size=2G

      - name: Install Qt
        uses: jurplel/install-qt-action@v3
        with:
          version: ${{ matrix.qt_version }}
          archives: icu qtbase qtsvg qttools

      - name: Install libtorrent
        run: |
          git clone \
            --branch v${{ matrix.libt_version }} \
            --depth 1 \
            --recurse-submodules \
            https://github.com/arvidn/libtorrent.git
          cd libtorrent
          cmake \
            -B build \
            -G "Ninja" \
            -DCMAKE_BUILD_TYPE=RelWithDebInfo \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
            -Ddeprecated-functions=OFF
          cmake --build build
          sudo cmake --install build

      - name: Build qBittorrent (Qt5)
        if: ${{ startsWith(matrix.qt_version, 5) }}
        run: |
          CXXFLAGS="$CXXFLAGS -Werror -Wno-error=deprecated-declarations" \
          LDFLAGS="$LDFLAGS -gz" \
          cmake \
            -B build \
            -G "Ninja" \
            -DCMAKE_BUILD_TYPE=RelWithDebInfo \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
            -DCMAKE_INSTALL_PREFIX="/usr" \
            -DTESTING=ON \
            -DVERBOSE_CONFIGURE=ON \
            -D${{ matrix.qbt_gui }}
          cmake --build build --target qbt_update_translations
          cmake --build build
          cmake --build build --target check
          DESTDIR="qbittorrent" cmake --install build

      - name: Build qBittorrent (Qt6)
        if: ${{ startsWith(matrix.qt_version, 6) }}
        run: |
          CXXFLAGS="$CXXFLAGS -Werror" \
          LDFLAGS="$LDFLAGS -gz" \
          cmake \
            -B build \
            -G "Ninja" \
            -DCMAKE_BUILD_TYPE=RelWithDebInfo \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
            -DCMAKE_INSTALL_PREFIX="/usr" \
            -DQT6=ON \
            -DTESTING=ON \
            -DVERBOSE_CONFIGURE=ON \
            -D${{ matrix.qbt_gui }}
          cmake --build build --target qbt_update_translations
          cmake --build build
          cmake --build build --target check
          DESTDIR="qbittorrent" cmake --install build

      - name: Prepare build artifacts
        run: |
          mkdir upload
          mkdir upload/cmake
          cp build/compile_commands.json upload/cmake
          mkdir upload/cmake/libtorrent
          cp libtorrent/build/compile_commands.json upload/cmake/libtorrent

      - name: 'AppImage: Prepare env'
        run: |
          sudo apt install libfuse2
          wget https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage
          wget https://github.com/linuxdeploy/linuxdeploy-plugin-qt/releases/download/continuous/linuxdeploy-plugin-qt-x86_64.AppImage
          wget https://github.com/linuxdeploy/linuxdeploy-plugin-appimage/releases/download/continuous/linuxdeploy-plugin-appimage-x86_64.AppImage
          chmod +x linuxdeploy-x86_64.AppImage
          chmod +x linuxdeploy-plugin-qt-x86_64.AppImage
          chmod +x linuxdeploy-plugin-appimage-x86_64.AppImage

      - name: 'AppImage: Prepare nox'
        if: matrix.qbt_gui == 'GUI=OFF'
        run: |
          mkdir -p qbittorrent/usr/share/icons/hicolor/scalable/apps/
          mkdir -p qbittorrent/usr/share/applications/
          cp dist/unix/menuicons/scalable/apps/qbittorrent.svg qbittorrent/usr/share/icons/hicolor/scalable/apps/qbittorrent.svg
          cp .github/workflows/helper/appimage/org.qbittorrent.qBittorrent.desktop qbittorrent/usr/share/applications/org.qbittorrent.qBittorrent.desktop

      - name: 'AppImage: Package'
        run: |
          ./linuxdeploy-x86_64.AppImage --appdir=qbittorrent --plugin qt
          rm qbittorrent/apprun-hooks/*
          cp .github/workflows/helper/appimage/export_vars.sh qbittorrent/apprun-hooks/export_vars.sh
          NO_APPSTREAM=1 \
          OUTPUT=upload/qbittorrent-CI_Ubuntu_x86_64.AppImage \
          ./linuxdeploy-x86_64.AppImage --appdir=qbittorrent --output appimage

      - name: Upload build artifacts
        uses: actions/upload-artifact@v3
        with:
          name: qBittorrent-CI_Ubuntu-x64_${{ matrix.qbt_gui }}_libtorrent-${{ matrix.libt_version }}_Qt-${{ matrix.qt_version }}
          path: upload
