# Contributor: Andreas Radke <andyrtr@archlinux.org>
# Contributor: Tom Newsom <Jeepster@gmx.co.uk>
# Contributor: Biru Ionut <ionut@archlinux.ro>
# Contributor: Mikko Seppälä <t-r-a-y@mbnet.fi>
# Contributor: Kaos < gianlucaatlas dot gmail dot com >

pkgbase="lib32-sqlite"
pkgname=('lib32-sqlite')
_srcver=3450100
pkgver=3.45.2
pkgrel=1
pkgdesc="A C library that implements an SQL database engine (32-bit)"
arch=('x86_64')
license=('custom:Public Domain')
url="https://www.sqlite.org/"
makedepends=('tcl' 'lib32-readline' 'sqlite')
options=('!emptydirs')
source=(https://www.sqlite.org/2024/sqlite-src-${_srcver}.zip
        license.txt)
# upstream now switched to sha3sums - currently not supported by makepkg
sha256sums=('7f7b14a68edbcd4a57df3a8c4dbd56d2d3546a6e7cdd50de40ceb03af33d34ba'
            '4e57d9ac979f1c9872e69799c2597eeef4c6ce7224f3ede0bf9dc8d217b1e65d')

prepare() {
  cd sqlite-src-$_srcver

  #autoreconf -vfi
}

build() {
  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  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_ENABLE_STAT4 \
	-DSQLITE_MAX_VARIABLE_NUMBER=250000 \
	-DSQLITE_MAX_EXPR_DEPTH=10000 \
	-DSQLITE_ENABLE_MATH_FUNCTIONS"

  # build sqlite
  cd sqlite-src-$_srcver

  export CC="gcc -m32"
  export CXX="g++ -m32"
  export PKG_CONFIG="i686-pc-linux-gnu-pkg-config"

  ./configure --prefix=/usr \
	--libdir=/usr/lib32 \
	--disable-static \
	--enable-fts3 \
	--enable-fts4 \
	--enable-fts5 \
	--enable-rtree \
	--disable-tcl \
	TCLLIBDIR=/usr/lib/sqlite$pkgver
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

package_lib32-sqlite() {

 pkgdesc="A C library that implements an SQL database engine (32-bit)"
 depends=('lib32-glibc' 'sqlite')
 provides=('libsqlite3.so')

  cd sqlite-src-$_srcver
  make DESTDIR="${pkgdir}" install

  rm -r "${pkgdir}"/usr/{bin,include}

  # license - no linking required because pkgbase=pkgname
  install -D -m644 "${srcdir}"/license.txt "${pkgdir}"/usr/share/licenses/${pkgbase}/license.txt
}
