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

pkgname=('libiconv' 'libiconv-devel')
pkgver=1.16
pkgrel=2
pkgdesc='Libiconv is a conversion library'
groups=('libraries')
arch=('i686' 'x86_64')
url='https://www.gnu.org/software/libiconv/'
license=('LGPL')
source=("https://ftp.gnu.org/pub/gnu/${pkgname}/${pkgname}-${pkgver}.tar.gz"
        1.14-cross-install.patch
        libiconv-1.16-msysize.patch)
sha256sums=('e6a1b1b589654277ee790cce3734f07876ac4ccfaecbee8afa0b649cf529cc04'
            'e1e7aec604732219f9985458db46042b2bafe0459d1a6a0fb245c5810f65b066'
            '7320176061251170c1af9f088e1ad0080dda9febafd4dc5358d1453f233fd1f5')
options=(!libtool)

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

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f ${_fname} ]; then
      rm -rf ${_fname}
    fi
  done
}
# =========================================== #


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

  apply_patch_with_msg \
    1.14-cross-install.patch \
    libiconv-1.16-msysize.patch

  rm build-aux/ltmain.sh
  rm m4/{libtool,lt~obsolete,ltoptions,ltsugar}.m4
  rm libcharset/build-aux/ltmain.sh
  rm libcharset/m4/{libtool,lt~obsolete,ltoptions,ltsugar}.m4
  cp -f srcm4/* m4/

  autoreconf -fiv
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}
  ./configure \
    --prefix=/usr \
    --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
}
