# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=thrift
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=0.13.0
pkgrel=2
pkgdesc="Apache Thrift is a software framework for scalable cross-language services development (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://thrift.apache.org/'
license=('MIT X11')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "make"
             "bison"
             "flex"
             "git")
options=('strip')
source=("https://archive.apache.org/dist/thrift/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('7ad348b88033af46ce49148097afe354d513c1fca7c607b59c33ebb6064b5179')

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake . \
    -Wno-dev \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DWITH_SHARED_LIB="OFF" \
    -DWITH_STATIC_LIB="ON" \
    -DCMAKE_BUILD_TYPE="Release" \
    -DBUILD_COMPILER="ON" \
    -DBUILD_TESTING="ON" \
    -DBUILD_TUTORIALS="OFF" \
    -DWITH_LIBEVENT="OFF" \
    -DWITH_OPENSSL="ON" \
    -DOPENSSL_USE_STATIC_LIBS="ON" \
    -DWITH_ZLIB="ON" \
    -DWITH_QT5="OFF" \
    -DWITH_CPP="ON" \
    -DWITH_C_GLIB="OFF" \
    -DWITH_JAVA="OFF" \
    -DWITH_PYTHON="OFF" \
    -DWITH_HASKELL="OFF"

  make -j1
}

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

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" -j1 install

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/thrift/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
