# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=glib2
pkgname=(glib2 glib2-devel)
pkgver=2.86.0
pkgrel=1
pkgdesc="Common C routines used by GTK+ and other libs"
license=('spdx:LGPL-2.1-or-later')
url="https://gitlab.gnome.org/GNOME/glib"
msys2_references=(
  "cpe: cpe:/a:gnome:glib"
  'cygwin: glib2.0'
)
arch=('i686' 'x86_64')
makedepends=('gcc'
             'libffi-devel'
             'libiconv-devel'
             'gettext-devel'
             'pcre2-devel'
             'pkgconf'
             'python'
             'python-packaging'
             'meson'
             'ninja'
             'zlib-devel'
             'python-docutils')
source=(https://download.gnome.org/sources/glib/${pkgver%.*}/glib-$pkgver.tar.xz
        001-gmodule-lib-prefix.patch)
noextract=("glib-$pkgver.tar.xz")
sha256sums=('b5739972d737cfb0d6fd1e7f163dfe650e2e03740bb3b8d408e4d1faea580d6d'
            '31bce1aff2dab2262ea372d101081162a77e9310fe0d68400da8c85d698ce65e')

prepare() {
  bsdtar -zxf "glib-${pkgver}.tar.xz" || true

  cd glib-${pkgver}

  patch -p1 -i ${srcdir}/001-gmodule-lib-prefix.patch
}

build() {
  mkdir "${srcdir}"/build && cd "${srcdir}"/build

  meson setup \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    --buildtype=plain \
    --prefix=/usr \
    -Dlibelf=disabled \
    -Dintrospection=disabled \
    -Ddtrace=disabled \
    -Dsystemtap=disabled \
    -Dsysprof=disabled \
    -Dglib_debug=disabled \
    "../glib-${pkgver}"

  meson compile

  DESTDIR="${srcdir}/dest" meson install

  pushd ${srcdir}/dest/usr/share/gdb/auto-load/usr/lib > /dev/null
    mv libglib* msys-glib-2.0-0.dll-gdb.py
    mv libgobject* msys-gobject-2.0-0.dll-gdb.py
  popd > /dev/null
}

check() {
  cd "${srcdir}"/build

  meson test
}

package_glib2() {
  depends=('libpcre2_8' 'libffi' 'libiconv' 'zlib' 'libintl')
  conflicts=('glib2-devel<2.80.3')
  options=('!docs' '!emptydirs')
  license=('LGPL')

  mkdir -p ${pkgdir}/usr/{bin,lib,libexec,share}
  mkdir -p ${pkgdir}/usr/share/man/man1
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/libexec/* ${pkgdir}/usr/libexec/
  for file in gapplication gdbus gio-querymodules glib-compile-schemas gsettings gresource gio; do
    cp -f ${srcdir}/dest/usr/bin/${file}.exe ${pkgdir}/usr/bin/
    cp -f ${srcdir}/dest/usr/share/man/man1/${file}.1 ${pkgdir}/usr/share/man/man1/
  done

  cp -rf ${srcdir}/dest/usr/share/gdb ${pkgdir}/usr/share/

  mkdir -p ${pkgdir}/usr/share/glib-2.0
  cp -rf ${srcdir}/dest/usr/share/glib-2.0/gdb ${pkgdir}/usr/share/glib-2.0/
  cp -rf ${srcdir}/dest/usr/share/glib-2.0/schemas ${pkgdir}/usr/share/glib-2.0/
  cp -rf ${srcdir}/dest/usr/share/locale ${pkgdir}/usr/share/

  cp -rf ${srcdir}/dest/usr/share/bash-completion ${pkgdir}/usr/share/

  for _i in "${pkgdir}/usr/share/bash-completion/completions/"*; do
    chmod -x "$_i"
  done

  python3 -m compileall -o 0 -o 1 -s "${pkgdir}" "${pkgdir}/usr/share"
}

package_glib2-devel() {
  pkgdesc="glib2 headers and libraries"
  groups=('development')
  depends=("glib2=${pkgver}" 'gettext-devel' 'libffi-devel' 'libiconv-devel' 'pcre2-devel' 'zlib-devel')
  conflicts=('glib2<2.80.3')
  optdepends=(
    'python: for gdbus-codegen and gtester-report'
    'python-packaging: for gdbus-codegen'
  )
  options=('emptydirs')

  mkdir -p ${pkgdir}/usr/{bin,lib,share}

  cp -f ${srcdir}/dest/usr/bin/gobject-query ${pkgdir}/usr/bin/
  cp -f ${srcdir}/dest/usr/bin/gdbus-codegen ${pkgdir}/usr/bin/
  cp -f ${srcdir}/dest/usr/bin/glib-* ${pkgdir}/usr/bin/
  rm -f ${pkgdir}/usr/bin/glib-compile-schemas*
  cp -f ${srcdir}/dest/usr/bin/gtester* ${pkgdir}/usr/bin/

  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib/
  cp -rf ${srcdir}/dest/usr/lib/glib* ${pkgdir}/usr/lib/

  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share/aclocal ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/glib-2.0 ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/gettext ${pkgdir}/usr/share/
  rm -rf ${pkgdir}/usr/share/glib-2.0/gdb
  rm -rf ${pkgdir}/usr/share/glib-2.0/schemas

  mkdir -p ${pkgdir}/usr/share/man/man1
  for file in gdbus-codegen.1 glib-compile-resources.1 glib-genmarshal.1 glib-gettextize.1 \
              glib-mkenums.1 gtester-report.1 gtester.1; do
    cp -f ${srcdir}/dest/usr/share/man/man1/${file} ${pkgdir}/usr/share/man/man1/
  done

  python3 -m compileall -o 0 -o 1 -s "${pkgdir}" "${pkgdir}/usr/share/glib-2.0/codegen"
}
