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

pkgname=('libiconv' 'libiconv-devel')
pkgver=1.15
pkgrel=1
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.14-reloc.patch
        libiconv-1.14-wchar.patch
        msys2-libiconv-1.14.patch)
sha256sums=('ccf536620a45458d26ba83887a983b96827001e92a13847b45e4925cc8913178'
            '0891f33528131003939c5e7fa610384834d580e80396f411de0b9f26ec1c520f'
            'f9186f805dbb136cc24ce389ecee5a4d4867eec722292bd77893524dcdedf190'
            '0c8f78f688c2f116a6441ba0dc9c4308d2d4845114b574dd8f0cd1165f070132'
            '849971a5b753222e672faf0d2b7c852e5e29ef07db12acd1d74f272ccc049d27')
options=(!libtool)

# Helper macros to help make tasks easier #
apply_patch_with_msg_p2() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp2 -i "${srcdir}/$_patch"
  done
}

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_p2 1.14-cross-install.patch \
    libiconv-1.14-reloc.patch \
    libiconv-1.14-wchar.patch
  apply_patch_with_msg  \
   msys2-libiconv-1.14.patch

  #./autogen.sh --skip-gnulib
}

build() {
  cd $srcdir/${pkgname}-${pkgver}
  ./configure --prefix=/usr --enable-static --enable-shared
  make
  make DESTDIR=${srcdir}/dest install
}

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

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

  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
}
