# 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.19.8.1
pkgrel=10
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="GNU internationalization library (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         #"${MINGW_PACKAGE_PREFIX}-termcap"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-ncurses" "git")
options=('strip' 'staticlibs')

# GPL3 for the package as a whole and LGPL for some parts, see the license files
license=(GPL3 partial:'LGPL2.1')

url="https://www.gnu.org/software/gettext/"
source=("https://ftp.gnu.org/pub/gnu/${_realname}/${_realname}-${pkgver}.tar.gz"
        00-relocatex-libintl-0.18.3.1.patch
        120-Fix-Woe32-link-errors-when-compiling-with-O0.patch
        04-mingw-script-slash-fix.mingw.patch
        05-always-use-libintl-vsnprintf.mingw.patch
        06-dont-include-ctype-after-gnulibs-wctype.mingw.patch
        07-fix-asprintf-conflict.mingw.patch
        08-vs-compatible.patch
        09-asm-underscore-mingw.patch
        121-keep-posix-path.patch
        122-Use-LF-as-newline-in-envsubst.patch
        200-tools-gnulib-define-installdir.patch)
sha256sums=('ff942af0e438ced4a8b0ea4b0b6e0d6d657157c5e2364de57baa279c1c125c43'
            '030987c317279c27eea503ef257c7c3cf4ff947de8ea71d84fdf5eea802ee587'
            '8f51c320c104c94e1ab3c8630905185214ca0c7ecf5423f20001c8694ead454c'
            'bebc791443a739f91ace5bca1dba3d45d50dff92706fae7629d150e197d2f411'
            '51604823755d1ae669544bb9730c5a09b78b0f1b1978577c8604fbd6cafc073e'
            '25aee9534c45772370ef5ca495da0d6a9f4d73e7a53d6ba91314883c6f3bb695'
            '3b85e4d0b771b47a168c629a65463f5b87a5d5627b5f05000a45c3d2df96b66d'
            '522715ac22936943a85b4b78274302a6058f4f5371439cf491193ed53d8fc729'
            '2ffc73f1b8d66d88ff5ce640be211e5a927daba13788cb2319b97fc885444eac'
            '051bf975687a92da8a5acc09a367632396570c2609c5ecc1eba06c60c7135ad6'
            '2abfa598e1586abe14e982b867c8981790d8114e1ee575cb08b7ed49d4a46c74'
            '71eb3554cbf37a8c866722e39c6102c3ae3a215b82e153000f61f2e80d3bfd1b')
validpgpkeys=('462225C3B46F34879FC8496CD605848ED7E69871') #Daiki Ueno

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  rm -rf gettext-runtime/intl/canonicalize.c \
         gettext-runtime/intl/canonicalize.h \
         gettext-runtime/intl/relocatex.c \
         gettext-runtime/intl/relocatex.h \
         gettext-tools/woe32dll/c++color.cc \
         gettext-tools/woe32dll/c++file-ostream.cc \
         gettext-tools/woe32dll/c++html-ostream.cc \
         gettext-tools/woe32dll/c++styled-ostream.cc \
         gettext-tools/woe32dll/c++term-ostream.cc \
         gettext-tools/woe32dll/c++write-catalog.cc \
         gettext-tools/woe32dll/c++write-po.cc \
         gettext-tools/woe32dll/c++write-properties.cc \
         gettext-tools/woe32dll/c++write-stringtable.cc \
         MINGW-PATCHES/README-relocatex-libintl.txt || true

  patch -p1 -i ${srcdir}/00-relocatex-libintl-0.18.3.1.patch
  patch -p0 -i ${srcdir}/04-mingw-script-slash-fix.mingw.patch
  patch -p0 -i ${srcdir}/05-always-use-libintl-vsnprintf.mingw.patch
  patch -p0 -i ${srcdir}/06-dont-include-ctype-after-gnulibs-wctype.mingw.patch
  patch -p0 -i ${srcdir}/07-fix-asprintf-conflict.mingw.patch
  patch -p0 -i ${srcdir}/08-vs-compatible.patch
  patch -p1 -i ${srcdir}/09-asm-underscore-mingw.patch
  patch -p1 -i ${srcdir}/120-Fix-Woe32-link-errors-when-compiling-with-O0.patch
  #patch -p1 -i ${srcdir}/121-keep-posix-path.patch
  patch -p1 -i ${srcdir}/122-Use-LF-as-newline-in-envsubst.patch
  patch -p1 -i ${srcdir}/200-tools-gnulib-define-installdir.patch

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

build() {
  export lt_cv_deplibs_check_method='pass_all'

  export MSYS2_ARG_CONV_EXCL="-DLOCALEDIR=;-DLIBDIR=;-DLOCALE_ALIAS_PATH="
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  ../${_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

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

  make V=1
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  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"
  rm "${pkgdir}${MINGW_PREFIX}/share/${_realname}/intl/COPYING.LIB"
}
