##
##  arangodb.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     3.3.20
%define       V_tarball  3.3.20

#   package information
Name:         arangodb
Summary:      Document/Graph/Key-Value Database
URL:          http://www.arangodb.org/
Vendor:       triAGENS GmbH
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Apache
Version:      %{V_opkg}
Release:      20181130

#   list of sources
Source0:      https://github.com/arangodb/arangodb/archive/v%{V_tarball}.tar.gz
Source1:      rc.arangodb
Source2:      arangod.conf
Source3:      arangosh.conf
Source4:      arangodump.conf
Source5:      arangomkdb.sh
Patch0:       arangodb.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc7, gcc7::with_cxx = yes, bison, python
PreReq:       OpenPKG, openpkg >= 20160101, x509
BuildPreReq:  readline, ncurses, openssl-threads, libexecinfo, boost
PreReq:       readline, ncurses, openssl-threads, libexecinfo, boost

%description
    ArangoDB is a distributed database with four main features:
    Multi-Model: documents, graphs and key-value pairs -— model your
    data as you see fit for your application; Joins: conveniently join
    what belongs together for flexible ad-hoc querying, less data
    redundancy; Transactions: easy application development keeping your
    data consistent and safe. No hassle in your client; Query Language:
    use the SQL-like ArangoDB Query Language (AQL) for powerful data
    retrival.

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

%prep
    %setup -q -n arangodb-%{V_tarball}
    %patch -p0

%build
    #   patch sources
    %{l_shtool} subst \
        -e 's;#!/bin/bash;#!%{l_bash};g' \
        `grep -r '#!/bin/bash' . | sed -e 's;:.*;;'`

    #   remove binary symlinks (which point to not existing files anyway)
    rm -rf js/node/node_modules/mocha/node_modules/.bin
    rm -rf js/node/node_modules/jshint/node_modules/.bin
    rm -rf js/node/node_modules/js-yaml/node_modules/.bin
    rm -rf js/node/node_modules/.bin

    #   provide tools as it is too hard to pass the correct flags
    #   to all sub-directory build processes of ArangoDB
    iflags="-I `pwd`/3rdParty/rocksdb/rocksdb/include"
    iflags="$iflags -I `pwd`/3rdParty/snappy/google-snappy-*"
    iflags="$iflags -I `pwd`/3rdParty/rocksdb/v5.6.X/include/rocksdb"
    iflags="$iflags -I `pwd`/3rdParty/rocksdb/v5.6.X/include"
    iflags="$iflags -I `pwd`/3rdParty/V8/v5*/include"
    iflags="$iflags -I `pwd`/3rdParty/V8/v5*/third_party/icu/source/common"
    iflags="$iflags -I `pwd`/3rdParty/V8/v5*/third_party/icu/source/i18n"
    iflags="$iflags -I `pwd`/3rdParty/libev/libev-*"
    iflags="$iflags -I `pwd`/3rdParty/zlib/zlib-*"
    iflags="$iflags -I `pwd`/3rdParty/curl/curl-*/include"
    lflags="-L `pwd`/build/3rdParty/rocksdb/rocksdb"
    lflags="$lflags -L `pwd`/build/3rdParty/snappy/google-snappy-*"
    lflags="$lflags -L `pwd`/build/3rdParty/V8/v5*/*.release/obj.target/third_party/icu"
    lflags="$lflags -L `pwd`/build/3rdParty/V8/v5*/*.release/obj.target/tools/gyp"
    lflags="$lflags -L `pwd`/build/3rdParty/libev"
    lflags="$lflags -L `pwd`/build/3rdParty/zlib/zlib-*"
    lflags="$lflags -L `pwd`/build/3rdParty/curl/curl-*"
    cflags="-std=gnu11 -Wno-sign-compare"
    cflags="$cflags %{l_cflags -O}"
    cflags="$cflags $iflags"
    cflags="$cflags %{l_cppflags openssl-threads .}"
    cflags="$cflags $lflags"
    cflags="$cflags %{l_ldflags openssl-threads .}"
    cxxflags="-std=gnu++11 -Wno-sign-compare"
    cxxflags="$cxxflags -D_GLIBCXX_USE_C99"
    cxxflags="$cxxflags %{l_cxxflags -O}"
    cxxflags="$cxxflags $iflags"
    cxxflags="$cxxflags %{l_cppflags openssl-threads .}"
    cxxflags="$cxxflags $lflags"
    cxxflags="$cxxflags %{l_ldflags openssl-threads .}"
    mkdir tool
    (   echo "#!/bin/sh"
        echo "exec %{l_prefix}/bin/gcc7 $cflags \${1+\"\$@\"}"
    ) >tool/cc
    (   echo "#!/bin/sh"
        echo "exec %{l_prefix}/bin/g++7 $cxxflags \${1+\"\$@\"}"
    ) >tool/c++
    chmod a+x tool/*
    PATH=`pwd`/tool:$PATH

    #   configure program
    mkdir build
    cd build
    cmake \
        -Wno-dev \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_C_COMPILER="cc" \
        -DCMAKE_C_FLAGS="%{l_cflags -O} %{l_cppflags openssl-threads .}" \
        -DCMAKE_CXX_COMPILER="c++" \
        -DCMAKE_CXX_FLAGS="%{l_cxxflags -O} %{l_cppflags openssl-threads .}" \
        -DCMAKE_EXE_LINKER_FLAGS="%{l_ldflags openssl-threads .}" \
        -DCMAKE_INSTALL_PREFIX="%{l_prefix}" \
        -DCMAKE_INSTALL_SYSCONFDIR_ARANGO="%{l_prefix}/etc/arangodb" \
        -DPYTHON_EXECUTABLE="%{l_prefix}/bin/python" \
        -DOPENSSL_ROOT_DIR="%{l_prefix}/lib/openssl-threads" \
        -DUSE_BACKTRACE=on \
        -DUSE_FAILURE_TESTS=off \
        -DUSE_MAINTAINER_MODE=off \
        -DUSE_BOOST_UNITTESTS=off \
        -DUSE_JEMALLOC=off \
        -DASM686=off \
        -DAMD64=off \
        ..

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

%install
    #   install program
    (   cd build
        %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT
    ) || exit $?

    #   post-adjust installation
    strip  $RPM_BUILD_ROOT%{l_prefix}/bin/*     >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/var/lib   >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/var/log   >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/var/db    >/dev/null 2>&1 || true

    #   install default configuration
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/arangodb/*
    mv  $RPM_BUILD_ROOT%{l_prefix}/etc/arangodb3 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/arangodb
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE arangod.conf} \
        %{SOURCE arangosh.conf} \
        %{SOURCE arangodump.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/arangodb/

    #   install utility
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_bash@;%{l_bash};g' \
        %{SOURCE arangomkdb.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/arangomkdb

    #   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.arangodb} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   provide SSL/TLS certificate/key pairs
        if [ ! -f $RPM_INSTALL_PREFIX/etc/arangodb/arangod-sv.pem ]; then
            cat $RPM_INSTALL_PREFIX/etc/x509/example-server.crt.pem \
                $RPM_INSTALL_PREFIX/etc/x509/example-server.key.pem \
                >$RPM_INSTALL_PREFIX/etc/arangodb/arangod-sv.pem
            chown %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/etc/arangodb/arangod-sv.pem
            chmod 600 $RPM_INSTALL_PREFIX/etc/arangodb/arangod-sv.pem
        fi
        if [ ! -f $RPM_INSTALL_PREFIX/etc/arangodb/arangod-ca.pem ]; then
            cat $RPM_INSTALL_PREFIX/etc/x509/example-ca.crt.pem \
                >$RPM_INSTALL_PREFIX/etc/arangodb/arangod-ca.pem
            chown %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/etc/arangodb/arangod-ca.pem
            chmod 644 $RPM_INSTALL_PREFIX/etc/arangodb/arangod-ca.pem
        fi

        #   display information about next steps
        ( echo "After initially starting ArangoDB with..."
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc arangodb start"
          echo "...you should immediately set the \"root\" password on database \"_system\":"
          echo "    \$ echo 'require(\"@arangodb/users\").update(\"root\", \"<root-pw>\");' | \\%{l_nil}"
          echo "      $RPM_INSTALL_PREFIX/bin/arangosh --server.authentication false"
          echo "You can then create a custom database with:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/arangomkdb <database> <username> <password>"
          echo "Then you can connect to the custom database with:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/arangosh \\%{l_nil}"
          echo "      --server.database <database> \\%{l_nil}"
          echo "      --server.username <username> \\%{l_nil}"
          echo "      --server.password <password>"
          echo "The web interface of ArangoDB you can reach under:"
          echo "    https://localhost:8529/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} arangodb status 2>/dev/null`
        [ ".$arangodb_active" = .yes ] && %{l_rc} mongodb restart
    fi
    exit 0

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

