# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=duckdb
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.4.3
pkgrel=1
pkgdesc='An in-process SQL OLAP Database Management System (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://duckdb.org/'
msys2_repository_url='https://github.com/duckdb/duckdb'
msys2_references=(
  'anitya: 375556'
  'cpe: cpe:2.3:a:duckdb:duckdb'
  'gentoo: dev-db/duckdb'
)
license=('spdx:MIT')
depepnds=(
  "${MINGW_PACKAGE_PREFIX}-cc-libs"
  "${MINGW_PACKAGE_PREFIX}-libwinpthread"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
)
source=("https://github.com/duckdb/duckdb/archive/v${pkgver}/${_realname}-v${pkgver}.tar.gz"
        "0001-duckdb-cmake-fix-lld-unknown-exclude-libs.patch"
        "0002-duckdb-cmake-fix-install-directory.patch")
noextract=("${_realname}-v${pkgver}.tar.gz")
sha256sums=('b6a2afd09d9cf07e50d5cd07077df7f7697b61cca2eb00754f5adf89a1ae6c64'
            '8ff46f7426b402a102bb8bdbde4111f3af24fc8f5883e0128bd88fde6bb99243'
            '332f22f3f8095b940d76c6224dd569b2fdc02a6a0b37f3b84e462d15af16da2e')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  tar xf "${_realname}-v${pkgver}.tar.gz" || true

  cd "${_realname}-${pkgver}"

  _apply_patch_with_msg \
    0001-duckdb-cmake-fix-lld-unknown-exclude-libs.patch \
    0002-duckdb-cmake-fix-install-directory.patch
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_UNITTESTS=OFF \
      -DOVERRIDE_GIT_DESCRIBE="v${pkgver}" \
      -DPython3_EXECUTABLE="${MINGW_PREFIX}"/bin/python.exe \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

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

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

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