##
##  sqlitedbms.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.
##

#   FIXME: rse: rc.sqlitedbms still missing
#   FIXME: rse: the sqlitedbmsd is still a little bit buggy under run-time!?

#   package information
Name:         sqlitedbms
Summary:      SQLite Database Management System
URL:          http://sqlitedbms.sourceforge.net/
Vendor:       Kibae Shin
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      MIT-style
Version:      0.5.1
Release:      20080101

#   list of sources
Source0:      http://download.sourceforge.net/sqlitedbms/sqlitedbms-%{version}.tar.gz
Source1:      sqlitedbms.conf
Patch0:       sqlitedbms.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  sqlite >= 3.3, pcre, openssl
PreReq:       sqlite >= 3.3, pcre, openssl

%description
    SQLiteDBMS is a client/server Relational Database Management System
    (RDBMS) based on the SQLite SQL and storage engine.

%track
    prog sqlitedbms = {
        version   = %{version}
        url       = http://sourceforge.net/projects/sqlitedbms/files/
        regex     = sqlitedbms-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure program
    %{l_shtool} subst \
        -e 's;conf/sqlitedbms.conf;%{l_prefix}/etc/sqlitedbms/sqlitedbms.conf;' \
        src/backend/include/define.h
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    LIBS="-lpcreposix -lpcre" \
    ./configure \
        --prefix=%{l_prefix} \
        --with-sqlite3=%{l_prefix} \
        --with-openssl=%{l_prefix} \
        --disable-shared

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

%install
    #   install program
    %{l_make} %{l_mflags} install \
        prefix=$RPM_BUILD_ROOT%{l_prefix} \
        exec_prefix=$RPM_BUILD_ROOT%{l_prefix}

    #   adjust installation
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share
    mv  $RPM_BUILD_ROOT%{l_prefix}/htdocs \
        $RPM_BUILD_ROOT%{l_prefix}/share/sqlitedbms

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/conf
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/data

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sqlitedbms
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE sqlitedbms.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sqlitedbms/

    #   create run-time directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/sqlitedbms/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/sqlitedbms/rep \
        $RPM_BUILD_ROOT%{l_prefix}/var/sqlitedbms/log

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/sqlitedbms/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/sqlitedbms/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   create initial database in batch
        if [ ! -f $RPM_INSTALL_PREFIX/var/sqlitedbms/db/catalog.user.superdb ]; then
            ( echo "CREATE TABLE USER (id text PRIMARY KEY, pass text, super int);"
              echo "CREATE TABLE GRANT_DB (id text, db text, grant text, PRIMARY KEY(id, db));"
              echo "CREATE TABLE GRANT_OBJECT (id text, db text, object text, grant text, PRIMARY KEY(id, db, object));"
              echo "INSERT INTO USER VALUES('admin', 'admin', 1);"
            ) | su - %{l_rusr} -c \
                $RPM_INSTALL_PREFIX/bin/sqlite3 \
                $RPM_INSTALL_PREFIX/var/sqlitedbms/db/catalog.user.superdb
        fi
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} sqlitedbms status 2>/dev/null`
        [ ".$sqlitedbms_active" = .yes ] && %{l_rc} sqlitedbms restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        %{l_rc} sqlitedbms stop 2>/dev/null
        rm -f $RPM_INSTALL_PREFIX/var/sqlitedbms/rep/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/sqlitedbms/log/* >/dev/null 2>&1 || true
    fi
    exit 0

