##
##  cayley.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
%define       V_cayley_base  0.7.7
%define       V_cayley_snap  20201226

#   package information
Name:         cayley
Summary:      Graph Database Server
URL:          https://cayley.io/
Vendor:       Barak Michener et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Apache
Version:      %{V_cayley_base}.%{V_cayley_snap}
Release:      20201226

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/cayley/cayley-%{V_cayley_snap}.tar.xz
Source1:      rc.cayley

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go
PreReq:       OpenPKG, openpkg >= 20160101

%description
    Cayley is a graph inspired by the graph database behind Freebase
    and Google's Knowledge Graph. Its goal is to be a part of the
    developer's toolbox where Linked Data and graph-shaped data
    (semantic webs, social networks, etc) in general are concerned. It
    provides a REST API and a GraphQL query mechanism. It is based on an
    underlying embedded or external database.

%track
    prog cayley = {
        version   = %{V_cayley_base}
        url       = https://github.com/cayleygraph/cayley/releases
        regex     = v(\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q -n cayley

%build
    #   build program
    (   export GOPATH=`pwd`
        cd $GOPATH/src/github.com/cayleygraph/cayley
        go build -v -o cayley cmd/cayley/cayley.go
    ) || exit $?

%install
    #   create directory hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/share/cayley \
        $RPM_BUILD_ROOT%{l_prefix}/var/cayley/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/cayley/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/cayley/db

    #   install program
    %{l_shtool} install -c -s -m 755 \
        src/github.com/cayleygraph/cayley/cayley \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/

    #   install assets
    cp -rp src/github.com/cayleygraph/cayley/{docs,configurations} \
        $RPM_BUILD_ROOT%{l_prefix}/share/cayley/

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   create initial database
        su - %{l_rusr} -c \
            "LC_CTYPE=C; export LC_CTYPE; umask 077; \
            $RPM_INSTALL_PREFIX/sbin/cayley \
                init --db=leveldb --dbpath=$RPM_INSTALL_PREFIX/var/cayley/db"
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} cayley status 2>/dev/null`
        [ ".$cayley_active" = .yes ] && %{l_rc} cayley restart
    fi
    exit 0

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

