##
##  sqlcipher.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         sqlcipher
Summary:      Encrypted SQLite Variant
URL:          http://sqlcipher.net/
Vendor:       D. Richard Hipp, ZETETIC LLC
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Database
License:      MIT
Version:      4.0.0
Release:      20181130

#   package options
%option       with_debug           no
%option       with_readline        no
%option       with_threads         no
%option       with_fts3            no
%option       with_fts4            no
%option       with_fts5            no
%option       with_json1           no
%option       with_rtree           no
%option       with_odbc            no
%option       with_jdbc            no
%option       with_dlopen          no
%option       with_sqlite          no

#   list of sources
Source0:      https://github.com/sqlcipher/sqlcipher/archive/v%{version}.zip
Patch0:       sqlcipher.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, gawk, tcl
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  openssl
PreReq:       openssl
%if "%{with_readline}" == "yes"
BuildPreReq:  readline, pkgconfig
PreReq:       readline
%endif
%if "%{with_sqlite}" == "yes"
Provides:     sqlite = 3.11.0
%endif

%description
    SQLCipher is a variant of SQLite 3.11.0, providing an SQL database
    with full page encryption. SQLCipher is fully compatible to the
    SQLite API and provides transparent 256-bit AES encryption of
    database files. Pages are encrypted before being written to disk and
    are decrypted when read back.

%track
    prog sqlcipher = {
        version   = %{version}
        url       = https://github.com/sqlcipher/sqlcipher/releases
        regex     = v(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure program
    CC="%{l_cc}"
    CFLAGS="%{l_cflags -O}"
    CPPFLAGS="-I. %{l_cppflags}"
    LDFLAGS="-L. %{l_ldflags}"
    LIBS="-lm"
    export CC
    export CPPFLAGS
    export CFLAGS
    export LDFLAGS
    export LIBS
    GREP="grep" \
    %{l_bash} ./configure \
        --prefix=%{l_prefix} \
%if "%{with_threads}" == "yes"
        --enable-threadsafe \
%else
        --disable-threadsafe \
%endif
%if "%{with_readline}" == "yes"
        --disable-editline \
        --enable-readline \
        --with-readline-lib="`%{l_prefix}/bin/pkg-config --libs readline`" \
        --with-readline-inc="`%{l_prefix}/bin/pkg-config --cflags readline`" \
%else
        --disable-editline \
        --disable-readline \
%endif
%if "%{with_debug}" == "yes"
        --enable-debug \
%else
        --disable-debug \
%endif
%if "%{with_dlopen}" == "yes"
        --enable-load-extension \
%else
        --disable-load-extension \
%endif
%if "%{with_fts3}" == "yes"
        --enable-fts3 \
%endif
%if "%{with_fts4}" == "yes"
        --enable-fts4 \
%endif
%if "%{with_fts5}" == "yes"
        --enable-fts5 \
%endif
%if "%{with_json1}" == "yes"
        --enable-json1 \
%endif
%if "%{with_rtree}" == "yes"
        --enable-rtree \
%endif
        --disable-tcl \
        --disable-amalgamation \
        --disable-shared

    #   build program
    %{l_make} %{l_mflags -O}

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/lib \
        $RPM_BUILD_ROOT%{l_prefix}/include \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1

    #   install program
    %{l_make} %{l_mflags} install \
        prefix=$RPM_BUILD_ROOT%{l_prefix}
    %{l_shtool} install -c -m 644 \
        sqlcipher.1 $RPM_BUILD_ROOT%{l_prefix}/man/man1/sqlcipher.1

    #   strip down installation files
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   optionally show up as original SQLite
%if "%{with_sqlite}" == "yes"
    ln  $RPM_BUILD_ROOT%{l_prefix}/bin/sqlcipher \
        $RPM_BUILD_ROOT%{l_prefix}/bin/sqlite
    ln  $RPM_BUILD_ROOT%{l_prefix}/bin/sqlcipher \
        $RPM_BUILD_ROOT%{l_prefix}/bin/sqlite3
    ln  $RPM_BUILD_ROOT%{l_prefix}/lib/libsqlcipher.a \
        $RPM_BUILD_ROOT%{l_prefix}/lib/libsqlite3.a
    ln  $RPM_BUILD_ROOT%{l_prefix}/include/sqlcipher/sqlite3.h \
        $RPM_BUILD_ROOT%{l_prefix}/include/sqlite3.h
    ln  $RPM_BUILD_ROOT%{l_prefix}/include/sqlcipher/sqlite3ext.h \
        $RPM_BUILD_ROOT%{l_prefix}/include/sqlite3ext.h
    ln  $RPM_BUILD_ROOT%{l_prefix}/lib/pkgconfig/sqlcipher.pc \
        $RPM_BUILD_ROOT%{l_prefix}/lib/pkgconfig/sqlite3.pc
    ln  $RPM_BUILD_ROOT%{l_prefix}/man/man1/sqlcipher.1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1/sqlite3.1
    ln  $RPM_BUILD_ROOT%{l_prefix}/man/man1/sqlcipher.1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1/sqlite.1
%endif

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std}

%files -f files

%clean

