# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

pkgbase="sqlite"
pkgname=('sqlite' 'libsqlite' 'libsqlite-devel' 'sqlite-doc'
        'tcl-sqlite'
         'sqlite-extensions'
         'lemon')
_sqlite_year=2021
_amalgamationver=3360000
pkgver=3.36.0
pkgrel=2
pkgdesc="A C library that implements an SQL database engine"
arch=('i686' 'x86_64')
license=(PublicDomain)
url="https://www.sqlite.org/"
depends=('libreadline' 'zlib' 'tcl')
makedepends=('libreadline-devel' 'icu-devel' 'zlib-devel')
source=(https://www.sqlite.org/${_sqlite_year}/sqlite-src-${_amalgamationver}.zip
        https://www.sqlite.org/${_sqlite_year}/sqlite-doc-${_amalgamationver}.zip
        LICENSE
        0010-sqlite-3.34.0-lemon.patch
        0001-sqlite3.32.3-configure.ac-fix-OS-recognition-on-MSYS2.patch
        0002-sqlite3.32.3-Makefile.in-fix-rule-compiling-rbu.exe.patch
        0007-sqlite3.36.0-Makefile.in-fix-libtclsqlite-package-installation-bug.patch
        Makefile.ext.in README.md.in)
sha256sums=('25a3b9d08066b3a9003f06a96b2a8d1348994c29cc912535401154501d875324'
            '79f03bf2b4afd845b66e7d07c7fa08c46a30c2484a4a5f8508db653c9da5d32d'
            '0b76663a90e034f3d7f2af5bfada4cedec5ebc275361899eccc5c18e6f01ff1f'
            'ee6bdd979a97205a1a2dd760e90c8588f45523cecb5256c0d927f24494f7fc9f'
            '589b7182343dba3dd8225c13ff1d7d275e5f714e7793bac1c88f7cfdd569d9e0'
            'b717c73804be44b82048d90bbb8e0c61ff081339a5757194e9769bd4d9c048e5'
            '10f30378e83423f8b84462c1dd5b58501913816865cfd595f69a7b9ddd7e4e2b'
            'f7da5d5fb4b4b3588fa59d7282244495afe546e01c73c2369e50c3930e461ea9'
            'ef3a1faa02a88e05539d160d51679f164dfa23cc5a637ff36bc504bf04d6d7a6')

_sqlite_tools="sqlite3.exe sqlite3_analyzer.exe sqldiff.exe dbhash.exe rbu.exe"

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

apply_patch_with_msg_p2() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp2 -i "${srcdir}/${_patch}"
  done
}



prepare() {

  msg2 "Preparing sqlite package..."
  cd "${srcdir}"/sqlite-src-${_amalgamationver}
  apply_patch_with_msg 0001-sqlite3.32.3-configure.ac-fix-OS-recognition-on-MSYS2.patch
  apply_patch_with_msg 0002-sqlite3.32.3-Makefile.in-fix-rule-compiling-rbu.exe.patch
  apply_patch_with_msg 0007-sqlite3.36.0-Makefile.in-fix-libtclsqlite-package-installation-bug.patch
  autoreconf -vfi

  msg2 "Preparing files for lemon.exe to be distributed..."
  cd "${srcdir}"
  cp -v sqlite-src-${_amalgamationver}/tool/lemon.c .
  cp -v sqlite-src-${_amalgamationver}/tool/lempar.c .
  patch -Np0 -i 0010-sqlite-3.34.0-lemon.patch
}

build() {
  msg2 "Building sqlite package..."
  export CPPFLAGS="$CPPFLAGS \
    -DSQLITE_ENABLE_DBSTAT_VTAB=1 \
    -DSQLITE_ENABLE_DBPAGE_VTAB=1 \
    -DSQLITE_ENABLE_STAT4=1 \
    -DSQLITE_ENABLE_UNLOCK_NOTIFY=1 \
    -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1 \
    -DSQLITE_OMIT_LOOKASIDE \
    -DSQLITE_SECURE_DELETE=1 \
    -DSQLITE_ENABLE_SESSION=1 \
    -DSQLITE_ENABLE_PREUPDATE_HOOK=1 \
    -DSQLITE_SOUNDEX=1 \
    -DSQLITE_TEMP_STORE=1 \
    -DSQLITE_USE_FCNTL_TRACE=1 \
    -DUSE_SYSTEM_SQLITE=1 \
    -DSQLITE_ENABLE_FTS3_PARENTHESIS=1 \
    -DSQLITE_ENABLE_COLUMN_METADATA=1 \
    -DSQLITE_USE_ALLOCA=1 \
    -DSQLITE_ENABLE_EXPLAIN_COMMENTS=1 \
    -DSQLITE_ENABLE_STMTVTAB=1 \
    -DSQLITE_HAVE_ZLIB=1 \
    -DSQLITE_ENABLE_MATH_FUNCTIONS=1 \
"

  mkdir -p build-${_amalgamationver}-${MSYSTEM_CHOST}
  cd build-${_amalgamationver}-${MSYSTEM_CHOST}
  ../sqlite-src-${_amalgamationver}/configure -C \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --build=${CHOST} \
    --enable-shared \
    --enable-static \
    --enable-readline --disable-editline \
    --enable-all

  make -j all ${_sqlite_tools}

  msg2 "Building sqlite extensions..."
  ./config.status --file=ext/misc/Makefile:../Makefile.ext.in 2> /dev/null
  make -j -C ext/misc
  msg2 "Building lemon.exe..."
  cd "${srcdir}"
  make lemon
}

check() {
  cd build-${_amalgamationver}-${MSYSTEM_CHOST}
  # This test run lasts very loooong ... despite the target name
  # sqlite 3.36.0: 54 errors out of 303582 tests on MSYS_NT-10.0-19043 64-bit little-endian
  # !Failures on these tests: attach-6.2 attach-6.2.2 journal3-1.2.2.1 journal3-1.2.2.4 journal3-1.2.3.1 journal3-1.2.3.4 journal3-1.2.4.1 journal3-1.2.4.4 symlink-1.1 symlink-1.1.2 symlink-1.1.4 symlink-1.2.2 symlink-1.2.3 symlink-1.3 symlink-2.1.2 symlink-2.1.4 symlink-2.1.6 symlink-2.2.2 symlink-2.2.4 symlink-2.2.6 symlink-4.2.1 symlink-4.2.2 symlink-4.3.1 symlink-4.3.2 symlink-4.4.2 tableapi-7.transient.37 tkt3457-1.3 triggerA-3.transient.94 triggerA-3.transient.118 triggerA-3.transient.119 triggerA-3.transient.275 triggerA-3.transient.305 triggerA-3.transient.306 triggerA-3.transient.317 triggerA-3.transient.318 triggerA-3.transient.319 triggerA-3.transient.320 wal2-12.2.2.1 wal2-12.2.2.4 wal2-12.2.3.1 wal2-12.2.3.4 wal2-12.2.4.1 wal2-12.2.4.4 wal2-13.3.1 wal2-13.4.1 wal2-13.5.1 wal2-13.7.1 wal2-13.7.3 wal2-13.7.4 wal2-13.8.1 wal2-13.8.3 wal2-13.8.4 wal2-13.9.1 wal2-13.9.2
  make quicktest || true
}
_install_license() {
  install -D -m644 ${srcdir}/LICENSE ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_sqlite() {
  pkgdesc="A C library that implements an SQL database engine"
  provides=("sqlite3=${pkgver}")
  replaces=("sqlite3")
  replaces+=('sqlite-icu' 'sqlite-rbu')
  depends=('libreadline' 'zlib')
  depends+=('tcl')  # sqlite3_analyzer.exe
  optdepends=("sqlite-extensions: various extensions for the SQL database engine ${pkgver}")

  mkdir -p ${pkgdir}/usr/bin
  cd build-${_amalgamationver}-${MSYSTEM_CHOST}/.libs
  cp -v ${_sqlite_tools}  ${pkgdir}/usr/bin/

  # documentation from TEA directory
  mkdir -p ${pkgdir}/usr/share/man/man1/
  cd ${srcdir}/sqlite-src-${_amalgamationver}
  cp autoconf/tea/doc/*  ${pkgdir}/usr/share/man/man1/

  _install_license
}

package_libsqlite() {
  pkgdesc="Sqlite3 library"
  groups=('libraries')
  options+=(!staticlibs)
  provides=("libsqlite=${pkgver}")
  # make pacman remove the following packages dependant on a certain version
  # of libsqlite which became obsolete since sqlite 3.33.0.
  conflicts=('sqlite-compress<3.33.0'
             'sqlite-icu<3.33.0'
             'sqlite-rbu<3.33.0'
             'sqlite-vfslog<3.33.0')

  cd build-${_amalgamationver}-${MSYSTEM_CHOST}
  make lib_install DESTDIR=${pkgdir}
  rm -fr ${pkgdir}/usr/lib

  _install_license
}

package_libsqlite-devel() {
  pkgdesc="Sqlite3 headers and libraries"
  groups=('development')
  depends=("libsqlite=${pkgver}" 'zlib-devel')
  optdepends=('tcl-sqlite: Tcl bindings for SQLite')
  options+=('staticlibs')

  cd build-${_amalgamationver}-${MSYSTEM_CHOST}
  make install DESTDIR=${pkgdir}
  rm -fr ${pkgdir}/usr/bin
  rm -fr ${pkgdir}/usr/lib/tcl*

  _install_license
}

package_sqlite-doc() {
  pkgdesc="Most of the static HTML files that comprise Sqlite ${pkgver} website, including all of the SQL Syntax and the C/C++ interface specs and other miscellaneous documentation"
  provides=("sqlite3-doc=${pkgver}")

  cd ${srcdir}/sqlite-doc-${_amalgamationver}
  mkdir -p ${pkgdir}/usr/share/doc/${pkgbase}
  cp -R *  ${pkgdir}/usr/share/doc/${pkgbase}/

  _install_license
}


package_sqlite-extensions() {
  pkgdesc="Various extensions for the SQL database engine ${pkgver}"
  depends=("sqlite=${pkgver}" "libsqlite=${pkgver}")
  # sqlite 3.32.3: The replaced packages are merged into 'sqlite-extensions'.
  replaces=('sqlite-compress' 'sqlite-vfslog')

  cd build-${_amalgamationver}-${MSYSTEM_CHOST}
  make -C ext/misc install DESTDIR=${pkgdir}

  mkdir -p "${pkgdir}/usr/share/sqlite/extensions/"
  cat "${srcdir}/README.md.in" |         \
    sed -e "s|@VERSION@|${pkgver}|g;"    \
    > "${pkgdir}/usr/share/sqlite/extensions/README.md"

  _install_license
}

package_tcl-sqlite() {
  pkgdesc="Tcl bindings for SQLite"
  depends=("libsqlite=${pkgver}" 'tcl') # tcl 8.6

  cd build-${_amalgamationver}-${MSYSTEM_CHOST}
  make tcl_install DESTDIR=${pkgdir}
  rm -fr ${pkgdir}/usr/bin
  find ${pkgdir}/usr/lib -type f -name '*.a' -delete

   _install_license
}

package_lemon() {
  pkgdesc="The LEMON LALR Parser Generator used in SQLite ${pkgver} (MSYS2)"

  mkdir -p ${pkgdir}/usr/{bin,share/lemon,share/licenses/lemon}
  cp -v lemon.exe  ${pkgdir}/usr/bin/
  cp -v lempar.c   ${pkgdir}/usr/share/lemon/
  cp -v LICENSE    ${pkgdir}/usr/share/licenses/lemon/
}
