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

pkgbase=icu
pkgname=('icu' 'icu-devel')
pkgver=69.1
pkgrel=1
pkgdesc="International Components for Unicode library"
arch=('i686' 'x86_64')
url="http://www.icu-project.org/"
license=('custom:"icu"')
depends=('gcc-libs' 'sh')
source=(https://github.com/unicode-org/${pkgname}/releases/download/release-${pkgver//./-}/${pkgname}4c-${pkgver//./_}-src.tgz
        toolutil-Makefile-ln-force.patch
        icu-59.1-msys2.patch)
sha256sums=('4cba7b7acd1d3c42c44bb0c14be6637098c7faf2b330ce876bc5f3b915d09745'
            '6fa1294880ea24263d010f9a1aacc52fdd663f552baa92a0046fbfb80a267463'
            'f3279572b38aa3182d526932e74c8c166253732088fe978e3be6503aa422e15a')

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

  patch -p2 -i ${srcdir}/toolutil-Makefile-ln-force.patch
  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
  pushd ${srcdir}/dest/usr/lib
    # fix installation directory
    mv msys-icu*${pkgver%%.*}.dll ${srcdir}/dest/usr/bin/
    # remove unversioned DLL symlinks
    rm -f *.dll
    # create unversioned implib symlinks
    for l in *.dll.a; do
      #dosym ${l} /usr/lib/${l//${pkgver%%.*}}
      cp ${l} ${l//${pkgver%%.*}}
    done
  popd

  sed -e '/^ICUUC_FILE=/ s/libdir/bindir/' \
    -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
}
