# 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=2020
_amalgamationver=3340000
pkgver=3.34.0
pkgrel=1
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.32.3-Makefile.in-fix-libtclsqlite-package-installation-bug.patch
        Makefile.ext.in README.md.in)
sha256sums=('a5c2000ece56d2de13c474658b9cdba6b7f2608a4d711e245518ea02a2a2333e'
            '1e86e24d3c1217b7ef10e67a9ff7f395d1e19a8beee732a46445b493c22979a8'
            '0b76663a90e034f3d7f2af5bfada4cedec5ebc275361899eccc5c18e6f01ff1f'
            'ee6bdd979a97205a1a2dd760e90c8588f45523cecb5256c0d927f24494f7fc9f'
            '589b7182343dba3dd8225c13ff1d7d275e5f714e7793bac1c88f7cfdd569d9e0'
            'b717c73804be44b82048d90bbb8e0c61ff081339a5757194e9769bd4d9c048e5'
            '12507108d633a6b0a01534e30bcc807a4dcd5c8f955e6336b9ebe505a986fce0'
            '572fe2e1492de5643018bcbb2ba7d69747d20a2b021c32cec868b87c92ccb18f'
            '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.32.3-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 \
    -DSQLITE_ENABLE_STMTVTAB \
    -DSQLITE_HAVE_ZLIB \
"

  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 \
    --enable-rtree

  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.34.0: 54 errors out of 302417 tests on fv-az15-18 MSYS_NT-10.0-17763 64-bit little-endian
  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/
}
