# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=czkawka
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-cli"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-gui"
         "${MINGW_PACKAGE_PREFIX}-krokiet")
pkgver=10.0.0
pkgrel=1
pkgdesc="Multi functional app to find duplicates, empty folders, similar images etc (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/qarmin/czkawka'
msys2_references=(
  'purl: pkg:cargo/czkawka_gui'
  'purl: pkg:cargo/czkawka_cli'
  'purl: pkg:cargo/krokiet'
)
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs" "${MINGW_PACKAGE_PREFIX}-libheif")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-gtk4")
source=("https://github.com/qarmin/czkawka/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('66ff3c231abe2feaeb377f52bb188eb81686c162d7f3fd28ed5b7374f0046c48')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  tar -xzf "${_realname}-${pkgver}.tar.gz" || true
  cd "${_realname}-${pkgver}"

  # like upstream does for release binaries
  sed -i 's|#lto|lto|;s|#codegen-units|codegen-units|' Cargo.toml

  cargo fetch --locked --target "${RUST_CHOST}"
}

build() {
  cd "${_realname}-${pkgver}"

  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
  cargo build \
    --bin krokiet \
    --bin czkawka_gui \
    --bin czkawka_cli \
    --release \
    --frozen \
    --features "heif libraw libavif"
}

check() {
  cd "${_realname}-${pkgver}"

  cargo test \
    --bin czkawka_cli \
    --bin czkawka_gui \
    --bin krokiet \
    --release \
    --frozen \
    --features "heif libraw libavif"
}

package_czkawka-cli() {
  cd "${_realname}-${pkgver}"

  license=('spdx:MIT')
  pkgdesc+=" (CLI)"

  install -Dm644 czkawka_cli/LICENSE_MIT -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}_cli/"
  install -Dm755 target/release/czkawka_cli.exe "${pkgdir}${MINGW_PREFIX}/bin/czkawka_cli.exe"
}

package_czkawka-gui() {
  cd "${_realname}-${pkgver}"

  depends+=("${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
            "${MINGW_PACKAGE_PREFIX}-glib2"
            "${MINGW_PACKAGE_PREFIX}-gtk4")
  license=('spdx:MIT AND CC-BY-4.0')
  pkgdesc+=" (Desktop App)"

  install -Dm644 czkawka_gui/LICENSE_{MIT_{APP_CODE,WINDOWS_THEME},CC_BY_4_ICONS} \
    -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}_gui/"
  install -Dm755 target/release/czkawka_gui.exe "${pkgdir}${MINGW_PREFIX}/bin/czkawka_gui.exe"
}

package_krokiet() {
  cd "${_realname}-${pkgver}"

  license=('spdx:MIT AND GPL-3.0-or-later AND CC-BY-4.0')
  pkgdesc="New Czkawka frontend written in Slint (mingw-w64)"

  install -Dm644 krokiet/LICENSE_{MIT_CODE,GPL_APP,CC_BY_4_ICONS} \
    -t "${pkgdir}${MINGW_PREFIX}/share/licenses/krokiet/"
  install -Dm755 target/release/krokiet.exe "${pkgdir}${MINGW_PREFIX}/bin/krokiet.exe"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
