# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=icu
pkgname=('icu' 'icu-devel')
pkgver=77.1
pkgrel=1
pkgdesc="International Components for Unicode library"
arch=('i686' 'x86_64')
url="https://icu.unicode.org"
msys2_repository_url="https://github.com/unicode-org/icu"
msys2_references=(
  "cpe: cpe:/a:icu-project:international_components_for_unicode"
)
license=('spdx:ICU')
depends=('gcc-libs' 'sh')
makedepends=('autoconf-archive' 'autotools' 'gcc')
source=("https://github.com/unicode-org/${pkgname}/releases/download/release-${pkgver//./-}/${pkgname}4c-${pkgver//./_}-src.tgz"{,.asc}
        icu-59.1-msys2.patch)
sha512sums=('a47d6d9c327d037a05ea43d1d1a06b2fd757cc02a94f7c1a238f35cfc3dfd4ab78d0612790f3a3cca0292c77412a9c2c15c8f24b718f79a857e007e66f07e7cd'
            'SKIP'
            'd3e7c7a65c000bd283d9fd843566ce499edff6580ac8b8004bdba219e47d0dc71c156548df711a08ffae0cb699e2f73fb2800af1850131c37124b1107faf897a')
validpgpkeys=('E52F07877A5805F9AF4AB0ACD46C5610D06E7001')

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

  patch -p2 -i ${srcdir}/icu-59.1-msys2.patch

  libtoolize --force --copy
  autoreconf -fi
}

build() {
  cd ${srcdir}/icu/source


  CC=/usr/bin/gcc \
  CXX=/usr/bin/g++ \
  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --sysconfdir=/etc \
    --mandir=/usr/share/man \
    --sbindir=/usr/bin
  make

  make -j1 DESTDIR=${srcdir}/dest install

  # remove extra implib
  rm ${srcdir}/dest/usr/bin/libicudata${pkgver%%.*}.dll.a
  # remove unversioned DLL
  rm ${srcdir}/dest/usr/bin/msys-icudata.dll

  pushd ${srcdir}/dest/usr/lib
    # create unversioned implib symlinks
    for l in *.dll.a; do
      cp ${l} ${l//${pkgver%%.*}}
    done
  popd

  sed -e 's/l${LIBICU}/l${ICUPREFIX}/g' \
    -e '/l${ICUPREFIX}/ s/${ICULIBSUFFIX_VERSION}//g' \
    -i ${srcdir}/dest/usr/bin/icu-config \
    || error "sed icu-config failed"

  # Install license
  install -Dm644 ${srcdir}/icu/license.html ${srcdir}/dest/usr/share/licenses/icu/license.html
}

check() {
  cd icu/source
  make -k check # passes all
}

package_icu() {
  depends=('gcc-libs')
  groups=('libraries')
  mkdir -p $pkgdir/usr/bin
  cp -f $srcdir/dest/usr/bin/*.dll $pkgdir/usr/bin/
}

package_icu-devel() {
  pkgdesc="ICU headers and libraries"
  groups=('development')
  depends=("icu=${pkgver}")

  cp -rf $srcdir/dest/usr $pkgdir/
  rm -f $pkgdir/usr/bin/*.dll
}
