# Maintainer: Sutou Kouhei <kou@clear-code.com>

_realname=gr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.73.19
pkgrel=1
pkgdesc="A graphics library for visualisation applications (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gr-framework.org/"
msys2_repository_url="https://github.com/sciapp/gr"
msys2_references=(
  'aur: gr-framework'
  'gentoo: sci-visualization/gr'
)
license=("spdx:MIT")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-glfw"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zeromq")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/sciapp/gr/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0002-missing-cmake-dep.patch")
sha256sums=('70dc02ca46230d1a5b7e63a8054f1c740cbbfa819e398c1d305d71668e31c1f4'
            'a57fc0c01f8c3a241cb01ac293034a2e28f631264d506c6988ccd1f3a7f54c79')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  # We can't use bsdtar to extract the archive because the archive includes
  # symbolic link.
  rm -rf "${_realname}-${pkgver}"
  tar -xf "${_realname}-${pkgver}.tar.gz" || true

  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 < "${srcdir}/0002-missing-cmake-dep.patch"

  # Set version manually, otherwise it resets to zero
  echo "${pkgver}" > version.txt
  sed -i "s|\${GR_VERSION}|${pkgver}|g" CMakeLists.txt
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/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 \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DGR_USE_BUNDLED_LIBRARIES=OFF \
    "${_extra_config[@]}" \
    ../${_realname}-${pkgver}

  cmake --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.md "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
