# Maintainer: Feng Shu <tumashu@163.com>

_realname=librime
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5.3
pkgrel=3
pkgdesc="Rime Input Method Engine Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/rime/librime"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-leveldb"
         "${MINGW_PACKAGE_PREFIX}-marisa"
         "${MINGW_PACKAGE_PREFIX}-opencc"
         "${MINGW_PACKAGE_PREFIX}-yaml-cpp"
         "${MINGW_PACKAGE_PREFIX}-glog")
optdepends=("${MINGW_PACKAGE_PREFIX}-rime-data: Rime schema repository from plum")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gtest")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/rime/librime/archive/${pkgver}.tar.gz"
        "001-fix-librime-path.patch")
sha256sums=('df4279df6bc0419c18d9325e7d76dfdab8f91f0ee00a954f19a27f66dd99f503'
            '7db91db4d81f873f5fcb89fa8ecec8c8e4fc819fbe445d16be08d8a72094e9a8')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p0 -i ${srcdir}/001-fix-librime-path.patch
}

build() {
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"MSYS Makefiles" \
    -DBOOST_ROOT=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_TEST=OFF \
    -DBOOST_USE_CXX11=ON \
    -DBUILD_STATIC=ON \
    -DBUILD_WITH_ICU=ON \
    -DENABLE_LOGGING="no" \
    -DCMAKE_CXX_STANDARD_LIBRARIES="-lbcrypt" \
    "../${_realname}-${pkgver}"

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
