name: 🐧 Linux GUI
on:
  push:
  pull_request:
  schedule:
    - cron: '0 0 * * 2'

env:
  CARGO_TERM_COLOR: always

jobs:
  linux-krokiet-gui:
    strategy:
      matrix:
        toolchain: [ stable, 1.74.0 ]
        type: [ release ]
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v4

      - name: Setup rust version
        run: rustup default ${{ matrix.toolchain }}

      - name: Enable LTO
        run: sed -i 's/#lto = "fat"/lto = "fat"/g' Cargo.toml
        if: ${{ (matrix.type == 'release') }}

      - name: Build Release Krokiet
        run: cargo build --release --bin krokiet
        if: ${{ (matrix.type == 'release') }}

      - name: Store Linux GUI Krokiet
        uses: actions/upload-artifact@v4
        with:
          name: krokiet-${{ runner.os }}-${{ matrix.toolchain }}
          path: target/release/krokiet
        if: ${{ matrix.type == 'release' }}

  linux-krokiet-gui-heif:
    strategy:
      matrix:
        toolchain: [ stable, 1.74.0 ]
        type: [ release ]
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Install basic libraries
        run: sudo apt update || true; sudo apt install libheif-dev libraw-dev -y

      - name: Setup rust version
        run: rustup default ${{ matrix.toolchain }}

      - name: Enable LTO
        run: sed -i 's/#lto = "fat"/lto = "fat"/g' Cargo.toml
        if: ${{ (matrix.type == 'release') }}

      - name: Build Release Krokiet heif
        run: cargo build --release --bin krokiet --features "heif,libraw"
        if: ${{ (matrix.type == 'release') }}

      - name: Store Linux GUI Krokiet heif libraw
        uses: actions/upload-artifact@v4
        with:
          name: krokiet-${{ runner.os }}-${{ matrix.toolchain }}-heif-libraw
          path: target/release/krokiet
        if: ${{ matrix.type == 'release' }}

  linux-gui:
    strategy:
      matrix:
        toolchain: [ stable, 1.74.0 ]
        type: [ release ]
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Install basic libraries
        run: sudo apt update || true; sudo apt install libgtk-4-dev libheif-dev libraw-dev -y

      - name: Setup rust version
        run: rustup default ${{ matrix.toolchain }}

      - name: Enable LTO
        run: sed -i 's/#lto = "fat"/lto = "fat"/g' Cargo.toml
        if: ${{ (matrix.type == 'release') }}

      - name: Build Release Heif Libraw
        run: cargo build --release --bin czkawka_gui --features "heif,libraw"
        if: ${{ (matrix.type == 'release') }}

      - name: Store Linux GUI Heif Libraw
        uses: actions/upload-artifact@v4
        with:
          name: czkawka_gui-${{ runner.os }}-${{ matrix.toolchain }}-heif-libraw
          path: target/release/czkawka_gui
        if: ${{ (matrix.type == 'release') && (matrix.toolchain == 'stable') }}

      - name: Build Release
        run: cargo build --release --bin czkawka_gui
        if: ${{ (matrix.type == 'release') }}

      # Only store stable toolchain
      - name: Store Linux GUI
        uses: actions/upload-artifact@v4
        with:
          name: czkawka_gui-${{ runner.os }}-${{ matrix.toolchain }}
          path: target/release/czkawka_gui
        if: ${{ (matrix.type == 'release') && (matrix.toolchain == 'stable') }}

  linux-appimage-gui:
    strategy:
      matrix:
        toolchain: [ stable ]
        type: [ release ]
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Install Dependencies
        run: sudo apt update || true; sudo apt install libgtk-4-dev libheif-dev librsvg2-dev wget fuse libfuse2 -y

      - name: Setup rust version
        run: rustup default ${{ matrix.toolchain }}

      - name: Enable LTO
        run: sed -i 's/#lto = "fat"/lto = "fat"/g' Cargo.toml
        if: ${{ (matrix.type == 'release') }}

      - name: Build Release
        run: cargo build --release --bin czkawka_gui

      - name: Download appimage dependencies
        run: |
          pwd
          wget -c "https://raw.githubusercontent.com/linuxdeploy/linuxdeploy-plugin-gtk/master/linuxdeploy-plugin-gtk.sh"
          wget -c "https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage"
          chmod +x linuxdeploy-plugin-gtk.sh
          chmod +x linuxdeploy-x86_64.AppImage
          mkdir -p AppDir/usr/bin
          pwd
          cp target/release/czkawka_gui AppDir/usr/bin
          ./linuxdeploy-x86_64.AppImage --appdir AppDir --plugin gtk --output appimage --icon-file data/icons/com.github.qarmin.czkawka.svg --desktop-file data/com.github.qarmin.czkawka.desktop

      - name: Store Linux Appimage GUI
        uses: actions/upload-artifact@v4
        with:
          name: czkawka_gui-appimage-${{ runner.os }}-${{ matrix.toolchain }}
          path: Czkawka*.AppImage

      - name: Minimal AppImage
        run: |
          pwd
          rm -rf czkawka_gui
          cp target/release/czkawka_gui .
          strip czkawka_gui
          wget https://github.com/AppImageCommunity/pkg2appimage/releases/download/continuous/pkg2appimage--x86_64.AppImage -O pkg2.appimage
          chmod +x ./pkg2.appimage
          ./pkg2.appimage misc/czkawka-appimage-recipe.yml
          mv out/Czkawka*.AppImage out/czkawka_gui-minimal.AppImage

      - name: Minimal Appimage Upload
        uses: actions/upload-artifact@v4
        with:
          name: czkawka_gui-${{ matrix.toolchain }}_minimal_AppImage
          path: out/*.AppImage

  linux-tests:
    strategy:
      matrix:
        toolchain: [ stable ]
        type: [ debug ]
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Install Dependencies
        run: sudo apt update || true; sudo apt install libgtk-4-dev libheif-dev librsvg2-dev wget fuse libfuse2 -y xvfb

      - name: Setup rust version
        run: rustup default ${{ matrix.toolchain }}

      - name: Test
        run: xvfb-run cargo test
