# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>

_realname=icu
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-libs")
pkgver=68.2
pkgrel=3
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
pkgdesc="International Components for Unicode library (mingw-w64)"
url="http://site.icu-project.org/home/"
license=('custom:icu')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
# There's no point adding 'debug' to options as both
# debug and release packages are built regardless.
options=('!buildflags' 'staticlibs' 'strip')
source=(#"http://download.icu-project.org/files/icu4c/${pkgver}/icu4c-${pkgver//./_}-src.tgz"
        https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-src.tgz
        0011-sbin-dir.mingw.patch
        0012-libprefix.mingw.patch
        0014-mingwize-pkgdata.mingw.patch
        0015-debug.mingw.patch
        0016-icu-pkgconfig.patch
        0017-icu-config-versioning.patch
        #0020-workaround-missing-locale.patch
        0021-mingw-static-libraries-without-s.patch
        0023-fix-twice-include-platform_make_fragment.patch
        0024-mingw-no-Bsymbolic.patch)
sha256sums=('c79193dee3907a2199b8296a93b52c5cb74332c26f3d167269487680d479d625'
            '4f4787caeccf70607cf0cbde0c005f05f5c6de1543265a927839122405b4054f'
            'e7ecdafe85e18a4a4b5f29bbfde38776521a848e5b65089a2379b90e59f1592d'
            'a131b64afbe45ad30d877973f7ff271044ef27409a506c56555663aa271ab011'
            '0181f9ff8b7dd0a423869d8772da0a4feea64188347eb0dee7eb059aea92131c'
            '87ebe8962f8c387714f2a697a664a0c49aed2331b988548069d0c211abc36e05'
            '3cd5a7b6ca87aae3b53c246286e1b4cee6639b3fec69831f8a1f6930cd92c6c3'
            'd8612f40b1731d9a94290afcf80c896184a2f15b8ae8f23b3643c64f6cabfa2f'
            '517a4b2308c5d7662768ece12b01b457b83a2cd80d8bf407e593e6412dfbde92'
            '3a62ca57c86d0629bd62180f46ac6d57171f6441a18438f140b232c1df4fd4fa')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  msg2 "Applying $1"
  patch -Nbp1 -i "${srcdir}"/$1
}

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

prepare() {
  cd "${srcdir}/icu"

  apply_patch_with_msg 0011-sbin-dir.mingw.patch
  apply_patch_with_msg 0012-libprefix.mingw.patch
  apply_patch_with_msg 0014-mingwize-pkgdata.mingw.patch
  apply_patch_with_msg 0015-debug.mingw.patch
  apply_patch_with_msg 0016-icu-pkgconfig.patch
  apply_patch_with_msg 0017-icu-config-versioning.patch
  apply_patch_with_msg 0021-mingw-static-libraries-without-s.patch
  apply_patch_with_msg 0023-fix-twice-include-platform_make_fragment.patch
  apply_patch_with_msg 0024-mingw-no-Bsymbolic.patch

  cd source
  autoreconf -vfi
}

build() {
  local -a extra_config
  CXXFLAGS+=" -DU_USE_STRTOD_L=0" # breaks on Windows XP
  #CXXFLAGS+=" -D_WIN32_WINNT=0x0601"
  cd "${srcdir}/icu/"

  for _variant in debug release; do
    [[ -d build-${CARCH}-${_variant} ]] && rm -rf build-${CARCH}-${_variant}
    if [ "${_variant}" = "debug" ]; then
      extra_config=( --enable-debug )
    else
      extra_config=( --enable-release )
    fi
    cp -rf source build-${CARCH}-${_variant}
    pushd build-${CARCH}-${_variant}
    ./configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --disable-rpath \
      --enable-shared \
      --enable-static \
      --with-data-packaging=dll \
      "${extra_config[@]}"
    make -j1
    popd
  done
}

check() {
  for _variant in debug release; do
    pushd "${srcdir}/icu/build-${CARCH}-${_variant}"
    PATH=${srcdir}/icu/build-${CARCH}/bin:${srcdir}/icu/build-${CARCH}/lib:$PATH \
    make V=1 -k check || true
    popd
  done
}

package_icu() {
  pushd "${srcdir}/icu/build-${CARCH}-release"
  make install DESTDIR="${pkgdir}" RM="rm -rf"

  #mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/bin/*.a "${pkgdir}${MINGW_PREFIX}"/lib/
  #pushd "${pkgdir}${MINGW_PREFIX}"/bin > /dev/null
  #  # remove unversioned DLL symlinks
  #  find . -regex "[^0-9]+\.dll" -exec rm -rf {} \;
  #popd > /dev/null
}

package_icu-debug-libs() {
  options=('debug' '!strip')
  pushd "${srcdir}/icu/build-${CARCH}-debug"
  make install DESTDIR="${pkgdir}" RM="rm -rf"

  #mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/bin/*.a "${pkgdir}${MINGW_PREFIX}"/lib/
  #pushd "${pkgdir}${MINGW_PREFIX}"/bin > /dev/null
  #  # remove unversioned DLL symlinks
  #  find . -regex "[^0-9]+\.dll" -exec rm -rf {} \;
  #popd > /dev/null

  # Remove bits that are also in the main package.
  # TODO :: Maybe move the pkg-config files to 'd' suffixed variants?
  rm -rf "${pkgdir}${MINGW_PREFIX}"/{include,sbin,share,lib/pkgconfig}
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin/*.exe
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin/icu-config
}

# 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;
