# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=harfbuzz
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.0
pkgrel=1
pkgdesc="OpenType text shaping engine (mingw-w64)"
arch=('any')
url="https://www.freedesktop.org/wiki/Software/HarfBuzz"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-icu"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-ragel")
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-graphite2")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python3-fonttools" 
              "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
options=('strip' 'staticlibs')
optdepends=("${MINGW_PACKAGE_PREFIX}-icu: harfbuzz-icu support"
            "${MINGW_PACKAGE_PREFIX}-cairo: hb-view program")
source=("https://www.freedesktop.org/software/harfbuzz/release/${_realname}-${pkgver}.tar.bz2")
sha256sums=('3b314db655a41d19481e18312465fa25fca6f63382217f08062f126059f96764')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  #autoreconf -fi
}

build() {
  # Build static and shared libs separately due to necessity of defining DGRAPHITE2_STATIC
  # manually when building static version
  export _static_dir="${srcdir}/build-${MINGW_CHOST}-static"
  export _shared_dir="${srcdir}/build-${MINGW_CHOST}-shared"

  # static build
  [[ -d "${_static_dir}" ]] && rm -rf "${_static_dir}"
  mkdir -p "${_static_dir}" && pushd "${_static_dir}"
  CFLAGS="${CFLAGS} -DGRAPHITE2_STATIC" \
  CXXFLAGS="${CXXFLAGS} -DGRAPHITE2_STATIC" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared=no \
    --enable-static=yes \
    --enable-introspection \
    --with-graphite2=yes \
    --with-uniscribe=yes \
    --with-directwrite=yes \
    --with-gobject
  make
  popd

  # shared build
  [[ -d "${_shared_dir}" ]] && rm -rf "${_shared_dir}"
  mkdir -p "${_shared_dir}" && pushd "${_shared_dir}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared=yes \
    --enable-static=no \
    --enable-introspection \
    --with-graphite2=yes \
    --with-uniscribe=yes \
    --with-directwrite=yes \
    --with-gobject
  make
  popd
}

check(){
  local _static_dir="${srcdir}/build-${MINGW_CHOST}-static"
  local _shared_dir="${srcdir}/build-${MINGW_CHOST}-shared"

  # static build 
  pushd "${_static_dir}"
  make check
  popd

  # shared build
  pushd "${_shared_dir}"
  make check
  popd
}

package() {
  local _static_dir="${srcdir}/build-${MINGW_CHOST}-static"
  local _shared_dir="${srcdir}/build-${MINGW_CHOST}-shared"

  # static build
  pushd "${_static_dir}"
  make DESTDIR="${pkgdir}" install
  popd

  # shared build
  pushd "${_shared_dir}"
  make DESTDIR="${pkgdir}" install
  popd

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
