##
##  fourstore.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:         fourstore
Summary:      RDF Triple Database
URL:          http://4store.org/
Vendor:       Steve Harris et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      GPL
Version:      1.1.5
Release:      20141012

#   list of sources
Source0:      http://4store.org/download/4store-v%{version}.tar.gz
Source1:      rc.fourstore
Patch0:       fourstore.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, pkgconfig, make
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  redland, redland-raptor, redland-rasqal
PreReq:       redland, redland-raptor, redland-rasqal
BuildPreReq:  pcre, glib, libxml, libxslt, zlib, openssl, curl, libuuid
PreReq:       pcre, glib, libxml, libxslt, zlib, openssl, curl, libuuid
BuildPreReq:  readline, ncurses
PreReq:       readline, ncurses

%description
    4store is an efficient, scalable and stable database for RDF triples
    to underpin Semantic Web applications. 4store includes a SPARQL HTTP
    protocol server, which can answer SPARQL queries using the standard
    SPARQL HTTP query protocol among other features.

%track
    prog fourstore = {
        version   = %{version}
        url       = http://4store.org/download/
        regex     = 4store-v(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n 4store-v%{version}
    %patch -p0

%build
    #   configure programs
    %{l_shtool} subst \
        -e 's;/var/log/4store;%{l_prefix}/var/fourstore/log;' \
        src/http/httpd.c
    %{l_shtool} subst \
        -e 's;-Wall;;g' \
        -e 's;-g;;g' \
        configure
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags redland e2fs .}" \
    LDFLAGS="%{l_ldflags e2fs .}" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --with-storage-path=%{l_prefix}/var/fourstore/db \
        --with-config-file=%{l_prefix}/fourstore.conf \
        --disable-shared

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

%install
    #   install programs and manual pages
    %{l_shtool} subst \
        -e 's;/usr/local;%{l_prefix};g' \
        -e 's;share/man/man1;man/man1;g' \
        `find . -name Makefile -print`
    %{l_make} %{l_mflags} install \
        DESTDIR=$RPM_BUILD_ROOT

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

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.fourstore} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   create initial database
        su - %{l_rusr} -c "umask 077; \
            $RPM_INSTALL_PREFIX/bin/4s-backend-setup \
            default"
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} fourstore status 2>/dev/null`
        [ ".$fourstore_active" = .yes ] && %{l_rc} fourstore restart
    fi
    exit 0

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

