# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ignacio Casal Quinteiro <icq@gnome.org>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=glib2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.72.1
pkgrel=2
url="https://gitlab.gnome.org/GNOME/glib"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="Common C routines used by GTK+ 3 and other libs (mingw-w64)"
license=(LGPL2)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('strip' '!debug' 'staticlibs')
source=("https://download.gnome.org/sources/glib/${pkgver%.*}/glib-${pkgver}.tar.xz"
        0001-Update-g_fopen-g_open-and-g_creat-to-open-with-FILE_.patch
        0002-disable_glib_compile_schemas_warning.patch
        0003-gtestutils-include-stdlib.patch
        glib-compile-schemas.hook.in
        gio-querymodules.hook.in
        pyscript2exe.py)
sha256sums=('c07e57147b254cef92ce80a0378dc0c02a4358e7de4702e9f403069781095fe2'
            '51d02360a1ee978fd45e77b84bca9cfbcf080d91986b5c0efe0732779c6a54ec'
            '396c25cfd740ffbb72209133c7b9453173167577265a4bb14502678de8d5a8f9'
            '3c91273acd9c836adbc3fd7d175368d6c484984305979c1c9254140d949b8bb7'
            '0c3d54636407e0e13429b73959cace626253cd772e1d4870de0fb92b0b99545a'
            'f18d27d98709dba8c5f9756672baaf0158fb4353c9edbdc2e80021f88ff34ced'
            'e57174517b08cf8f9fb4f43a381d342d23d2db3cad661107f35ca21c39b5734d')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Np1 -i "${srcdir}/${_patch}"
  done
}


prepare() {
  cd "${srcdir}/glib-${pkgver}"

  apply_patch_with_msg 0001-Update-g_fopen-g_open-and-g_creat-to-open-with-FILE_.patch
  apply_patch_with_msg 0002-disable_glib_compile_schemas_warning.patch

  # https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2613
  apply_patch_with_msg 0003-gtestutils-include-stdlib.patch
}

build() {
  msg "Build static version"
  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("--buildtype=release")
  else
    extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe \
    -Dlibelf=disabled \
    --prefix="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    --default-library=static \
    "../glib-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile

  msg "Build shared version"
  [[ -d "${srcdir}"/build-${CARCH}-shared ]] && rm -rf "${srcdir}"/build-${CARCH}-shared
  mkdir "${srcdir}"/build-${CARCH}-shared && cd "${srcdir}"/build-${CARCH}-shared

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe \
    -Dlibelf=disabled \
    --prefix="${MINGW_PREFIX}" \
    "${extra_config[@]}" \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    -Ddefault_library=shared \
    -Dman=true \
    -Dgtk_doc=false \
    "../glib-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile
}

package() {
  cd "${srcdir}/build-${CARCH}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install

  cd "${srcdir}/build-${CARCH}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install

  for hook in glib-compile-schemas gio-querymodules; do
    local hook_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-${hook}.hook";
    cp "${srcdir}/${hook}.hook.in" "${hook_path}"
    sed -s "s|@MINGW_HOOK_TARGET_PREFIX@|${MINGW_PREFIX:1}|g" -i "${hook_path}"
    sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${hook_path}"
    install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 "${hook_path}"
  done

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

  for name in glib-mkenums glib-genmarshal gdbus-codegen gtester-report; do
    ${MINGW_PREFIX}/bin/python3 \
      "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/${name}"
  done
}
