# Contributor: Andreas Radke <andyrtr@archlinux.org>
# Contributor: Tom Newsom <Jeepster@gmx.co.uk>

_pkgname=sqlite
pkgname=static-compat-$_pkgname
_srcver=3400100
_docver=${_srcver}
#_docver=3330000
pkgver=3.40.1
pkgrel=1
pkgdesc="A C library that implements an SQL database engine"
arch=('x86_64')
license=('custom:Public Domain')
url="https://www.sqlite.org/"
depends=(static-compat-zlib static-compat-readline)
makedepends=(static-compat-configure static-compat-tcl patchelf)
source=(https://www.sqlite.org/2022/sqlite-src-${_srcver}.zip
        sqlite-lemon-system-template.patch
        license.txt)
options=('!emptydirs' 'staticlibs')
# upstream now switched to sha3sums - currently not suppoerted by makepkg
sha256sums=('5064126aa50db20c35578b612b56c3129425c0506ed4d1610efa4a0f01bdf8d0'
            '55746d93b0df4b349c4aa4f09535746dac3530f9fd6de241c9f38e2c92e8ee97'
            '4e57d9ac979f1c9872e69799c2597eeef4c6ce7224f3ede0bf9dc8d217b1e65d')

prepare() {
  cd sqlite-src-$_srcver

  # patch taken from Fedora
  # https://src.fedoraproject.org/rpms/sqlite/blob/master/f/sqlite.spec
  patch -Np1 -i ../sqlite-lemon-system-template.patch

  #autoreconf -vfi
}

build() {
  source static-compat-environment
  export PATH=$PWD:$PATH
  printf '#!/usr/bin/bash\nexec /usr/bin/pkg-config --static "$@"\n' > pkg-config
  chmod +x pkg-config
  export CPPFLAGS="$CPPFLAGS \
	-DSQLITE_ENABLE_COLUMN_METADATA=1 \
	-DSQLITE_ENABLE_UNLOCK_NOTIFY \
	-DSQLITE_ENABLE_DBSTAT_VTAB=1 \
	-DSQLITE_ENABLE_FTS3_TOKENIZER=1 \
	-DSQLITE_SECURE_DELETE \
	-DSQLITE_ENABLE_STMTVTAB \
	-DSQLITE_MAX_VARIABLE_NUMBER=250000 \
	-DSQLITE_MAX_EXPR_DEPTH=10000 \
	-DSQLITE_ENABLE_MATH_FUNCTIONS"

  # build sqlite
  cd sqlite-src-$_srcver
  static-compat-configure \
	--disable-shared \
	--enable-fts3 \
	--enable-fts4 \
	--enable-fts5 \
	--enable-rtree \
	--enable-json1 \
	TCLLIBDIR="$static_compat_prefix"/lib/sqlite$pkgver
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

package() {
  source static-compat-environment
  cd sqlite-src-$_srcver
  make DESTDIR="${pkgdir}" install

  patchelf --remove-rpath "${pkgdir}/$static_compat_prefix"/bin/sqlite3
  install -D -m644 "${srcdir}"/license.txt "${pkgdir}"/usr/share/licenses/${pkgname}/license.txt
}
