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

_realname=glibmm
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.66.8
pkgrel=2
pkgdesc="Glib-- (glibmm) is a C++ interface for glib (V2.66) (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.gtkmm.org/"
msys2_repository_url='https://gitlab.gnome.org/GNOME/glibmm.git'
license=('spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-libsigc++")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://download.gnome.org/sources/${_realname}/${pkgver%.*}/${_realname}-${pkgver}.tar.xz")
sha256sums=('64f11d3b95a24e2a8d4166ecff518730f79ecc27222ef41faf7c7e0340fc9329')

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

  # glibmm_binding test can not be built due to linking error
  sed '/glibmm_binding/d' -i tests/meson.build
}

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

  local -a _meson_options=(
    --prefix=${MINGW_PREFIX}
    --buildtype=plain
    --wrap-mode=nodownload
    --auto-features=enabled
    -Dmaintainer-mode=false
    -Dbuild-documentation=false
    -Dbuild-examples=true
  )

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

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

  CFLAGS+=" -Wno-attributes" \
  CXXFLAGS+=" -Wno-attributes" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    "${_meson_options[@]}" \
    --default-library=shared \
    "${_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() {
  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}-static" --destdir "${pkgdir}"
  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}-shared" --destdir "${pkgdir}"

  sed -s "s|$(cygpath -wm /usr/bin)|/usr/bin|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/glibmm-2.4/proc/gmmproc
  sed -s 's|#!.*|#! /usr/bin/perl|g' -i "${pkgdir}${MINGW_PREFIX}"/lib/glibmm-2.4/proc/gmmproc

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