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

_realname=gettext
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.22.3
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.gnu.org/software/gettext/"
pkgdesc="GNU internationalization library (mingw-w64)"
# 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')
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "groff")
options=('strip' 'staticlibs')
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
        0001-printf_parse-fix-off-by-two.patch)
sha256sums=('839a260b2314ba66274dae7d245ec19fce190a3aa67869bf31354cb558df42c7'
            'SKIP'
            'cbc2f533012d646521afa20f8b256917fce040741ff42cf53fb6dd7123a6670a'
            'ef9f11a1cd10539d4323c6fcba3013cc503d47366004fe8a99c642dc3ddf2fd0'
            'a28a27192f336f0b0908bdbf840d3b19d7b587c4ac52cad635cb43e95eb3c78d'
            '380dbddee2f9e2feee4c1435e8a942b5d11d0125e60c3350ebb10c19b19011aa'
            'c354f6a7021069c99b90f1c6d6f6a4ccf40a01e9f6742b866df2b5a7286cb868'
            '4f34906eeb535c74fa3f2936729b59c36d05d503a274e2b850fc770263c60b46'
            'e6187cc1f1190fcb2267e4b811ade0049126e78dd8fd37d3a86a716dfdbf713e')
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 \
    0001-printf_parse-fix-off-by-two.patch

  autoreconf -fiv
}

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

  export lt_cv_deplibs_check_method='pass_all'
  export MSYS2_ARG_CONV_EXCL="-DLOCALEDIR=;-DLIBDIR=;-DLOCALE_ALIAS_PATH="

  # 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} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --disable-java \
    --disable-native-java \
    --disable-csharp \
    --enable-static \
    --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 \
    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
}

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

  # 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() {
  cd ${srcdir}/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

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