#!/bin/sh
set -e

# libffi?
# libiconv?

fsdeps/dep/libintl-static

PACKAGE=glib-2.66.7
CHECKSUM=09f158769f6f26b31074e15b1ac80ec39b13b53102dfae66cfe826fb2cc65502
REVISION=4

. fsdeps/dep.sh

# FIXME: Would be nice to extract 2.66 from PACKAGE
python3 fsdeps/download.py \
https://download.gnome.org/sources/glib/2.66/$PACKAGE.tar.xz \
sha256:$CHECKSUM

rm -Rf fsdeps/_build && mkdir fsdeps/_build && cd fsdeps/_build
tar xf ../_sources/$PACKAGE.tar.xz
cd $PACKAGE

if [ $SYSTEM_OS = "Windows" ]; then

patch -p1 <<EOF
diff --git a/gio/tests/meson.build b/gio/tests/meson.build
index dca33bd44..47a95b7b3 100644
--- a/gio/tests/meson.build
+++ b/gio/tests/meson.build
@@ -406,7 +406,8 @@ if installed_tests_enabled
   install_subdir('cert-tests', install_dir : installed_tests_execdir)
 endif
 
-if not meson.is_cross_build() or meson.has_exe_wrapper()
+windows_static = host_system == 'windows' and get_option('default_library') == 'static'
+if (not meson.is_cross_build() or meson.has_exe_wrapper()) and not windows_static
 
   plugin_resources_c = custom_target('plugin-resources.c',
     input : 'test4.gresource.xml',
EOF

patch -p1 <<EOF
From 7cf2af987f9a2379b28f5d3426e9dc6633fa29a5 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Sat, 6 Jan 2018 22:04:23 +0100
Subject: [PATCH] win32: Make the static build work with MinGW when posix
 threads are used

MinGW does not support the use of DllMain() for static builds, but that
is currently always used on Windows, partly because it is needed for
handling win32 threads and because there are problems with MSVC
optimizing constructors away (see 7a29771a743a8b5337).

To make the static build at least work in case mingw+posix threads are used,
switch to using constructors for that. The g_clock_win32_init() call is
moved into glib_init(), so it's also called in that case.

If mingw+static+win32 threads are used abort the build early and print
an error message.

https://bugzilla.gnome.org/show_bug.cgi?id=792297
---
 glib/glib-init.c | 15 ++++++++++++---
 gobject/gtype.c  |  2 +-
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/glib/glib-init.c b/glib/glib-init.c
index ed800dca1..4866b5239 100644
--- a/glib/glib-init.c
+++ b/glib/glib-init.c
@@ -264,19 +264,29 @@ glib_init (void)
 
   glib_inited = TRUE;
 
+#ifdef G_OS_WIN32
+  g_clock_win32_init ();
+#endif
   g_messages_prefixed_init ();
   g_debug_init ();
   g_quark_init ();
 }
 
 #if defined (G_OS_WIN32)
+HMODULE glib_dll;
+#endif
+
+#if defined(__MINGW32__) && defined(GLIB_STATIC_COMPILATION) && !defined(THREADS_POSIX)
+/* MinGW static builds do not work with DllMain, but win32 threads need it atm */
+#error "Static build under MinGW only supported when build with posix threads"
+#endif
+
+#if defined (G_OS_WIN32) && (!defined(__MINGW32__) || defined(DLL_EXPORT))
 
 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
                      DWORD     fdwReason,
                      LPVOID    lpvReserved);
 
-HMODULE glib_dll;
-
 BOOL WINAPI
 DllMain (HINSTANCE hinstDLL,
          DWORD     fdwReason,
@@ -287,7 +297,6 @@ DllMain (HINSTANCE hinstDLL,
     case DLL_PROCESS_ATTACH:
       glib_dll = hinstDLL;
       g_crash_handler_win32_init ();
-      g_clock_win32_init ();
 #ifdef THREADS_WIN32
       g_thread_win32_init ();
 #endif
diff --git a/gobject/gtype.c b/gobject/gtype.c
index 425c6195d..91cd7e2cf 100644
--- a/gobject/gtype.c
+++ b/gobject/gtype.c
@@ -4456,7 +4456,7 @@ gobject_init (void)
   _g_signal_init ();
 }
 
-#if defined (G_OS_WIN32)
+#if defined (G_OS_WIN32) && (!defined(__MINGW32__) || defined(DLL_EXPORT))
 
 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
                      DWORD     fdwReason,
-- 
2.22.0

EOF

fi

if [ $SYSTEM_OS = "macOS" ]; then
patch -p1 <<EOF
--- a/meson.build	2021-02-11 13:24:55.000000000 +0100
+++ b/meson.build	2021-11-02 13:47:12.000000000 +0100
@@ -1998,46 +1998,12 @@
   endif
 endif
 
-# First check in libc, fallback to libintl, and as last chance build
-# proxy-libintl subproject.
-# FIXME: glib-gettext.m4 has much more checks to detect broken/uncompatible
-# implementations. This could be extended if issues are found in some platforms.
 libintl_deps = []
-if cc.has_function('ngettext')
-  have_bind_textdomain_codeset = cc.has_function('bind_textdomain_codeset')
-else
-  # First just find the bare library.
-  libintl = cc.find_library('intl', required : false)
-  # The bare library probably won't link without help if it's static.
-  if libintl.found() and not cc.has_function('ngettext', dependencies : libintl)
-     libintl_iconv = cc.find_library('iconv', required : false)
-     # libintl supports different threading APIs, which may not
-     # require additional flags, but it defaults to using pthreads if
-     # found. Meson's "threads" dependency does not allow you to
-     # prefer pthreads. We may not be using pthreads for glib itself
-     # either so just link the library to satisfy libintl rather than
-     # also defining the macros with the -pthread flag.
-     libintl_pthread = cc.find_library('pthread', required : false)
-     # Try linking with just libiconv.
-     if libintl_iconv.found() and cc.has_function('ngettext', dependencies : [libintl, libintl_iconv])
-       libintl_deps += [libintl_iconv]
-     # Then also try linking with pthreads.
-     elif libintl_iconv.found() and libintl_pthread.found() and cc.has_function('ngettext', dependencies : [libintl, libintl_iconv, libintl_pthread])
-       libintl_deps += [libintl_iconv, libintl_pthread]
-     else
-       libintl = disabler()
-     endif
-  endif
-  if not libintl.found()
-    libintl = subproject('proxy-libintl').get_variable('intl_dep')
-    libintl_deps = [libintl] + libintl_deps
-    have_bind_textdomain_codeset = true  # proxy-libintl supports it
-  else
-    libintl_deps = [libintl] + libintl_deps
-    have_bind_textdomain_codeset = cc.has_function('bind_textdomain_codeset',
-                                                   dependencies : libintl_deps)
-  endif
-endif
+libintl = cc.find_library('intl', required : false)
+# libintl_iconv = cc.find_library('iconv', required : false)
+# libintl_deps += [libintl_iconv]
+libintl_deps = [libintl] + libintl_deps
+have_bind_textdomain_codeset = true
 
 glib_conf.set('HAVE_BIND_TEXTDOMAIN_CODESET', have_bind_textdomain_codeset)
 

EOF
fi

# python3 ../../replace.py \
# gio/tests/meson.build \
# "if not meson.is_cross_build() or meson.has_exe_wrapper()" \
# "if false"
# replace meson.build "subdir('po')" ""

if [ $SYSTEM_OS = "Windows" ]; then
meson \
	--prefix $PREFIX \
	--default-library static \
	--libdir lib \
	-Dinternal_pcre=true \
	-Dforce_posix_threads=true \
	_build
else
meson \
	--prefix $PREFIX \
	--default-library static \
	--libdir lib \
	-Dinternal_pcre=true \
	_build
fi
ninja -C _build
ninja -C _build install

if [ $SYSTEM_OS = "Windows" ]; then
# Add more libraries needed for static linking (without needing --static
# flag to pkgconfig)
sed -i.tmp \
	"s/-lwinmm -lm/-lwinmm -lm -lole32 -lshlwapi/g" \
	$PREFIX/lib/pkgconfig/glib-2.0.pc
fi

touch $INSTALLED
