# Maintainer: Fabio 'Lolix' Loli <fabio.loli@disroot.org> -> https://github.com/FabioLolix
# Contributor: HurricanePootis <hurricanepootis@protonmail.com>

pkgname=upscayl-ncnn
_pkgver=20251207-174704
_modelver=0.2.5.0
pkgver=20251207.174704_m0.2.5.0
pkgrel=1
pkgdesc="Fork of the NCNN implementation of Real-ESRGAN"
arch=(x86_64)
url="https://github.com/upscayl/upscayl-ncnn"
license=(AGPL-3.0-only)
depends=(libwebp gcc-libs glibc ncnn)
makedepends=(git cmake vulkan-headers glslang shaderc)
source=("git+https://github.com/upscayl/upscayl-ncnn.git#tag=${_pkgver}"
        cmake.patch
        "https://github.com/xinntao/Real-ESRGAN/releases/download/v${_modelver}/realesrgan-ncnn-vulkan-20220424-ubuntu.zip")
b2sums=('b99442918a10a705ad62e6dda8e5c9f82d553b7d4f9c6e163ec6c082e5338400430f7f3cea42e5492a12f431ab5c110f5b9c65bd2280e2965c161f9f7ebc31fb'
        '61080c111a8a19c4a01c7c9dd1d3e5a8ff50d4af975ae5361a7cd5de3aec045750b625596f5e50f770b7ffa94fece8bda0881b67cf0f19e0d969f2b955c04ae2'
        'c16c05c343501272f5b4df6db7e7f8c592c4f8c8170ca0a0181c4433d1889ddea702d65fc134013bdaca302eeeab6e5b6d6fbad407adb7a2afa0acbefd6f4eca')

prepare() {
  cd upscayl-ncnn
  patch -Np1 -i ../cmake.patch # fail to detect system's glslang
}

build() {
  local _flags=(
	-DBUILD_SHARED_LIBS=0
	-DUSE_SYSTEM_NCNN=1
	-DUSE_SYSTEM_WEBP=1
    #-DGLSLANG_TARGET_DIR=/usr/lib/cmake/glslang
  )

  cmake -B build -S "upscayl-ncnn/src" -Wno-dev \
    -DCMAKE_BUILD_TYPE=Rlease \
    -DCMAKE_INSTALL_PREFIX=/usr \
    "${_flags[@]}"

  cmake --build build
}

package() {
  #install -Dm755 build/upscayl-bin -t "${pkgdir}/usr/bin/"
  #ln -s /usr/bin/upscayl-bin ${pkgdir}/usr/bin/upscayl

  install -Dm755 build/upscayl-bin "${pkgdir}/usr/lib/upscayl/upscayl"

  install -d "${pkgdir}/usr/bin"
  ln -s /usr/lib/upscayl/upscayl "${pkgdir}/usr/bin/upscayl"

  # Upscaler want the upstream binary name
  ln -s /usr/lib/upscayl/upscayl "${pkgdir}/usr/bin/upscayl-bin"

  install -d "${pkgdir}/usr/lib/upscayl/models"
  cp -r models/* "${pkgdir}/usr/lib/upscayl/models"
}
