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

pkgname=('libiconv' 'libiconv-devel')
pkgver=1.18
pkgrel=1
pkgdesc='Libiconv is a conversion library'
groups=('libraries')
arch=('i686' 'x86_64')
makedepends=('autotools' 'gcc' 'gettext-devel')
url='https://www.gnu.org/software/libiconv/'
msys2_repository_url='https://git.savannah.gnu.org/gitweb/?p=libiconv.git'
license=('LGPL')
source=("https://ftp.gnu.org/pub/gnu/${pkgname}/${pkgname}-${pkgver}.tar.gz"
        1.16-aliases.patch
        1.16-cross-install.patch
        1.16-wchar.patch
        libiconv-1.16-msysize.patch)
sha256sums=('3b08f5f4f9b4eb82f151a7040bfd6fe6c6fb922efe4b1659c66ea933276965e8'
            '72c8318424acdf92260550176359ae1426e751e2071ecfa061a515a8f59d292c'
            '1e24e990d67fd8fbb9ece6318880127ff06b84d2d278dd18b0643044e64e5edf'
            'c2f4b2d0ce0cd7d2dc98aa1d5e2db31c3b88c1b399d3706dbdacfaef3c13d70c'
            '8a43391e9402e99f70ef3f5dfbb8dda7ab7a30d91c00189ce5ab38c1bc6352d8')
options=(!libtool)

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

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

  apply_patch_with_msg \
    1.16-aliases.patch \
    1.16-cross-install.patch \
    1.16-wchar.patch \
    libiconv-1.16-msysize.patch

  cp -f srcm4/* m4/

  (cd libcharset && autoreconf -fiv)
  autoreconf -fiv
}

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

  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --without-libintl-prefix \
    --enable-static \
    --enable-shared \
    --enable-extra-encodings

  make
  make DESTDIR=${srcdir}/dest install
}

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

package_libiconv() {
  depends=('gcc-libs' 'libintl')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
}

package_libiconv-devel() {
  groups=('development')
  options=('staticlibs')
  depends=("libiconv=${pkgver}")
  pkgdesc="libiconv headers and libraries"

  mkdir -p ${pkgdir}/usr
  cp -rf $srcdir/dest/usr/include ${pkgdir}/usr/
  cp -rf $srcdir/dest/usr/lib ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/lib/charset.alias
}
