# Maintainer: Jaroslav Lichtblau <svetlemodry@archlinux.org>
# Maintainer: Carl Smedstad <carsme@archlinux.org>
# Contributor: flu

pkgname=sqlcipher
pkgver=4.13.0
pkgrel=1
pkgdesc="SQLite extension that provides transparent 256-bit AES encryption of database files"
arch=('x86_64' 'aarch64')
url="https://www.zetetic.net/sqlcipher"
license=('BSD-3-Clause')
depends=(
  'glibc'
  'openssl'
  'readline'
  'tcl'
  'zlib'
)
makedepends=('patchelf')
source=("https://www.zetetic.net/downloads/sqlcipher/verify/$pkgver/$pkgname-$pkgver.zip"{,.sig})
b2sums=('5bd401df93e0703f1613fdaded1f59f6d36f405908e65b5e88f34594ef35c382cecb6b416344b03c8b02edb5edd602a4efb9754d1e56adbc61d07fb5bf236abc'
        'SKIP')
validpgpkeys=('D83F5F9EB811D6E6B4A0D9C5D1FA3A2A97ED25C2') # Zetetic LLC <support@zetetic.net>

build() {
  cd $pkgname-$pkgver

  # 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_MAX_VARIABLE_NUMBER=250000 \
    -DSQLITE_MAX_EXPR_DEPTH=10000 \
    -DSQLITE_ENABLE_MATH_FUNCTIONS"

  export CFLAGS="$CFLAGS \
    -DSQLITE_HAS_CODEC \
    -DSQLITE_TEMP_STORE=2 \
    -DSQLCIPHER_TEST \
    -DSQLITE_EXTRA_INIT=sqlcipher_extra_init \
    -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown"
  export LDFLAGS="$LDFLAGS -lcrypto -Wl,-soname,libsqlcipher.so.0"

  # Use sqlcipher naming to avoid conflicts with sqlite package
  ./configure --prefix=/usr \
    --with-tempstore=yes \
    --disable-static \
    --dll-basename=libsqlcipher \
    --fts3 \
    --fts4 \
    --fts5 \
    --rtree
  make
  make testfixture
}

check() {
  cd $pkgname-$pkgver
  ./testfixture test/sqlcipher.test
}

package() {
  cd $pkgname-$pkgver
  make DESTDIR="$pkgdir" install

  # Rename binary to avoid conflict with sqlite
  mv -v "$pkgdir/usr/bin/sqlite3" "$pkgdir/usr/bin/sqlcipher"

  # Move headers to sqlcipher subdirectory to avoid conflict with sqlite
  install -vd "$pkgdir/usr/include/sqlcipher"
  mv -v "$pkgdir/usr/include/sqlite3.h" "$pkgdir/usr/include/sqlcipher/"
  mv -v "$pkgdir/usr/include/sqlite3ext.h" "$pkgdir/usr/include/sqlcipher/"

  # Fix TCL module: rename to match old package layout and fix soname
  mv -v "$pkgdir/usr/lib/tcl8.6/sqlite3.51.2" "$pkgdir/usr/lib/tcl8.6/sqlite3"
  mv -v "$pkgdir/usr/lib/tcl8.6/sqlite3/libsqlite3.51.2.so" \
    "$pkgdir/usr/lib/tcl8.6/sqlite3/libtclsqlite3.so"
  patchelf --set-soname libtclsqlite3.so \
    "$pkgdir/usr/lib/tcl8.6/sqlite3/libtclsqlite3.so"

  # Install sqlcipher man page instead of sqlite3
  rm -v "$pkgdir/usr/share/man/man1/sqlite3.1"
  install -vDm644 sqlcipher.1 "$pkgdir/usr/share/man/man1/sqlcipher.1"

  # Fix pkg-config file: rename and update references
  mv -v "$pkgdir/usr/lib/pkgconfig/sqlite3.pc" "$pkgdir/usr/lib/pkgconfig/sqlcipher.pc"
  sed -i -e 's/-lsqlite3/-lsqlcipher/g' \
    -e 's|includedir=.*|includedir=${prefix}/include/sqlcipher|' \
    "$pkgdir/usr/lib/pkgconfig/sqlcipher.pc"

  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE.md
}
