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

pkgbase="sqlite"
pkgname=('sqlite' 'libsqlite' 'libsqlite-devel' 'sqlite-doc')
_amalgamationver=3210000
_docver=${_amalgamationver}
pkgver=3.21.0
pkgrel=4
pkgdesc="A C library that implements an SQL database engine"
arch=('i686' 'x86_64')
license=(PublicDomain)
url="https://www.sqlite.org/"
makedepends=('libreadline-devel' 'icu-devel' 'zlib-devel')
source=( # tarball containing the amalgamation for SQLite >= 3.7.5 together with a configure script and makefile for building it; includes now also the Tcl Extension Architecture (TEA)
        https://www.sqlite.org/2017/sqlite-autoconf-${_amalgamationver}.tar.gz
        https://www.sqlite.org/2017/sqlite-doc-${_docver}.zip
        LICENSE
        'sqlite3-3.21.0-1.src.patch'
        'sqlite-3.19.3-msys2.patch')
options=('!libtool' '!emptydirs' '!debug' 'strip')
sha256sums=('d7dd516775005ad87a57f428b6f86afd206cb341722927f104d3f0cf65fbbbe3'
            '78c2fc9b144b168c7df53ff192c84fa7c29bcc44324b48b0f809a13810bc6c36'
            '1e188e0cf2088da923bff80c76a9f13b7028d4688d2d49cdb10562718068ee98'
            '3701ae88ade7791707f99030c828f49c7a7e58be20e1a89ed7a86559cbe313f7'
            '843cf2e8197ac7b634dc751e122ca4020085cdd64a68e993bfbb75a35907208f')

# 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
}


del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  cd "${srcdir}"/sqlite-autoconf-${_amalgamationver}
  del_file_exists README amatch.c anycollseq.c carray.c closure.c compress.c csv.c \
    eval.c fileio.c fuzzer.c icu.c ieee754.c json1.c \
    memvfs.c nextchar.c percentile.c \
    rbu.c regexp.c remember.c rot13.c \
    scrub.c series.c sha1.c shathree.c showauth.c spaceanal.tcl spellfix.c sqldiff.c sqlite3rbu.c sqlite3rbu.h \
    totype.c vfslog.c vfsstat.c vtshim.c wholenumber.c

  apply_patch_with_msg_p2 sqlite3-3.21.0-1.src.patch
  apply_patch_with_msg sqlite-3.19.3-msys2.patch

  autoreconf -fi
}

build() {
  export CPPFLAGS="$CPPFLAGS \
    -DHAVE_MALLOC_H \
    -DHAVE_MALLOC_USABLE_SIZE \
    -DSQLITE_ENABLE_DBSTAT_VTAB=1 \
    -DSQLITE_ENABLE_STAT4=1 \
    -DSQLITE_ENABLE_UNLOCK_NOTIFY=1 \
    -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1 \
    -DSQLITE_OMIT_LOOKASIDE \
    -DSQLITE_OS_UNIX=1 \
    -DSQLITE_OS_WIN=1 \
    -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"

  cd "${srcdir}"/sqlite-autoconf-$_amalgamationver
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --build=${CHOST} \
    --enable-shared \
    --enable-static \
    --enable-readline --disable-editline \
    --enable-fts5 --enable-json1

  make
  make DESTDIR=${srcdir}/dest install
}

_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"
  depends=('libreadline' 'libsqlite')
  provides=("sqlite3=${pkgver}")
  replaces=("sqlite3")

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  _install_license
}

package_libsqlite() {
  pkgdesc="Sqlite3 library"
  depends=('libreadline' 'icu>=59.1' 'zlib')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  _install_license
}

package_libsqlite-devel() {
  pkgdesc="Sqlite3 headers and libraries"
  groups=('development')
  depends=("libsqlite=${pkgver}")
  options+=('staticlibs')

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
  _install_license
}

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")
  depends=("sqlite=${pkgver}")

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

  # fix permissions and remove obsolete files; https://bugs.archlinux.org/task/24605
  find ${pkgdir} -type f -perm 755 -exec ls -lha {} \;
  find ${pkgdir} -type f -perm 755 -exec chmod 644 {} \;

  find ${pkgdir} -type f -name '*~' -exec ls -lha {} \;
  find ${pkgdir} -type d -name '*~' -exec ls -lha {} \;
  find ${pkgdir} -name '*~' -exec rm -f {} \;

  find ${pkgdir} -type f -name '.~*' -exec ls -lha {} \; # /build/pkg/sqlite-doc/usr/share/doc/sqlite/images/fileformat/.~lock.indexpage.odg#
  find ${pkgdir} -type d -name '.~*' -exec ls -lha {} \;
  find ${pkgdir} -name '.~*' -exec rm -f {} \;

  _install_license
}
