# Template file for 'duckdb'
pkgname=duckdb
version=1.5.0
revision=1
archs="x86_64* aarch64*" # 32-bit FTBFS
build_style=cmake
build_helper="python3"
build_wrksrc="${pkgname}-${version}"
configure_args="-DOVERRIDE_GIT_DESCRIBE=v${version}
 -DBUILD_EXTENSIONS='autocomplete;icu;json;parquet'"
hostmakedepends="pkg-config cmake ninja python3-build python3-installer python3-scikit-build-core
 python3-pybind11 python3-setuptools_scm"
makedepends="openssl-devel"
short_desc="In-process SQL OLAP database management system"
maintainer="icp <pangolin@vivaldi.net>"
license="MIT"
homepage="https://duckdb.org"
changelog="https://github.com/duckdb/duckdb/releases"
distfiles="https://github.com/duckdb/duckdb/archive/refs/tags/v${version}.tar.gz>duckdb.tar.gz
 https://github.com/duckdb/duckdb-python/archive/refs/tags/v${version}.tar.gz>duckdb-python.tar.gz"
checksum="fb039699c5a91dec9876540aed7904b6b4e713b800014840dbf641168147a556
 4102194e49600650e90d3408a7c5ffcd4cfd35165c836da1be34f81e2901331b"

_pypkg_wrksrc="${XBPS_BUILDDIR}/duckdb-${version}/duckdb-python-${version}"

case "${XBPS_TARGET_MACHINE}" in
	x86_64) configure_args+=" -DDUCKDB_EXPLICIT_PLATFORM=linux_amd64" ;;
	x86_64-musl) configure_args+=" -DDUCKDB_EXPLICIT_PLATFORM=linux_amd64_musl" ;;
	aarch64) configure_args+=" -DDUCKDB_EXPLICIT_PLATFORM=linux_arm64" ;;
	aarch64-musl) configure_args+=" -DDUCKDB_EXPLICIT_PLATFORM=linux_arm64_musl" ;;
esac

post_extract() {
	rmdir "${_pypkg_wrksrc}/external/duckdb"
	cp -a "${wrksrc}/duckdb-${version}" "${_pypkg_wrksrc}/external/duckdb"
	vsed -e "s/pybind11\[global\]/pybind11/g" -i "${_pypkg_wrksrc}/pyproject.toml"
	touch "${_pypkg_wrksrc}/.git"
}

post_build() {
	OVERRIDE_GIT_DESCRIBE="v${version}" CMAKE_EXECUTABLE="${XBPS_MASTERDIR}/usr/bin/cmake" python3 -m build \
		--no-isolation --wheel ${_pypkg_wrksrc}
}

post_install() {
	python3 -m installer --destdir "${DESTDIR}" --no-compile-bytecode \
		${_pypkg_wrksrc}/dist/duckdb-${version}-*-*-*.whl

	vlicense LICENSE
}

duckdb-devel_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/cmake
		vmove "usr/lib/*.a"
		vmove "usr/lib/*.so"
	}
}

python3-duckdb_package() {
	short_desc+=" - python bindings"
	pkg_install() {
		vmove "${py3_sitelib}"
	}
}
