# 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}")
pkgver=68.2
pkgrel=3
arch=('any')
pkgdesc="International Components for Unicode library (mingw-w64)"
url="http://site.icu-project.org/home/"
license=('custom:icu')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
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() {

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

  CXXFLAGS+=" -DU_USE_STRTOD_L=0" # breaks on Windows XP
  #CXXFLAGS+=" -D_WIN32_WINNT=0x0601"

  ${srcdir}/icu/source/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-rpath \
    --disable-shared \
    --enable-static \
    --enable-release

    make -j1
}

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

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install DESTDIR="${pkgdir}" RM="rm -rf"

  #mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  mv -v "${pkgdir}${MINGW_PREFIX}"/bin/*.a "${pkgdir}${MINGW_PREFIX}"/lib/

  # Jeroen: save some space in rtools
  rm -Rf "${pkgdir}${MINGW_PREFIX}"/bin/
  rm -Rf "${pkgdir}${MINGW_PREFIX}"/sbin/
}
