# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=libgedit-tepl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=6.13.0
pkgrel=2
pkgdesc='Library that eases the development of GtkSourceView-based text editors and IDEs (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gitlab.gnome.org/World/gedit/libgedit-tepl'
license=('spdx:LGPL-3.0-or-later')
depends=(
  "${MINGW_PACKAGE_PREFIX}-cairo"
  "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
  "${MINGW_PACKAGE_PREFIX}-glib2"
  "${MINGW_PACKAGE_PREFIX}-gsettings-desktop-schemas"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
  "${MINGW_PACKAGE_PREFIX}-icu"
  "${MINGW_PACKAGE_PREFIX}-libgedit-amtk"
  "${MINGW_PACKAGE_PREFIX}-libgedit-gfls"
  "${MINGW_PACKAGE_PREFIX}-libgedit-gtksourceview"
  "${MINGW_PACKAGE_PREFIX}-libhandy"
  "${MINGW_PACKAGE_PREFIX}-pango"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-gettext-tools"
  "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
  "${MINGW_PACKAGE_PREFIX}-gtk-doc"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
)
source=("https://gitlab.gnome.org/World/gedit/libgedit-tepl/-/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('39d97a69c6f75a1e86f72976159a4ec3f51996971d1cc5b7289df1315a7d6bab')

build() {
  local -a _static_flags=(
    -DCAIRO_WIN32_STATIC_BUILD
    -DGIO_STATIC_COMPILATION
    -DGLIB_STATIC_COMPILATION
    -DGMODULE_STATIC_COMPILATION
    -DGOBJECT_STATIC_COMPILATION
  )

  local -a _meson_options=(
    --prefix="${MINGW_PREFIX}"
    --wrap-mode=nodownload
    --auto-features=enabled
    --buildtype=plain
  )

  CFLAGS+=" ${_static_flags[@]}" \
  CXXFLAGS+=" ${_static_flags[@]}" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    "${_meson_options[@]}" \
    --default-library=static \
    -Dgtk_doc=false \
    -Dgobject_introspection=false \
    "${_realname}-${pkgver}" \
    "build-${MSYSTEM}-static"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    "${_meson_options[@]}" \
    --default-library=shared \
    -Dgtk_doc=true \
    -Dgobject_introspection=true \
    "${_realname}-${pkgver}" \
    "build-${MSYSTEM}-shared"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}-shared"
}

check() {
  ${MINGW_PREFIX}/bin/meson.exe test -C "build-${MSYSTEM}-static" || warning "Tests failed"
  ${MINGW_PREFIX}/bin/meson.exe test -C "build-${MSYSTEM}-shared" || warning "Tests failed"
}

package_libgedit-tepl() {
  conflicts=("${MINGW_PACKAGE_PREFIX}-tepl")
  replaces=("${MINGW_PACKAGE_PREFIX}-tepl")

  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}-static" --destdir "${pkgdir}"
  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}-shared" --destdir "${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSES/LGPL-3.0-or-later.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/gtk-doc dest${MINGW_PREFIX}/share/gtk-doc
}

package_libgedit-tepl-docs() {
  pkgdesc+=" (documentation)"
  depends=()
  conflicts=()
  replaces=()

  mv dest/* "${pkgdir}"
}

# 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;
