# 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)
pkgdesc='Character encoding conversion library and utility (mingw-w64)'
pkgver=1.18
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.gnu.org/software/libiconv/'
license=('spdx:GPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-gperf")
source=("https://ftp.gnu.org/pub/gnu/${_realname}/${_realname}-${pkgver}.tar.gz"{,.sig}
        0003-add-cp65001-as-utf8-alias.patch
        0002-fix-cr-for-awk-in-configure.all.patch
        fix-pointer-buf.patch
        iconv.pc)
sha256sums=('3b08f5f4f9b4eb82f151a7040bfd6fe6c6fb922efe4b1659c66ea933276965e8'
            'SKIP'
            'cb2b1cca44c9b9bd11c3bd33997c10d22dff263542275324c88f34b59910ef87'
            '89d5c0f666e50a0186cfb142ba7b77e8fe1ac4d65bdfd9ae14ae8d2f0045a87c'
            '6b6e2393840f4dc6067587165777b1a07978f4c05247d7c1010a45ad251bbeeb'
            '56e7ec406bf42eb66b1d972f20d229f7a76ba00c38c7b4403bf348875704baae')
validpgpkeys=('9001B85AF9E1B83DF1BDA942F5BE8B267C6A406D')  # "Bruno Haible (Open Source Development) <bruno@clisp.org>"

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

regenerate_files() {
  msg2 "Regenerating autogenerated files"
  CC=${CC} make -f Makefile.devel all
}

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

  apply_patch_with_msg \
    0002-fix-cr-for-awk-in-configure.all.patch \
    fix-pointer-buf.patch \
    0003-add-cp65001-as-utf8-alias.patch

  regenerate_files
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --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-${MSYSTEM}"
  make check || msg2 "tests failed"
}

package_libiconv() {
  pkgdesc='Character encoding conversion library (mingw-w64)'
  license=('spdx:LGPL-2.1-or-later' documentation:'spdx:GPL-3.0-or-later') # This is LGPL except for documentation, see README

  cd "${srcdir}/build-${MSYSTEM}"
  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-runtime")
  license=('spdx:GPL-3.0-or-later')

  cd "${srcdir}/build-${MSYSTEM}"
  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;
