# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=gtk2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.24.33
pkgrel=4
pkgdesc="GTK+ is a multi-platform toolkit (v2) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.gtk.org/"

# According to gtk/gtk.h, this is either LGPL2+ or LGPL2.1+, if that makes any
# difference. So let's stick to LGPL2.1+ for safety.
license=(LGPL2.1+)

makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-atk>=1.29.2"
         "${MINGW_PACKAGE_PREFIX}-cairo>=1.6"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2>=2.21.0"
         "${MINGW_PACKAGE_PREFIX}-glib2>=2.28.0"
         "${MINGW_PACKAGE_PREFIX}-pango>=1.20"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info")
#optdepends=("${MINGW_PACKAGE_PREFIX}-gnome-icon-theme: Default icon theme")
install=gtk2-${MSYSTEM}.install
options=(!libtool strip staticlibs)
source=("https://download.gnome.org/sources/gtk+/${pkgver%.*}/gtk+-${pkgver}.tar.xz"
        'gtkrc'
        0001-fix-m4-location.mingw.patch
        0003-Fix-notebook-tab-state-change.mingw.patch
        0005-Fix-header-ref.all.patch
        0006-Fix-unix-symbols-in-docs.mingw.patch
        0009-give-cc-to-gir-scanner.mingw.patch
        0010-put-gtk-dll-into-path.mingw.patch
        0011-gir-for-gdkwin32.mingw.patch
        0012-embed-manifest.all.patch
        0013_fix_mouse_events.patch
        0022-icontheme-win32-detect-SVG-files-by-extension.patch
        0023-gdkkeys-win32.c-fix-initialisation-of-key_state-in-u.patch
        0024-gtk-update-icon-cache-rc-quote.patch)

sha256sums=('ac2ac757f5942d318a311a54b0c80b5ef295f299c2a73c632f6bfb1ff49cc6da'
            'b77a427df55a14182c10ad7e683b4d662df2846fcd38df2aa8918159d6be3ae2'
            '9641e7e7204df7b96448033cd45357ef4a9f60f4ba24dedab772034049dab04b'
            'a11eb8ca1ff7a96b1bda6202c259f85ca0c1b1a489ffa81745dffe329985a5f1'
            '90f5374c6b9b1cbdf59ca71ac3335475f4395af617e2a9bbca2dc7dfe00f6928'
            '07dac87d01d52bfe6c3f57098c0723dbda1bebb1c5b74b38e96e551bd631926c'
            '458840bc6bedeb7503114c89bf9f9f34766b39f9a6bfaec09af411a023c96a31'
            '75f3bd1098c39ccf881508f4c9d23278465bcfe15beaed4bed2af4b00efa00f3'
            '10569e2bc2afe6ab320dcb4b0e708ddd8235244ec0daff05ca518cb8463f2924'
            '4bc70de68084585dcf2b5a7e4abe32a789360fc2be97a9c94ba3b52dac89ad93'
            'f6d731acecf6e35e4ec9739a6d949d2d045c9396c4564283ee6ec935ec690e24'
            'b0f18b85d2fd47057c266887848be1f1c0b6171a1ee6d52a33df7af6e0abb8b0'
            '68f9c7ea1a548a5d700aa4ad17bb961815ad59cc8d05fc1af156ef680da24a15'
            '9c6e2646b22c9dc1725595dbd5fedcaa71ea85ddb5611a1bccc130a00e9e7e30')

prepare() {
  cd gtk+-${pkgver}
  patch -p1 -i ${srcdir}/0001-fix-m4-location.mingw.patch
  patch -p1 -i ${srcdir}/0003-Fix-notebook-tab-state-change.mingw.patch
  patch -p1 -i ${srcdir}/0005-Fix-header-ref.all.patch
  patch -p1 -i ${srcdir}/0006-Fix-unix-symbols-in-docs.mingw.patch
  patch -p1 -i ${srcdir}/0009-give-cc-to-gir-scanner.mingw.patch
  patch -p1 -i ${srcdir}/0010-put-gtk-dll-into-path.mingw.patch
  patch -p1 -i ${srcdir}/0011-gir-for-gdkwin32.mingw.patch
  #patch -p1 -i ${srcdir}/0012-embed-manifest.all.patch
  patch -p1 -i ${srcdir}/0013_fix_mouse_events.patch
  patch -p1 -i ${srcdir}/0022-icontheme-win32-detect-SVG-files-by-extension.patch
  
  # https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/3741
  patch -p1 -i ${srcdir}/0023-gdkkeys-win32.c-fix-initialisation-of-key_state-in-u.patch

  # https://github.com/msys2/MINGW-packages/issues/9130
  patch -p1 -i ${srcdir}/0024-gtk-update-icon-cache-rc-quote.patch

  autoreconf -fi
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  ../gtk+-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-gdktarget=win32 \
    --disable-modules \
    --disable-cups \
    --disable-papi \
    --enable-static \
    --enable-shared \
    --enable-introspection \
    --disable-glibtest \
    --disable-visibility \
    --with-included-immodules=ime

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/gtkrc" "${pkgdir}${MINGW_PREFIX}/share/gtk-2.0/gtkrc.example"
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
  install -Dm644 "${srcdir}/gtk+-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
