#!/usr/bin/bash
# JOBoRun		: Jwm OpenBox Obarun RUNit
# Maintainer	: Joe Bo Run <joborun@disroot.org>
# PkgSource		: url="https://gittea.disroot.org/joborun-pkg/jobcore/$pkgname"
# Website		: https://pozol.eu
#-----------------------------------------| DESCRIPTION |---------------------------------------

pkgbase="sqlite"
pkgname=('sqlite' 'sqlite-tcl' 'sqlite-analyzer' 'lemon' 'sqlite-doc')
_srcver=3420000
_docver=${_srcver}
pkgver=3.42.0
pkgrel=01
pkgdesc="A C library that implements an SQL database engine"
url="https://www.sqlite.org/"
makedepends=('tcl' 'readline' 'zlib')
#options=('!emptydirs' 'debug')  ## uncomment this to produce the debug pkg
options=('!emptydirs')
source=(https://www.sqlite.org/2023/sqlite-src-${_srcver}.zip
        https://www.sqlite.org/2023/sqlite-doc-${_docver}.zip
        sqlite-lemon-system-template.patch
        license.txt)
options=('!emptydirs') # '!makeflags') # json extensions breaks parallel build
# upstream now switched to sha3sums - currently not supported by makepkg

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() {
  export CPPFLAGS="$CPPFLAGS \
	-DSQLITE_ENABLE_COLUMN_METADATA=1 \
	-DSQLITE_ENABLE_UNLOCK_NOTIFY \
	-DSQLITE_ENABLE_DBSTAT_VTAB=1 \
	-DSQLITE_ENABLE_FTS3_TOKENIZER=1 \
	-DSQLITE_ENABLE_FTS3_PARENTHESIS \
	-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
  ./configure --prefix=/usr \
	--disable-static \
	--enable-fts3 \
	--enable-fts4 \
	--enable-json1 \
	--enable-fts5 \
	--enable-rtree \
	TCLLIBDIR=/usr/lib/sqlite$pkgver
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
  # build additional tools
  make showdb showjournal showstat4 showwal sqldiff sqlite3_analyzer
}

package_sqlite() {

 pkgdesc="A C library that implements an SQL database engine"
 depends=('readline' 'zlib')
 provides=("sqlite3=$pkgver" 'libsqlite3.so')
 replaces=("sqlite3")

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

  install -m755 showdb showjournal showstat4 showwal sqldiff "${pkgdir}"/usr/bin/

  # install manpage
  install -m755 -d "${pkgdir}"/usr/share/man/man1
  install -m644 sqlite3.1 "${pkgdir}"/usr/share/man/man1/

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

  # split out tcl extension
  mkdir "$srcdir"/tcl
  mv "$pkgdir"/usr/lib/sqlite* "$srcdir"/tcl
}

package_sqlite-tcl() {

 pkgdesc="sqlite Tcl Extension Architecture (TEA)"
 depends=('sqlite')
 provides=("sqlite3-tcl=$pkgver")
 replaces=("sqlite3-tcl")

  install -m755 -d "${pkgdir}"/usr/lib
  mv "$srcdir"/tcl/* "${pkgdir}"/usr/lib

  # install manpage
  install -m755 -d "${pkgdir}"/usr/share/man/mann
  install -m644 "${srcdir}"/sqlite-src-$_srcver/autoconf/tea/doc/sqlite3.n "${pkgdir}"/usr/share/man/mann/

  # link license
  install -m755 -d "${pkgdir}"/usr/share/licenses
  ln -sf /usr/share/licenses/${pkgbase} "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_sqlite-analyzer() {

 pkgdesc="An analysis program for sqlite3 database files"
 depends=('sqlite' 'tcl')

  cd sqlite-src-$_srcver
  install -m755 -d "${pkgdir}"/usr/bin
  install -m755 sqlite3_analyzer "${pkgdir}"/usr/bin/
}

package_lemon() {

 # https://www.sqlite.org/lemon.html
 pkgdesc="A parser generator"
 depends=('glibc')

  cd sqlite-src-$_srcver
  # ELF file ('usr/bin/lemon') lacks FULL RELRO, check LDFLAGS. - no fix found so far
  install -Dm755 lemon ${pkgdir}/usr/bin/lemon
  install -Dm644 lempar.c ${pkgdir}/usr/share/lemon/lempar.c
  
  mkdir -p "${pkgdir}"/usr/share/doc/${pkgname}
  cp ../sqlite-doc-${_docver}/lemon.html  "${pkgdir}"/usr/share/doc/${pkgname}/
  install -m755 -d "${pkgdir}"/usr/share/licenses
  ln -sf /usr/share/licenses/${pkgbase} "${pkgdir}/usr/share/licenses/${pkgname}"

}

package_sqlite-doc() {

 pkgdesc="most of the static HTML files that comprise this website, including all of the SQL Syntax and the C/C++ interface specs and other miscellaneous documentation"
 #arch=('any') - not yet supported
 provides=("sqlite3-doc=$pkgver")
 replaces=("sqlite3-doc")

  cd sqlite-doc-${_docver}
  mkdir -p "${pkgdir}"/usr/share/doc/${pkgbase}
  cp -R *  "${pkgdir}"/usr/share/doc/${pkgbase}/
  
  rm "${pkgdir}"/usr/share/doc/${pkgbase}/lemon.html
}

#---- arch license gpg-key & sha256sums ----

arch=(x86_64)

license=('custom:Public Domain')

# From: https://www.sqlite.org/download.html
# sqlite-src-3420000.zip
# (13.27 MiB) Snapshot of the complete (raw) source tree for SQLite version 3.42.0. See How To Compile SQLite for usage details.
# (SHA3-256: f28b48f90cad0ec3c726ef99dfc44d52110e3e8cb829a309d4ffa77fe4958994)
# sqlite-doc-3420000.zip
# (10.17 MiB) Documentation as a bundle of static HTML files.
# (SHA3-256: 6dc0f3bb68bd9a541bbe9c75ec4b2304ba0857e706a21af5fea072136d46995e)

#sha3-256sums=(f28b48f90cad0ec3c726ef99dfc44d52110e3e8cb829a309d4ffa77fe4958994  # sqlite-src-3420000.zip
#	   6dc0f3bb68bd9a541bbe9c75ec4b2304ba0857e706a21af5fea072136d46995e  # sqlite-doc-3420000.zip
#	   37179a170dcf00d2562a69dbeabeae9f3ddeff3513e2a0a773331727ea7b3771  # sqlite-lemon-system-template.patch
#	   39258c3b8490cfc2ee8ca21fef08f588e67f2a36694fc8a5c140c28c3d1379f7) # license.txt


sha256sums=(38ca56a317be37fb00bd92bc280d9b9209bd4008b297d483c41ec1f6079bfb6d  # sqlite-src-3420000.zip
	d62fd5f0fcfd15b2c26171e89f762268e2863df3c09696e76cf1482e34db8262  # sqlite-doc-3420000.zip
	55746d93b0df4b349c4aa4f09535746dac3530f9fd6de241c9f38e2c92e8ee97  # sqlite-lemon-system-template.patch
	4e57d9ac979f1c9872e69799c2597eeef4c6ce7224f3ede0bf9dc8d217b1e65d) # license.txt

##  7956265de4edf9ca5afe87882a063243dde0179571d279278de9b88368b705a9  lemon-3.42.0-01-x86_64.pkg.tar.lz
##  bf4aa4a913695deb4df65440a437b387cc0b335ce6c86603736cf23a6e226c95  sqlite-3.42.0-01-x86_64.pkg.tar.lz
##  ddeb8ec3fcddb3306b2e08460cb1f86d46f88c941cb506ae1972bac95544157d  sqlite-analyzer-3.42.0-01-x86_64.pkg.tar.lz
##  665db56b5d7267d6d57f317aa77f1748a5826d0aa16dbe0da720eae15a482866  sqlite-doc-3.42.0-01-x86_64.pkg.tar.lz
##  79cb0967fe274dd190f5170ee8d807b92090e0eff9f780cb2b0baf61145ad68f  sqlite-tcl-3.42.0-01-x86_64.pkg.tar.lz

