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

# Note: I would like to update to 59.1 but I found
# putil.cpp: In function 'const char* uprv_getDefaultLocaleID_59d()':
# putil.cpp:1733:86: error: 'GetUserDefaultLocaleName' was not declared in this sc           ope
# int length = GetUserDefaultLocaleName(windowsLocale, UPRV_LENGTHOF(windowsL           ocale));

_realname=icu
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-libs")
pkgver=62.1
pkgrel=1
arch=('any')
pkgdesc="International Components for Unicode library (mingw-w64)"
arch=('any')
url="http://www.icu-project.org/"
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"
        0004-move-to-bin.mingw.patch
        0007-actually-move-to-bin.mingw.patch
        0008-data-install-dir.mingw.patch
        0009-fix-bindir-in-config.mingw.patch
        0010-msys-rules-for-makefiles.mingw.patch
        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)
sha256sums=('3dd9868d666350dda66a6e305eecde9d479fb70b30d5b55d78a1deffb97d5aa3'
            '0b6f5ea91bb579f03cdfa2737719411e3207169f5921ffeea8564cf24fa46715'
            'a32b3fae6a59599c942e5f8bc2d29416aaedca0ce2832c857ecf99eb7529cee1'
            'f437f25a62420856ed234d2b33672b860ae00106cb5d4ccc682f5ff15176aeaf'
            'c84fda5b32edcbf062d629bbf04c47e73e7b9620f4dae752c1c0c25f49124ca8'
            '1cf7fd08bad22f39441213d54f0659a3046e163729aa4cd6ff393602ec22c285'
            '4f4787caeccf70607cf0cbde0c005f05f5c6de1543265a927839122405b4054f'
            'e7ecdafe85e18a4a4b5f29bbfde38776521a848e5b65089a2379b90e59f1592d'
            '564e0171b9c183dfc45a0022ee94220de715d17351b3d2a8aa7fa7ff4c20a8cd'
            '0181f9ff8b7dd0a423869d8772da0a4feea64188347eb0dee7eb059aea92131c'
            '87ebe8962f8c387714f2a697a664a0c49aed2331b988548069d0c211abc36e05'
            '5247102df5604671c33a6e7226e5135a6b48049550861b892f0686dcefec4329'
            'd8612f40b1731d9a94290afcf80c896184a2f15b8ae8f23b3643c64f6cabfa2f')

# 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 0004-move-to-bin.mingw.patch
  apply_patch_with_msg 0007-actually-move-to-bin.mingw.patch
  apply_patch_with_msg 0008-data-install-dir.mingw.patch
  apply_patch_with_msg 0009-fix-bindir-in-config.mingw.patch
  apply_patch_with_msg 0010-msys-rules-for-makefiles.mingw.patch
  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 0020-workaround-missing-locale.patch
  apply_patch_with_msg 0021-mingw-static-libraries-without-s.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 ICU we ignore the options for debug above and always
  # build both debug and release (and static and shared).
  # This is because ICU people want the debug libraries be
  # suffixed with 'd' and e.g. Qt-Project go along with this
  # (though only for the static variant, oddly, see:
  #   qtbase/config.tests/unix/icu/icu.pro
  for _variant in debug release; do
    [[ -d build-${CARCH}-${_variant} ]] && rm -rf build-${CARCH}-${_variant}
    if [ "${_variant}" = "debug" ]; then
      extra_config=( --enable-debug )
      extra_config+=( --with-library-suffix=d )
    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=archive \
      "${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
    popd
  done
}

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

  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"

  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
}

package_mingw-w64-i686-icu() {
  package_icu
}

package_mingw-w64-i686-icu-debug-libs() {
  package_icu-debug-libs
}

package_mingw-w64-x86_64-icu() {
  package_icu
}

package_mingw-w64-x86_64-icu-debug-libs() {
  package_icu-debug-libs
}
