##
##  neo4j.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 version
%define       V_opkg  4.4.12
%define       V_dist  4.4.12

#   package information
Name:         neo4j
Summary:      Graph Database
URL:          http://neo4j.org/
Vendor:       Neo Technology
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Database
License:      AGPL
Version:      %{V_opkg}
Release:      20221011

#   list of sources
Source0:      http://dist.neo4j.org/neo4j-community-%{V_dist}-unix.tar.gz
Source1:      neo4j-server.sh
Source2:      neo4j-shell.sh
Source3:      neo4j-admin.sh
Source4:      neo4j-admin-password.sh
Source5:      neo4j-import.sh
Source6:      cypher-shell.sh
Source7:      neo4j-log4j.properties
Source8:      neo4j.conf
Source9:      rc.neo4j

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  java, JAVA-JDK
PreReq:       java, JAVA-JDK

%description
    Neo4J is a graph database, a fully transactional database that
    stores data structured as graphs. A graph is a flexible data
    structure that allows for a more agile and rapid style of
    development. You work with a flexible graph network consisting
    of nodes, relationships and properties. Neo4J has a disk-based,
    native storage manager completely optimized for storing graph
    structures for maximum performance and scalability. It allows
    massive scalability. Neo4J can handle graphs of several billion
    nodes/relationships/properties on a single machine and can be
    sharded to scale out across multiple machines. Neo4J also provides a
    powerful traversal framework for high-speed traversals in the node
    space. Neo4J can be used an an embedded database or as a standlone
    server speaking Bolt and REST.

%track
    prog neo4j = {
        version   = %{V_dist}
        url       = https://neo4j.com/download-center/
        regex     = release=(\d+\.\d+\.\d+)[^-]
    }

%prep
    %setup -q -n neo4j-community-%{V_dist}

%build

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/neo4j/neo4j-tls.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/neo4j/neo4j-tls.trusted.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/neo4j/neo4j-tls.revoked.d \
        $RPM_BUILD_ROOT%{l_prefix}/lib/neo4j/server \
        $RPM_BUILD_ROOT%{l_prefix}/lib/neo4j/client \
        $RPM_BUILD_ROOT%{l_prefix}/lib/neo4j/plugins \
        $RPM_BUILD_ROOT%{l_prefix}/var/neo4j/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/neo4j/import \
        $RPM_BUILD_ROOT%{l_prefix}/var/neo4j/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/neo4j/log

    #   install program components
    %{l_shtool} install -c -m 644 \
        lib/*.jar $RPM_BUILD_ROOT%{l_prefix}/lib/neo4j/server/
    %{l_shtool} install -c -m 644 \
        bin/tools/cypher-shell.jar $RPM_BUILD_ROOT%{l_prefix}/lib/neo4j/client/

    #   install startup wrappers
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE neo4j-server.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/neo4j-server
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE neo4j-shell.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/neo4j-shell
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE neo4j-admin.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/neo4j-admin
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE neo4j-admin-password.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/neo4j-admin-password
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE neo4j-import.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/neo4j-import
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE cypher-shell.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/cypher-shell

    #   install configuration files
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE neo4j-log4j.properties} \
        %{SOURCE neo4j.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/neo4j/

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

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/neo4j' \
        '%config %attr(644,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/neo4j/neo4j.conf' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/neo4j/neo4j-tls.d' \
        '%attr(755,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/neo4j/neo4j-tls.*.d' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/neo4j/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display information about next steps
        (   echo "The Neo4J administrator is \"neo4j\" with default password \"neo4j\"."
            echo "You should change this with the following command before starting Neo4J:"
            echo "    \$ $RPM_INSTALL_PREFIX/bin/neo4j-admin-password <password>"
            echo "If you do not initially change it, you are forced to change it later in"
            echo "cypher-shell(1) or the Neo4J Browser. In cypher-shell(1) change it with:"
            echo "    neo4j> CALL dbms.security.changePassword(\"<password>\");"
            echo ""
            echo "Then start Neo4J with..."
            echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc neo4j start"
            echo "...and connect to Neo4J Browser under:"
            echo "    http://localhost:7474/browser/"
            echo "    https://localhost:7473/browser/"
            echo "...or on the command-line with:"
            echo "    \$ $RPM_INSTALL_PREFIX/bin/cypher-shell \\%{l_nil}"
            echo "      -a bolt://127.0.0.1:7687 -u neo4j [-p <password>]"
            echo ""
            echo "You can create a new user in cypher-shell(1) with:"
            echo "    neo4j> CALL dbms.security.createUser("
            echo "           \"<username>\", \"<password>\", false);"
            echo ""
            echo "The Neo4J Bolt (with/without TLS) endpoint is under:"
            echo "    bolt://localhost:7687/"
            echo "The Neo4J Transactional Cypher HTTP endpoint is under:"
            echo "    http://localhost:7474/db/data/transaction"
            echo "    https://localhost:7473/db/data/transaction"
            echo "The Neo4J Legacy Cypher HTTP endpoint is under:"
            echo "    http://localhost:7474/db/data/cypher"
            echo "    https://localhost:7473/db/data/cypher"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    exit 0

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

