# Maintainer: Philippe Weyland <philippe.weyland@libertysurf.fr>

_realname=gmic
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.6.5
pkgrel=1
pkgdesc="A Full-Featured Open-Source Framework for Image Processing (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gmic.eu"
msys2_repository_url='https://github.com/GreycLab/gmic'
msys2_documentation_url='https://gmic.eu/reference/'
license=('spdx:CECILL-2.1 AND CECILL-C')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-imath"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-opencv"
         "${MINGW_PACKAGE_PREFIX}-openexr")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
# "https://github.com/GreycLab/gmic/releases/download/v.${pkgver}/gmic_${pkgver}.tar.gz"
source=("https://gmic.eu/files/source/gmic_${pkgver}.tar.gz"
        001-building-man-pages.patch)
sha256sums=('0987e54d64dc3a82df6a3052e6aa5d5b5f1e9115c6fd4155e1aceb78e462169a'
            'e7e0ca90fb431708cea2127c686f5f8146792082e3ea3c2cdcaa1e43d45a195c')

prepare() {
  cd ${_realname}-${pkgver}

  patch -p1 -i "${srcdir}"/001-building-man-pages.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_LIB=ON \
    -DBUILD_MAN=OFF \
    -DBUILD_LIB_STATIC=OFF \
    -DBUILD_BASH_COMPLETION=OFF \
    -DENABLE_FFMPEG=ON \
    -DENABLE_FFTW=ON \
    -DENABLE_GRAPHICSMAGICK=ON \
    -DENABLE_JPEG=ON \
    -DENABLE_OPENCV=ON \
    -DENABLE_OPENEXR=ON \
    -DENABLE_OPENMP=ON \
    -DENABLE_PNG=ON \
    -DENABLE_TIFF=ON \
    -DENABLE_ZLIB=ON \
    -DENABLE_DYNAMIC_LINKING=ON \
    ../${_realname}-${pkgver}

  cmake --build .
}

package() {
  cd build-${MSYSTEM}

  DESTDIR="${pkgdir}" cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
