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

_realname=gettext
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-runtime" "${MINGW_PACKAGE_PREFIX}-${_realname}-tools")
pkgver=0.22.5
pkgrel=2
pkgdesc="GNU internationalization library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.gnu.org/software/gettext/"
# GPL3 for the package as a whole and LGPL for some parts, see the license files
license=('spdx:GPL-3.0-or-later AND LGPL-2.1-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-libiconv"
             "groff")
source=(https://ftp.gnu.org/pub/gnu/${_realname}/${_realname}-${pkgver}.tar.gz{,.sig}
        0005-Fix-compilation-of-pthread_sigmask.c.patch
        122-Use-LF-as-newline-in-envsubst.patch
        gettext-0.22-disable-libtextstyle.patch
        0021-replace-fsync.patch
        0022-libasprintf.patch
        0023-gnulib.patch
        0024-disable-gnu-format.patch)
sha256sums=('ec1705b1e969b83a9f073144ec806151db88127f5e40fe5a94cb6c8fa48996a0'
            'SKIP'
            'cbc2f533012d646521afa20f8b256917fce040741ff42cf53fb6dd7123a6670a'
            'ef9f11a1cd10539d4323c6fcba3013cc503d47366004fe8a99c642dc3ddf2fd0'
            'a28a27192f336f0b0908bdbf840d3b19d7b587c4ac52cad635cb43e95eb3c78d'
            '380dbddee2f9e2feee4c1435e8a942b5d11d0125e60c3350ebb10c19b19011aa'
            'c354f6a7021069c99b90f1c6d6f6a4ccf40a01e9f6742b866df2b5a7286cb868'
            '4f34906eeb535c74fa3f2936729b59c36d05d503a274e2b850fc770263c60b46'
            'ce7ccf6dd3a492cab322253cd67310899b546eccc25821c25cbc047a1a984633')
validpgpkeys=('462225C3B46F34879FC8496CD605848ED7E69871'  # Daiki Ueno
              '9001B85AF9E1B83DF1BDA942F5BE8B267C6A406D') # Bruno Haible

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

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

  apply_patch_with_msg \
    0005-Fix-compilation-of-pthread_sigmask.c.patch \
    122-Use-LF-as-newline-in-envsubst.patch

  # https://gitlab.archlinux.org/archlinux/packaging/packages/gettext/-/blob/c85956ab0dc426c674551fd2bf977a96089752e9/gettext-0.22-disable-libtextstyle.patch
  apply_patch_with_msg \
    gettext-0.22-disable-libtextstyle.patch

  apply_patch_with_msg \
    0021-replace-fsync.patch \
    0022-libasprintf.patch \
    0023-gnulib.patch

  # https://github.com/msys2/MINGW-packages/pull/19047#issuecomment-1960769618
  # https://git.savannah.gnu.org/gitweb/?p=gettext.git;a=commitdiff;h=eef429bbccbff2bca08bafc2ff76f7ec2be93d2a
  apply_patch_with_msg \
    0024-disable-gnu-format.patch

  libtoolize --automake --copy --force
  WANT_AUTOMAKE=latest ./autogen.sh --skip-gnulib
}

_build() {
  _config_opt=$1

  # gl_cv_func_mkdir_trailing_dot_works=yes was added to avoid having two
  # incompatible declarations of mkdir in the same compilation unit.
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --disable-java \
    --disable-native-java \
    --disable-csharp \
    ${_config_opt} \
    --enable-threads=win32 \
    --enable-relocatable \
    --without-emacs \
    --disable-openmp \
    --without-cvs \
    --without-git \
    --with-included-libcroco \
    --with-included-libunistring \
    --with-included-libxml \
    --with-included-glib \
    --with-libncurses-prefix=${MINGW_PREFIX} \
    --disable-silent-rules \
    lt_cv_deplibs_check_method='pass_all' \
    gl_cv_func_mkdir_trailing_dot_works=yes

  # to make relocate() in gnulib-lib work
  sed -s "s|${MINGW_PREFIX}|$(cygpath -m ${MINGW_PREFIX})|g" -i gettext-tools/config.h

  make
}

build() {

  export MSYS2_ARG_CONV_EXCL="-DLOCALEDIR=;-DLIBDIR=;-DLOCALE_ALIAS_PATH="

  msg2 "Build static libraries"
  mkdir -p ${srcdir}/build-${MSYSTEM}-static && cd ${srcdir}/build-${MSYSTEM}-static
  _build "--enable-static --disable-shared"

  msg2 "Build shared libraries"
  mkdir -p ${srcdir}/build-${MSYSTEM}-shared && cd ${srcdir}/build-${MSYSTEM}-shared
  _build "--enable-shared --disable-static"
}

check () {
  cd ${srcdir}/build-${MSYSTEM}-static

  # ensure that the Windows-specific `%I64*` format family is recognized correctly
  cat >test-I64d.c <<EOF
#include <stdio.h>
#include <stdint.h>
#include <libintl.h>

int main(int argc, char **argv)
{
	char buf[1024] = "(failure)";
	int len = libintl_snprintf(buf, 1024, "hello %llu %I64u 0:%d\n", (uint64_t)123, (uint64_t)123, 0);

	printf("%d, '%s'\n", len, buf);

	return len < 0 ? 1 : 0;
}
EOF
  gcc -Wall -g -o test-I64d.exe test-I64d.c ./gettext-runtime/intl/.libs/libintl.a || return 1
  ./test-I64d.exe || return 1
}

package_gettext-runtime() {
  pkgdesc="GNU internationalization runtime library (mingw-w64)"
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gcc-libs"
    "${MINGW_PACKAGE_PREFIX}-libiconv"
  )
  conflicts=("${MINGW_PACKAGE_PREFIX}-gettext<=0.22.4-3")

  cd ${srcdir}/build-${MSYSTEM}-static/gettext-runtime
  make DESTDIR="${pkgdir}" install

  cd ${srcdir}/build-${MSYSTEM}-shared/gettext-runtime
  make DESTDIR="${pkgdir}" install

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-runtime/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/gettext-runtime/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-runtime/intl/COPYING.LIB" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/gettext-runtime/intl/COPYING.LIB"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-runtime/libasprintf/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/gettext-runtime/libasprintf/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-runtime/libasprintf/COPYING.LIB" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/gettext-runtime/libasprintf/COPYING.LIB"
}

package_gettext-tools() {
  pkgdesc="GNU internationalization tools (mingw-w64)"
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
    "${MINGW_PACKAGE_PREFIX}-gcc-libs"
    "${MINGW_PACKAGE_PREFIX}-libiconv"
  )
  provides=("${MINGW_PACKAGE_PREFIX}-gettext=${pkgver}-${pkgrel}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-gettext<=0.22.4-3")
  replaces=("${MINGW_PACKAGE_PREFIX}-gettext")

  cd ${srcdir}/build-${MSYSTEM}-static/gettext-tools
  make DESTDIR="${pkgdir}" install

  cd ${srcdir}/build-${MSYSTEM}-shared/gettext-tools
  make DESTDIR="${pkgdir}" install

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-tools/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/gettext-tools/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gettext-tools/gnulib-lib/libxml/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/gettext-tools/gnulib-lib/libxml/COPYING"

  # Not sure where to put them, so put them in the gettext-tools package
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/gnulib-local/lib/libxml/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/gnulib-local/lib/libxml/COPYING"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
