# Maintainer: Philippe Renon <philippe.renon@yahoo.fr>

_realname=rocksdb
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.12.7
pkgrel=1
pkgdesc="A library that provides an embeddable, persistent key-value store for fast storage (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://rocksdb.org/"
license=("Apache")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/facebook/${_realname}/archive/v${pkgver}.tar.gz"
        "0004-fix-generating-and-install-import-lib.patch"
        "0005-fix-cast-between-incompatible-pointers.patch"
        "0006-cmake-build.patch"
        "0007-mingw-fix-build-flags.patch"
        "0008-fix-threads.patch")
sha256sums=('fee38528108b6b49f813b9f055584b123ff0debbc3f39584d4a663c46075a6e9'
            '0f857c6d215b010bc9b47d526e7b5e5faabd380b35449f5d46e4c1556680cfc6'
            'f0ce06542820a45eee74316f4b84a921f266032666a07e25284680ad1cc041da'
            'e3ca227ef5751fe558135720ee911c9747fb77a6d6b84a2f4001ccff7c224ee1'
            '14ed48fde07d6165316e467fa13f908f00a7efc3449c67a1256ff37cb4c88039'
            '677fc71881f360826094d0ac024b7cbf2af89cc2bcf352b673cc0628a1959eee')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0004-fix-generating-and-install-import-lib.patch"
  patch -p1 -i "${srcdir}/0005-fix-cast-between-incompatible-pointers.patch"
  patch -p1 -i "${srcdir}/0006-cmake-build.patch"
  patch -p1 -i "${srcdir}/0007-mingw-fix-build-flags.patch"
  patch -p1 -i "${srcdir}/0008-fix-threads.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  declare -a extra_config
  if check_option "debug" "n"; then
    # tests don't compile in Release mode
    extra_config+=("-DCMAKE_BUILD_TYPE=Release" "-DWITH_TESTS=OFF" "-DWITH_GFLAGS=OFF")
  else
    # not tested + needs gflags dependency
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DWITH_ZLIB=ON \
    -DWITH_BZ2=ON \
    -DWITH_LZ4=ON \
    -DWITH_ZSTD=ON \
    -DWITH_TBB=OFF \
    -DWITH_SNAPPY=ON \
    -DUSE_RTTI=ON \
    -DPORTABLE=ON \
    -DROCKSDB_INSTALL_ON_WINDOWS=ON \
    ../${_realname}-${pkgver}

  ninja
}

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

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/${_realname}/*.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.Apache "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/LICENSE.leveldb "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE.leveldb
  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
