# Maintainer: Jeroen Ooms <jeroen@berkeley.edu>

_realname=tiledb
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.27.0
pkgrel=4
pkgdesc="Storage management library for sparse and dense array data (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://tiledb.com/"
msys2_references=(
  'aur: tiledb'
)
msys2_repository_url='https://github.com/TileDB-Inc/TileDB/'
license=("spdx:MIT")
depends=($([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo \
            "${MINGW_PACKAGE_PREFIX}-aws-c-common" \
            "${MINGW_PACKAGE_PREFIX}-aws-crt-cpp" \
            "${MINGW_PACKAGE_PREFIX}-aws-sdk-cpp")
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-file"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-spdlog"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-catch"
             "${MINGW_PACKAGE_PREFIX}-curl")
source=("https://github.com/TileDB-Inc/TileDB/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "002-enable-soversion.patch")
sha256sums=('a1d42e675a983291c1a0edd1a13f70e26649700160bb2bee5d6011a2af55837a'
            '845ccd7b2a73dbce6ab51fa8699f5c3388468e4fe68aa07ae9c1ef51b16edd43')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.gz ..."
  tar -xzf "${srcdir}"/${_realname}-${pkgver}.tar.gz || true

  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/002-enable-soversion.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _extra_config+=("-DTILEDB_S3=OFF")
  else
    _extra_config+=("-DTILEDB_S3=ON")
    CXXFLAGS+=" -Wno-c++11-narrowing"
  fi

  TILEDB_DISABLE_AUTO_VCPKG=0 \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DCMAKE_C_STANDARD=17 \
    "${_extra_config[@]}" \
    -DTILEDB_STATIC=OFF \
    -DTILEDB_WERROR=OFF \
    -DTILEDB_SUPERBUILD=OFF \
    -DTILEDB_TESTS=OFF \
    -DTILEDB_WEBP=OFF \
    -DTILEDB_DISABLE_AUTO_VCPKG=ON \
    -DTILEDB_LIBMAGIC_EP_BUILT=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  # Remove full path reference in cmake files
  local _PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"
  for cmfile in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/TileDB/*.cmake; do
    sed -e "s|${_PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${cmfile}"
  done

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