# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=libiconv
pkgbase=mingw-w64-${_realname}
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname} ${MINGW_PACKAGE_PREFIX}-iconv)
license=('GPL3')
pkgdesc='Character encoding conversion library and utility (mingw-w64)'
pkgver=1.16
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://www.gnu.org/software/libiconv/'
source=("https://ftp.gnu.org/pub/gnu/${_realname}/${_realname}-${pkgver}.tar.gz"
        0001-compile-relocatable-in-gnulib.mingw.patch
        0002-fix-cr-for-awk-in-configure.all.patch
        fix-pointer-buf.patch
        iconv.pc)
sha256sums=('e6a1b1b589654277ee790cce3734f07876ac4ccfaecbee8afa0b649cf529cc04'
            '7e905d21d670672b8a6a3bd80e5b8244b38bb7021a15fc7bdd0229125a3a68ee'
            'fe1a82f0836d65f895bebe9307bbe70ae53e5c872dd727f2042c46edc7cf060d'
            'fc1921f9d8ae21be0d1ee8a037fbb89aa066865f193a4883aa6e673827abba88'
            '56e7ec406bf42eb66b1d972f20d229f7a76ba00c38c7b4403bf348875704baae')
options=('!libtool' 'staticlibs')
makedepends=(${MINGW_PACKAGE_PREFIX}-gcc)

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/0001-compile-relocatable-in-gnulib.mingw.patch
  patch -p1 -i ${srcdir}/0002-fix-cr-for-awk-in-configure.all.patch
  patch -p1 -i ${srcdir}/fix-pointer-buf.patch
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --docdir=${MINGW_PREFIX}/share/doc/libiconv \
    --enable-static \
    --enable-shared \
    --enable-extra-encodings \
    --enable-relocatable \
    --disable-rpath \
    --enable-silent-rules \
    --enable-nls

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make check || true
}

package_libiconv() {
  pkgdesc='Character encoding conversion library (mingw-w64)'
  license=(LGPL2 documentation:'GPL3') # This is LGPL except for documentation, see README

  cd "${srcdir}/build-${MINGW_CHOST}"
  make install DESTDIR="${pkgdir}"
  rm -fr "${pkgdir}${MINGW_PREFIX}"/bin/*.exe
  rm -f  "${pkgdir}${MINGW_PREFIX}"/lib/charset.alias
  rm -fr "${pkgdir}${MINGW_PREFIX}"/share/locale
  rm -f  "${pkgdir}${MINGW_PREFIX}"/share/doc/libiconv/*.1.*
  rm -fr "${pkgdir}${MINGW_PREFIX}"/share/man/man1

  # Pkg-config file
  install -Dm644 "${srcdir}/iconv.pc"  "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/iconv.pc"
  sed -e "s|@PREFIX@|${MINGW_PREFIX}|g" \
      -e "s|@VERSION@|${pkgver}|g" \
      -i "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/iconv.pc"

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/README"                 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/README"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING"                "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LIB"            "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/libcharset/COPYING.LIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/libcharset/COPYING.LIB"
}

package_iconv() {
  pkgdesc='Character encoding conversion utility (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-libiconv=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gettext")
  license=('GPL3')

  cd "${srcdir}/build-${MINGW_CHOST}"
  make install DESTDIR="${pkgdir}"
  rm -f  "${pkgdir}${MINGW_PREFIX}"/bin/*.dll
  rm -fr "${pkgdir}${MINGW_PREFIX}"/include
  rm -fr "${pkgdir}${MINGW_PREFIX}"/lib
  rm -f  "${pkgdir}${MINGW_PREFIX}"/share/doc/libiconv/*.3.*
  rm -fr "${pkgdir}${MINGW_PREFIX}"/share/man/man3

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/README"  "${pkgdir}${MINGW_PREFIX}/share/licenses/iconv/README"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/iconv/COPYING"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
