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

pkgbase=gettext
pkgname=('gettext' 'gettext-devel' 'libgettextpo' 'libasprintf' 'libintl')
pkgver=0.19.8.1
pkgrel=1
pkgdesc="GNU internationalization library"
arch=('i686' 'x86_64')
url="https://www.gnu.org/software/gettext/"
license=('GPL')
groups=('base-devel')
makedepends=('libiconv-devel')
options=(!docs)
source=(https://ftp.gnu.org/pub/gnu/gettext/${pkgname}-${pkgver}.tar.gz{,.sig}
        0.19.8.1-autopoint-V.patch
        0.19.8.1-localename.patch
        0.19.8.1-no-woe32dll.patch
        0.19.8.1-tests-cygwin.patch
        0.19.8.1-cygwin-ftm.patch
        0.19.8.1-have_wprintf.patch
        gettext-0.19.8.1-msys2.patch
        gettext-0.19.7-archive.patch)
sha256sums=('ff942af0e438ced4a8b0ea4b0b6e0d6d657157c5e2364de57baa279c1c125c43'
            'SKIP'
            '0d3082e9a6b997b61dbb160f5af234a1bc89cddf9dc680999429276cf605b731'
            '25aa01d5a2f58e60878f03bead62662434f56247ba965668fd7b26108b80c4f3'
            'b182af5d1e60c520fc6e716565c58a5f7497f608b5d4b07e3a58500fec54cdc5'
            'd564c2d10ded1c60c421b0790639339f60ea7a4bc7e6d3034e59ec50d04d07be'
            '22cfaca417f04507a901cba93a7eb219183d5e681b7459d8c0e9660621a9a696'
            'd4df598ebde60ea4e069991baed43b00dbb28d3b8a40cc7cf4a6e12847ee3616'
            'c049be2f812524647b662d421dc2a5df59e7da68a42ab0e1cef6af6665cb00a0'
            'e117ddf7cbb175c0ceabc03d8114f875fa120e8f9999e914ecb2bc4ded8d92ce')
validpgpkeys=('462225C3B46F34879FC8496CD605848ED7E69871')

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

  # Cygwin patches
  patch -p2 -i $srcdir/0.19.8.1-autopoint-V.patch
  patch -p2 -i $srcdir/0.19.8.1-localename.patch
  patch -p2 -i $srcdir/0.19.8.1-no-woe32dll.patch
  patch -p2 -i $srcdir/0.19.8.1-tests-cygwin.patch
  patch -p2 -i $srcdir/0.19.8.1-cygwin-ftm.patch
  patch -p3 -i $srcdir/0.19.8.1-have_wprintf.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}"

cat > config.cache << EOF
ac_cv_header_dirent_h=no
EOF
 
  ./configure --build=${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 \
    --cache-file=config.cache

  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')
  install=gettext.install

  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' 'base-devel')
  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/
}
