# Template file for 'sqlite'
pkgname=sqlite
version=3.53.2
revision=1
_amalgamationver=$(printf "%d%02d%02d00\n" ${version//./ })
build_style=configure
# Even when it is included in the build, memsys5 is disabled by default.
configure_args="--enable-threadsafe --enable-load-extension --enable-math
 --enable-fts3 --enable-fts4 --enable-fts5 --enable-memsys5
 --enable-rtree --enable-geopoly
 --prefix=/usr --soname=legacy --editline
 --enable-session --enable-dbpage --enable-dbstat
 --enable-icu-collations"
hostmakedepends="tcl"
makedepends="libedit-devel zlib-devel tcl-devel icu-devel"
short_desc="SQL Database Engine in a C Library"
maintainer="Orphaned <orphan@voidlinux.org>"
license="Public Domain"
homepage="https://sqlite.org/"
changelog="https://www.sqlite.org/changes.html"
distfiles="https://sqlite.org/2026/sqlite-src-${_amalgamationver}.zip"
checksum=cafff764c03f6d720968f746e2f47a986bbf12bf4c18904f1eb131c0b0b592d3

CFLAGS="-DSQLITE_ENABLE_DBSTAT_VTAB -DSQLITE_ENABLE_COLUMN_METADATA"
CFLAGS+=" -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_SECURE_DELETE"
CFLAGS+=" -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4"
CFLAGS+=" -DSQLITE_ENABLE_FTS3_TOKENIZER=1 -DSQLITE_ENABLE_BATCH_ATOMIC_WRITE=1"
CFLAGS+=" -DSQLITE_ENABLE_DESERIALIZE -DSQLITE_USE_URI=1"
# explicitly enable fdatasync
CFLAGS+=" -DHAVE_FDATASYNC"
disable_parallel_build=yes

# explicitly set byte order to avoid bad autodetection macros
case "$XBPS_TARGET_ENDIAN" in
	le) CFLAGS+=" -DSHA3_BYTEORDER=1234 -DSQLITE_BYTEORDER=1234";;
	be) CFLAGS+=" -DSHA3_BYTEORDER=4321 -DSQLITE_BYTEORDER=4321";;
esac

if [ "$CROSS_BUILD" ]; then
	configure_args+=" --with-icu-config=$XBPS_WRAPPERDIR/icu-config"
	configure_args+=" --with-readline-header=${XBPS_CROSS_BASE}/usr/include/editline/readline.h"
	configure_args+=" --with-tcl=$XBPS_CROSS_BASE/usr/lib"
	configure_args+=" --host=$XBPS_CROSS_TRIPLET"
else
	configure_args+=" --with-icu-config=/usr/bin/icu-config"
	configure_args+=" --with-readline-header=/usr/include/editline/readline.h"
fi


_tools="showdb showjournal showstat4 showwal sqldiff sqlite3_analyzer"
post_configure() {
	sed -i '/LDFLAGS.rpath/s/=.*/=/' Makefile
}

post_build() {
	make $_tools
	cd tool
	echo $CC $CFLAGS $LDFLAGS -o lemon lemon.c
	$CC $CFLAGS $LDFLAGS -o lemon lemon.c
}

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

sqlite-lemon_package() {
	short_desc="LALR(1) Parser Generator from sqlite"
	pkg_install() {
		vbin tool/lemon
		vinstall tool/lempar.c 644 usr/share/lemon
	}
}

sqlite-tools_package() {
	short_desc+=" - tools"
	pkg_install() {
		local t
		for t in $_tools; do
			vbin $t
		done
	}
}

sqlite-tcl_package() {
	short_desc+=" - tcl bindings"
	pkg_install() {
		vmove "usr/lib/tcl*"
	}
}
