# 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=74.2
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="International Components for Unicode library (mingw-w64)"
url="https://icu.unicode.org/home/"
license=('spdx:ICU')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "autoconf-archive")
source=(https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-src-FIXED.tgz
        0011-sbin-dir.mingw.patch
        0012-libprefix.mingw.patch
        0014-mingwize-pkgdata.mingw.patch
        0015-debug.mingw.patch
        0016-icu-pkgconfig.patch
        0021-mingw-static-libraries-without-s.patch
        0023-fix-twice-include-platform_make_fragment.patch)
sha256sums=('68db082212a96d6f53e35d60f47d38b962e9f9d207a74cfac78029ae8ff5e08c'
            '4f4787caeccf70607cf0cbde0c005f05f5c6de1543265a927839122405b4054f'
            'e7ecdafe85e18a4a4b5f29bbfde38776521a848e5b65089a2379b90e59f1592d'
            'd9f5f756443d7d14175aca06f4f1bb33237b58907e5db7a6ff7e38d2c4ea7957'
            '0181f9ff8b7dd0a423869d8772da0a4feea64188347eb0dee7eb059aea92131c'
            '87ebe8962f8c387714f2a697a664a0c49aed2331b988548069d0c211abc36e05'
            'd8612f40b1731d9a94290afcf80c896184a2f15b8ae8f23b3643c64f6cabfa2f'
            '517a4b2308c5d7662768ece12b01b457b83a2cd80d8bf407e593e6412dfbde92')

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

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 0021-mingw-static-libraries-without-s.patch
  apply_patch_with_msg 0023-fix-twice-include-platform_make_fragment.patch

  cd source
  autoreconf -vfi
}

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

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--enable-release")
  else
    _extra_config+=("--enable-debug")
  fi

  ../icu/source/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --disable-rpath \
    --enable-shared \
    --enable-static \
    --with-data-packaging=dll \
    "${_extra_config[@]}"

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  PATH=${srcdir}/icu/build-${MSYSTEM}/bin:${srcdir}/icu/build-${MSYSTEM}/lib:$PATH \
  make V=1 -k check || true
}

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

  mv "${pkgdir}${MINGW_PREFIX}"/bin/*.a "${pkgdir}${MINGW_PREFIX}"/lib/
}
