name: 🏁 Windows
on:
  push:
  pull_request:
  schedule:
    - cron: '0 0 * * 2'

env:
  CARGO_TERM_COLOR: always

jobs:
  krokiet-compiled-on-linux:
    strategy:
      fail-fast: false
      matrix:
        use_heif: [ normal ]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install dependencies(mostly sd)
        run: |
          sudo apt update || true;sudo apt install -y mingw-w64 mingw-w64-x86-64-dev
          wget https://github.com/chmln/sd/releases/download/v1.0.0/sd-v1.0.0-x86_64-unknown-linux-gnu.tar.gz -O a.tar.gz
          tar -xzf a.tar.gz
          cp sd-v1.0.0-x86_64-unknown-linux-gnu/sd .
          chmod +x ./sd

      - name: Setup rust version
        run: |
          rustup target add x86_64-pc-windows-gnu

      - name: Enable LTO
        run: sed -i 's/#lto = "fat"/lto = "fat"/g' Cargo.toml

      - name: Compile Krokiet
        run: cargo build --release --target x86_64-pc-windows-gnu --bin krokiet

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: krokiet-windows-c-on-linux-${{ github.sha }}-${{ matrix.use_heif }}
          path: |
            target/x86_64-pc-windows-gnu/release/krokiet.exe
          if-no-files-found: error

      - name: Show console window on windows
        run: |
          ./sd -s '#![windows_subsystem = "windows"]' '//#![windows_subsystem = "windows"]' krokiet/src/main.rs
          cat krokiet/src/main.rs

      - name: Compile Krokiet Console
        run: cargo build --release --target x86_64-pc-windows-gnu --bin krokiet

      - name: Upload artifacts Console
        uses: actions/upload-artifact@v4
        with:
          name: krokiet-windows-c-on-linux-${{ github.sha }}-${{ matrix.use_heif }}-console
          path: |
            target/x86_64-pc-windows-gnu/release/krokiet.exe
          if-no-files-found: error

  krokiet-compiled-on-windows:
    strategy:
      fail-fast: false
      matrix:
        use_heif: [ normal ]
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install dependencies(mostly sd)
        run: |
          Invoke-WebRequest -Uri https://github.com/chmln/sd/releases/download/v1.0.0/sd-v1.0.0-x86_64-pc-windows-gnu.zip -OutFile a.zip
          Expand-Archive ./a.zip
          cp a/sd-v1.0.0-x86_64-pc-windows-gnu/sd.exe .

      - name: Setup rust version
        run: rustup default stable-x86_64-pc-windows-gnu

      - name: Enable LTO
        run: sed -i 's/#lto = "fat"/lto = "fat"/g' Cargo.toml

      - name: Compile Krokiet
        run: cargo build --release --bin krokiet

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: krokiet-windows-c-on-windows-${{ github.sha }}-${{ matrix.use_heif }}
          path: |
            target/release/krokiet.exe
          if-no-files-found: error

      - name: Show console window on windows
        run: |
          ./sd.exe -s '#![windows_subsystem = "windows"]' '//#![windows_subsystem = "windows"]' krokiet/src/main.rs
          cat krokiet/src/main.rs

      - name: Compile Krokiet Console
        run: cargo build --release --bin krokiet

      - name: Upload artifacts Console
        uses: actions/upload-artifact@v4
        with:
          name: krokiet-windows-c-on-windows-${{ github.sha }}-${{ matrix.use_heif }}-console
          path: |
            target/release/krokiet.exe
          if-no-files-found: error


  container_4_10:
    strategy:
      fail-fast: false
      matrix:
        use_heif: [ non_heif ]
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/mglolenstine/gtk4-cross:gtk-4.10
    steps:
      - uses: actions/checkout@v4
      - name: Install additional dependencies
        # gio is for the build script
        run: |
          dnf install curl wget2 unzip mingw64-bzip2.noarch mingw64-poppler mingw64-poppler-glib mingw32-python3 rust-gio-devel adwaita-icon-theme -y && dnf clean all -y
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
          source "$HOME/.cargo/env"
          rustup target add x86_64-pc-windows-gnu

      - name: Cross compile for Windows
        run: |
          source "$HOME/.cargo/env"
          #!/bin/bash
          set -euo pipefail
          export PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/share/pkgconfig:$MINGW_PREFIX/lib/pkgconfig/:/usr/x86_64-w64-mingw32/lib/pkgconfig/
          cargo build --target=x86_64-pc-windows-gnu --release --locked
          mkdir -p package
          cp target/x86_64-pc-windows-gnu/release/czkawka_gui.exe package/
          cp target/x86_64-pc-windows-gnu/release/czkawka_cli.exe package/

      - name: Package
        run: |
          #!/bin/bash
          set -euo pipefail
          cp -t package $(pds -vv -f package/*.exe)
          # Add gdbus which is recommended on Windows (why?)
          cp $MINGW_PREFIX/bin/gdbus.exe package
          # Handle the glib schema compilation as well
          glib-compile-schemas $MINGW_PREFIX/share/glib-2.0/schemas/
          mkdir -p package/share/glib-2.0/schemas/
          cp -T $MINGW_PREFIX/share/glib-2.0/schemas/gschemas.compiled package/share/glib-2.0/schemas/gschemas.compiled
          # Pixbuf stuff, in order to get SVGs (scalable icons) to load
          mkdir -p package/lib/gdk-pixbuf-2.0
          cp -rT $MINGW_PREFIX/lib/gdk-pixbuf-2.0 package/lib/gdk-pixbuf-2.0
          cp -f -t package $(pds -vv -f $MINGW_PREFIX/lib/gdk-pixbuf-2.0/2.10.0/loaders/*)
          find package -iname "*.dll" -or -iname "*.exe" -type f -exec mingw-strip {} +

          cd package/share
          wget2 https://github.com/qarmin/czkawka/files/10832192/gtk4_theme.zip
          unzip gtk4_theme.zip
          rm gtk4_theme.zip
          cd ../..
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: czkawka-windows-${{ github.sha }}-${{ matrix.use_heif }}-4.10
          path: |
            ./package
          if-no-files-found: error

  container_4_6:
    strategy:
      fail-fast: false
      matrix:
        use_heif: [ non_heif ]
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/piegamesde/gtk4-cross:gtk-4.6
    steps:
      - uses: actions/checkout@v4
      - name: Install additional dependencies
        # gio is for the build script
        run: |
          dnf install curl wget2 unzip mingw64-bzip2.noarch mingw64-poppler mingw64-poppler-glib mingw32-python3 rust-gio-devel adwaita-icon-theme -y && dnf clean all -y
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
          source "$HOME/.cargo/env"
          rustup target add x86_64-pc-windows-gnu

      - name: Cross compile for Windows
        run: |
          source "$HOME/.cargo/env"
          #!/bin/bash
          set -euo pipefail
          export PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/share/pkgconfig:$MINGW_PREFIX/lib/pkgconfig/:/usr/x86_64-w64-mingw32/lib/pkgconfig/
          cargo build --target=x86_64-pc-windows-gnu --release --locked
          mkdir -p package
          cp target/x86_64-pc-windows-gnu/release/czkawka_gui.exe package/
          cp target/x86_64-pc-windows-gnu/release/czkawka_cli.exe package/

      - name: Package
        run: |
          #!/bin/bash
          set -euo pipefail
          cp -t package $(pds -vv -f package/*.exe)
          # Add gdbus which is recommended on Windows (why?)
          cp $MINGW_PREFIX/bin/gdbus.exe package
          # Handle the glib schema compilation as well
          glib-compile-schemas $MINGW_PREFIX/share/glib-2.0/schemas/
          mkdir -p package/share/glib-2.0/schemas/
          cp -T $MINGW_PREFIX/share/glib-2.0/schemas/gschemas.compiled package/share/glib-2.0/schemas/gschemas.compiled
          # Pixbuf stuff, in order to get SVGs (scalable icons) to load
          mkdir -p package/lib/gdk-pixbuf-2.0
          cp -rT $MINGW_PREFIX/lib/gdk-pixbuf-2.0 package/lib/gdk-pixbuf-2.0
          cp -f -t package $(pds -vv -f $MINGW_PREFIX/lib/gdk-pixbuf-2.0/2.10.0/loaders/*)
          find package -iname "*.dll" -or -iname "*.exe" -type f -exec mingw-strip {} +

          cd package/share
          wget2 https://github.com/qarmin/czkawka/files/10832192/gtk4_theme.zip
          unzip gtk4_theme.zip
          rm gtk4_theme.zip
          cd ../..
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: czkawka-windows-${{ github.sha }}-${{ matrix.use_heif }}-4.6
          path: |
            ./package
          if-no-files-found: error

  # Provide option to log things to windows CLI
  container_4_6_console_window:
    runs-on: ubuntu-22.04
    container:
      image: ghcr.io/piegamesde/gtk4-cross:gtk-4.6
    steps:
      - uses: actions/checkout@v4
      - name: Install dependencies(mostly sd)
        run: |
          dnf install wget -y
          wget https://github.com/chmln/sd/releases/download/v1.0.0/sd-v1.0.0-x86_64-unknown-linux-gnu.tar.gz -O a.tar.gz
          tar -xzf a.tar.gz
          chmod +x sd-v1.0.0-x86_64-unknown-linux-gnu/sd
          sudo cp sd-v1.0.0-x86_64-unknown-linux-gnu/sd /usr/bin/sd
      - name: Install additional dependencies
        # gio is for the build script
        run: |
          dnf install curl wget2 unzip mingw64-bzip2.noarch mingw64-poppler mingw64-poppler-glib mingw32-python3 rust-gio-devel adwaita-icon-theme -y && dnf clean all -y
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
          source "$HOME/.cargo/env"
          rustup target add x86_64-pc-windows-gnu

      - name: Show console window on windows
        run: sd -s '#![windows_subsystem = "windows"]' '//#![windows_subsystem = "windows"]' krokiet/src/main.rs
      - name: Cross compile for Windows
        run: |
          source "$HOME/.cargo/env"
          #!/bin/bash
          set -euo pipefail
          export PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/share/pkgconfig:$MINGW_PREFIX/lib/pkgconfig/:/usr/x86_64-w64-mingw32/lib/pkgconfig/
          cargo build --target=x86_64-pc-windows-gnu --release --locked
          mkdir -p package
          cp target/x86_64-pc-windows-gnu/release/czkawka_gui.exe package/
          cp target/x86_64-pc-windows-gnu/release/czkawka_cli.exe package/
      - name: Package
        run: |
          #!/bin/bash
          set -euo pipefail
          cp -t package $(pds -vv -f package/*.exe)
          # Add gdbus which is recommended on Windows (why?)
          cp $MINGW_PREFIX/bin/gdbus.exe package
          # Handle the glib schema compilation as well
          glib-compile-schemas $MINGW_PREFIX/share/glib-2.0/schemas/
          mkdir -p package/share/glib-2.0/schemas/
          cp -T $MINGW_PREFIX/share/glib-2.0/schemas/gschemas.compiled package/share/glib-2.0/schemas/gschemas.compiled
          # Pixbuf stuff, in order to get SVGs (scalable icons) to load
          mkdir -p package/lib/gdk-pixbuf-2.0
          cp -rT $MINGW_PREFIX/lib/gdk-pixbuf-2.0 package/lib/gdk-pixbuf-2.0
          cp -f -t package $(pds -vv -f $MINGW_PREFIX/lib/gdk-pixbuf-2.0/2.10.0/loaders/*)
          find package -iname "*.dll" -or -iname "*.exe" -type f -exec mingw-strip {} +

          cd package/share
          wget2 https://github.com/qarmin/czkawka/files/10832192/gtk4_theme.zip
          unzip gtk4_theme.zip
          rm gtk4_theme.zip
          cd ../..
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: czkawka-windows-${{ github.sha }}-console-4.6
          path: |
            ./package
          if-no-files-found: error
