# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=gettext
pkgname=('gettext' 'gettext-devel' 'libgettextpo' 'libasprintf' 'libintl')
pkgver=0.22.5
pkgrel=1
pkgdesc="GNU internationalization library"
arch=('i686' 'x86_64')
url="https://www.gnu.org/software/gettext/"
msys2_references=(
  "cpe: cpe:/a:gnu:gettext"
)
license=('GPL')
makedepends=('groff' 'libiconv-devel' 'autotools' 'gcc')
options=(!docs)
source=(https://ftp.gnu.org/pub/gnu/gettext/${pkgname}-${pkgver}.tar.gz{,.sig}
        gettext-tools-tests-locale-ll-es.patch
        gettext-0.21.1-autopoint-V.patch
        gettext-0.21.1-cygwin-ftm.patch
        gettext-0.22-no-woe32dll-gettext-tools-configure-ac.patch
        gettext-0.22-no-woe32dll-m4-woe32-dll-m4.patch
        gettext-0.22.5-gettext-runtime-gnulib-lib-localcharset-c.patch
        gettext-0.22.5-gettext-runtime-gnulib-lib-localename-h.patch
        gettext-0.22.5-gettext-runtime-gnulib-lib-localename-unsafe-c.patch
        gettext-0.22.5-gettext-runtime-intl-gnulib-lib-localcharset-c.patch
        gettext-0.22.5-gettext-runtime-intl-gnulib-lib-localename-h.patch
        gettext-0.22.5-gettext-runtime-intl-gnulib-lib-localename-unsafe-c.patch
        gettext-0.22.5-gettext-tools-gnulib-lib-localcharset-c.patch
        gettext-0.22.5-gettext-tools-gnulib-lib-localename-h.patch
        gettext-0.22.5-gettext-tools-gnulib-lib-localename-unsafe-c.patch
        gettext-0.22-disable-libtextstyle.patch
        gettext-0.19.8.1-msys2.patch
        gettext-0.19.7-archive.patch)
sha256sums=('ec1705b1e969b83a9f073144ec806151db88127f5e40fe5a94cb6c8fa48996a0'
            'SKIP'
            'a83c2e89aa799232847d120ce3da856fbee2fda87245233d336c5de044465321'
            '2a8ea083abf7278b3b6157e6f3eed4e6a90b803308396e30b1302085b840c8e9'
            '62c0cf906df4c0196e2e3e4f79b91486f6a95d29bf9de8aed8c92b5075058497'
            '71f26d49ddabf400ee14ea574495a5d3b3055957d81fecfb4cb5cb99dab3cd47'
            'd5b14b03ca609764f1c646efa9e85dedafe69f967f37b8db194fcddc199f2d97'
            'b65bbf78a55a3b8a048a89f8351b3daeff79058d8c45318f8642f07b7cb74f5a'
            'e1e5a2e50d23d9e16b4f33dfeeab33f193d7f7678a560db9a84fcad81dd33369'
            'd05a8da45d4aa90e5c412554d198b84845d34c60aa5ae1fa1167b5a551828f3f'
            '9774ca475c280ba6641807063b79420a7a350a363154b7e20fb6307096f031ed'
            '5060b449ac30fe3699e284bd633644d8a82faaa2b4e6516fb38b69b616972ca9'
            'd15bd9341df9653603b6cb7de962ce47eff3a557368508c7f57240acca7057e1'
            'b43e41f10e123ce449b0fc7224dfde18da122cb95897198f54175fe30f41f05e'
            '082edeacdea36bad6f8901e276ee38876667ed79366d6745264cf905f1031042'
            '5a5ee0d8a72eb056489c1881c8e2b183e128e96a6dc5cc9619286626223c226e'
            'a28a27192f336f0b0908bdbf840d3b19d7b587c4ac52cad635cb43e95eb3c78d'
            '1f6d2a340676543f1565416307ecf00d0c4ce68b2b3519f4ba9499d37c045a91'
            '5e0851e372d78aac8ec20be40b13b2c010e45c1703d676711d793ae86ca82fd5')
validpgpkeys=('462225C3B46F34879FC8496CD605848ED7E69871'  # Daiki Ueno
              '9001B85AF9E1B83DF1BDA942F5BE8B267C6A406D') # Bruno Haible (Open Source Development)

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

  # Cygwin patches
  patch -p1 -i $srcdir/gettext-tools-tests-locale-ll-es.patch
  patch -p1 -i $srcdir/gettext-0.21.1-autopoint-V.patch
  patch -p1 -i $srcdir/gettext-0.21.1-cygwin-ftm.patch
  patch -p1 -i $srcdir/gettext-0.22-no-woe32dll-gettext-tools-configure-ac.patch
  patch -p1 -i $srcdir/gettext-0.22-no-woe32dll-m4-woe32-dll-m4.patch
  patch -p1 -i $srcdir/gettext-0.22.5-gettext-runtime-gnulib-lib-localcharset-c.patch
  patch -p1 -i $srcdir/gettext-0.22.5-gettext-runtime-gnulib-lib-localename-h.patch
  patch -p1 -i $srcdir/gettext-0.22.5-gettext-runtime-gnulib-lib-localename-unsafe-c.patch
  patch -p1 -i $srcdir/gettext-0.22.5-gettext-runtime-intl-gnulib-lib-localcharset-c.patch
  patch -p1 -i $srcdir/gettext-0.22.5-gettext-runtime-intl-gnulib-lib-localename-h.patch
  patch -p1 -i $srcdir/gettext-0.22.5-gettext-runtime-intl-gnulib-lib-localename-unsafe-c.patch
  patch -p1 -i $srcdir/gettext-0.22.5-gettext-tools-gnulib-lib-localcharset-c.patch
  patch -p1 -i $srcdir/gettext-0.22.5-gettext-tools-gnulib-lib-localename-h.patch
  patch -p1 -i $srcdir/gettext-0.22.5-gettext-tools-gnulib-lib-localename-unsafe-c.patch
  patch -p1 -i $srcdir/gettext-0.22-disable-libtextstyle.patch

  # MSYS2 patch
  patch -p1 -i $srcdir/gettext-0.19.8.1-msys2.patch

  pushd gettext-tools/misc > /dev/null
    mkdir archive
    tar -xvf archive.dir.tar -C archive
    rm -f archive.dir.tar
    pushd archive > /dev/null
      patch -p1 -i ${srcdir}/gettext-0.19.7-archive.patch
      tar -cvf ../archive.dir.tar *
    popd > /dev/null
    rm -rf archive
  popd > /dev/null

  libtoolize --copy --force
  ./autogen.sh --skip-gnulib
}

build() {
	cd "${srcdir}/${pkgname}-${pkgver}"

  local CHOST="$(echo "${CHOST}" | sed 's|-msys$|-cygwin|')"

  ./configure \
    --build=${CHOST} \
    --host=${CHOST} \
    --prefix=/usr \
    --with-included-libcroco \
    --with-included-libunistring \
    --with-included-libxml \
    --with-included-glib \
    --with-included-gettext \
    --without-emacs \
    --enable-shared \
    --enable-static \
    --disable-java \
    --disable-native-java \
    --disable-csharp \
    --disable-openmp

  make

  make DESTDIR="${srcdir}/dest" install
  rm -f ${srcdir}/dest/usr/lib/charset.alias
}

check() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make check
}

package_gettext() {
  depends=('libintl' 'libgettextpo' 'libasprintf')

  mkdir -p ${pkgdir}/usr/{bin,lib,share}

  cp -rf ${srcdir}/dest/usr/lib/gettext ${pkgdir}/usr/lib/

  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  rm -rf ${pkgdir}/usr/bin/msys-asprintf-0.dll
  rm -rf ${pkgdir}/usr/bin/msys-gettextpo-0.dll
  rm -rf ${pkgdir}/usr/bin/msys-intl-8.dll

  cp -rf ${srcdir}/dest/usr/share/aclocal ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/info ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/locale ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/man ${pkgdir}/usr/share/

}

package_gettext-devel() {
  pkgdesc="GNU Internationalization development utilities"
  groups=('development')
  depends=("gettext=${pkgver}" "libiconv-devel")
  options=('staticlibs')
  optdepends=('cvs: for autopoint tool')

  mkdir -p ${pkgdir}/usr/{bin,include,lib,share}

  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
  rm -rf ${pkgdir}/usr/lib/gettext
  cp -rf ${srcdir}/dest/usr/share/gettext ${pkgdir}/usr/share/
}

package_libintl() {
  pkgdesc="GNU Internationalization runtime library"
  groups=('libraries')
  depends=('gcc-libs' 'libiconv')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/msys-intl-8.dll ${pkgdir}/usr/bin/
}

package_libgettextpo() {
  pkgdesc="GNU Internationalization runtime library"
  groups=('libraries')
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/msys-gettextpo-0.dll ${pkgdir}/usr/bin/
}

package_libasprintf() {
  pkgdesc="C-style formatted output in C++ (runtime)"
  groups=('libraries')
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/msys-asprintf-0.dll ${pkgdir}/usr/bin/
}
