# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0
PortGroup           openssl 1.0

github.setup        sqlcipher sqlcipher 4.14.0 v
revision            0
categories          databases
license             BSD

maintainers         {gmail.com:ybzhao1989 @z7z8th} openmaintainer

description         Security extesion to the embedded SQL database engine

long_description    SQLCipher extends the SQLite database library to add \
                    security enhancements that make it more suitable for \
                    encrypted local data storage like: \
                     * on-the-fly encryption \
                     * tamper detection \
                     * memory sanitization \
                     * strong key derivation. \
                    \
                    SQLite3 is an SQL database engine in a C library. \
                    Programs that link the SQLite3 library can have SQL \
                    database access without running a separate RDBMS \
                    process. The distribution comes with a standalone \
                    command-line access program (sqlite3) that can be used \
                    to administer an SQLite3 database and which serves \
                    as an example of how to use the SQLite3 library.

homepage            https://www.zetetic.net/sqlcipher

checksums           rmd160  8df8b8420a78e2ec408543c415772e7604c03a58 \
                    sha256  67fb27e967a4a6968c0905691c89c908e7250dddc581b887c19ef981c737e473 \
                    size    19024067
github.tarball_from archive

openssl.configure   build_flags

# Somehow upstream has broken OpenSSL linking in v. 4.7.0.
# Undefined symbols: "_EVP_CIPHER_CTX_new" etc.
configure.ldflags-append \
                    -lcrypto

# This sqlite3 bug landed into sqlcipher now.
# ld: in /opt/local/lib, can't map file, errno=22
patchfiles-append   no-rpath.patch

# enable sqlcipher security enhancements
configure.cppflags-prepend  -DSQLITE_HAS_CODEC

# These are required by the build now. SQLITE_TEMP_STORE can be 2 or 3.
# Needed for both subports.
configure.cppflags-prepend  -DSQLITE_EXTRA_INIT=sqlcipher_extra_init \
                            -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown \
                            -DSQLITE_TEMP_STORE=2

build.type              gnu

# Needed for both subports:
if {${os.major} <= 15} {
    # https://github.com/sqlcipher/sqlcipher/issues/518
    configure.cppflags-prepend \
                        -DSQLCIPHER_OMIT_LOG_DEVICE
}

if {${subport} eq ${name}} {
    depends_lib-append  port:libedit \
                        port:ncurses \
                        port:zlib

    # to enable `--enable-dynamic-extensions', re-genereate configure from configure.ac
    configure.args      --disable-readline \
                        --enable-editline \
                        --enable-threadsafe \
                        --prefix=${prefix}/libexec/${name} \
                        --with-readline-inc=-I${prefix}/include/editline \
                        AWK=/usr/bin/awk

    if {${os.major} <= 16} {
        configure.args-prepend  --disable-tcl
    }

    # search in worksrcpath for sqlcipher/sqlite3.h first -- don't pick up an installed one!
    configure.cppflags-prepend  -DSQLITE_DISABLE_INTRINSIC \
                                -DSQLITE_ENABLE_COLUMN_METADATA \
                                -DSQLITE_SECURE_DELETE \
                                -DSQLITE_ENABLE_FTS3_PARENTHESIS \
                                -DSQLITE_ENABLE_FTS4 \
                                -DSQLITE_ENABLE_FTS5 \
                                -DSQLITE_ENABLE_JSON1 \
                                -DSQLITE_ENABLE_RTREE \
                                -DSQLITE_ENABLE_STAT4 \
                                -DSQLITE_ENABLE_UNLOCK_NOTIFY \
                                -DSQLITE_SOUNDEX \
                                -I${worksrcpath}

    post-destroot {
        xinstall -m 644 ${worksrcpath}/${name}.1 ${destroot}${prefix}/share/man/man1
    }
}

subport ${name}-tools {
    description               A bundle of command-line tools for managing SQLite \
                              database files

    long_description          ${description}, including the sqldiff program \
                              and the sqlite3_analyzer program.

    conflicts                 sqlite3-tools

    # This is incredible: config file deliberately undefines a macro which is then
    # explicitly required by the build. How come this was not tested?
    # sqlite3_analyzer.c:107601:2: error: #error "SQLCipher must be compiled with -DSQLITE_THREADSAFE=<1 or 2>"
    post-patch {
        reinplace "s|SQLITE_THREADSAFE 0|SQLITE_THREADSAFE 1|" ${worksrcpath}/tool/sqlite3_analyzer.c.in
    }

    depends_lib-append        port:tcl \
                              port:zlib

    configure.args            --with-tcl=${prefix}/lib

    configure.cppflags-prepend \
                              -DSQLITE_THREADSAFE=1

    build.target              sqldiff sqlite3_analyzer

    destroot {
        xinstall -m 0755 ${worksrcpath}/sqldiff ${destroot}${prefix}/bin
        xinstall -m 0755 ${worksrcpath}/sqlite3_analyzer ${destroot}${prefix}/bin
    }
}
