##
##  mongodb.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:         mongodb
Summary:      MongoDB Document Database
URL:          http://www.mongodb.org/
Vendor:       MongoDB, Inc.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Database
License:      SSPL
Version:      4.2.6
Release:      20200422

#   package options
%option       with_ssl  no

#   list of sources
Source0:      http://fastdl.mongodb.org/src/mongodb-src-r%{version}.tar.gz
Source1:      rc.mongodb
Source2:      mongodb.conf
Patch0:       mongodb.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, gcc::with_cxx = yes, python, python-yaml, python-www
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  libpcap, libexecinfo, curl
PreReq:       libpcap, libexecinfo, curl
%if "%{with_ssl}" == "yes"
BuildPreReq:  openssl, x509
PreReq:       openssl, x509
%endif

%description
    MongoDB bridges the gap between key-value stores (which are fast and
    highly scalable) and traditional RDBMS systems (which provide rich
    queries and deep functionality). MongoDB (from "humongous") is a
    scalable, high-performance, open source, document-oriented database.
    Written in C++, MongoDB features: Document-oriented storage, Full
    Index Support, Replication & High Availability, Auto-Sharding,
    Querying, Fast In-Place Updates, Map/Reduce, GridFS, etc.

%track
    prog mongodb = {
        version   = %{version}
        url       = https://www.mongodb.com/download-center/community
        regex     = mongodb-src-r(\d+\.[02468]\.\d+)\.tar\.gz
    }

%prep
    %setup -q -n mongodb-src-r%{version}
    %patch -p0

%build
    #   patch paths
    %{l_shtool} subst \
        -e 's;/data/db;%{l_prefix}/var/mongo/db;g' \
        buildscripts/resmokelib/config.py \
        debian/mongod.1 \
        etc/evergreen.yml \
        src/mongo/db/storage/storage_options.cpp \
        src/mongo/shell/replsettest.js \
        src/mongo/shell/servers.js \
        src/mongo/shell/servers_misc.js \
        src/mongo/shell/shardingtest.js \
        src/mongo/shell/utils.js

    #   build program
    %{l_shtool} mkdir -f -p -m 755 \
        `pwd`/scons-cache
    TERM=vt100 \
    PYTHON=%{l_prefix}/bin/python \
    python src/third_party/scons-*/scons.py \
        -Q -j 4 \
        --prefix=%{l_prefix} \
        --cache-dir=`pwd`/scons-cache \
        --release \
        --allocator=system \
        --disable-warnings-as-errors \
        --dbg=off \
        --opt=on \
%if "%{with_ssl}" == "yes"
        --ssl=SSL \
%endif
        CC="%{l_cc}" \
        CFLAGS="%{l_cflags -O}" \
        CXX="%{l_cxx}" \
        CXXFLAGS="%{l_cxxflags -O} -std=c++14 -I`pwd`" \
        CPPDEFINES="-D_GLIBCXX_USE_C99" \
        CPPPATH=%{l_prefix}/include \
        LINKFLAGS="%{l_ldflags}" \
        LIBPATH=%{l_prefix}/lib \
        LIBS="curl nghttp2 ssl crypto z" \
        .

%install
    #   install program
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin
    %{l_shtool} install -c -s -m 755 \
        mongo mongobridge mongod mongos mongotrafficreader \
        $RPM_BUILD_ROOT%{l_prefix}/bin/

    #   install manual pages
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1
    %{l_shtool} install -c -m 644 \
        debian/*.1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1/

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

    #   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.mongodb} $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/mongodb/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/mongodb/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/mongodb/run

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   create cluster key file
        apg -n1 -a1 -m64 -x64 -M NCL \
            >$RPM_INSTALL_PREFIX/etc/mongodb/mongodb.key
        chmod 700 $RPM_INSTALL_PREFIX/etc/mongodb/mongodb.key
        chown %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/etc/mongodb/mongodb.key

        #   optionally create bundled SSL certificate/key file
%if "%{with_ssl}" == "yes"
        cat $RPM_INSTALL_PREFIX/etc/x509/example-server.crt.pem \
            $RPM_INSTALL_PREFIX/etc/x509/example-server.key.pem \
            >$RPM_INSTALL_PREFIX/etc/mongodb/mongodb.pem
        chmod 700 $RPM_INSTALL_PREFIX/etc/mongodb/mongodb.pem
        chown %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/etc/mongodb/mongodb.pem
%endif

        #   display information about next steps
        ( echo "After initially starting MongoDB with..."
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mongodb start"
          echo "...you should immediately configure an administrator account:"
          echo "    \$ echo 'db.createUser({ user: \"admin\", pwd: \"<admin-password>\", \\ "
          echo "      roles: [ \"userAdmin\", \"userAdminAnyDatabase\", \\ "
          echo "      \"dbAdmin\", \"dbAdminAnyDatabase\", \"clusterAdmin\" ] });' | \\ "
          echo "      $RPM_INSTALL_PREFIX/bin/mongo --quiet admin"
          echo "Later create service account <name> for database <name>:"
          echo "    \$ (echo 'use <name>'; echo 'db.createUser({ user: \"<name>\", \\ "
          echo "      pwd: \"<name-password>\", roles: [ \"readWrite\", \"dbAdmin\" ] });') | \\ "
          echo "      $RPM_INSTALL_PREFIX/bin/mongo --quiet \\ "
          echo "      --username \"admin\" --password \"<admin-password>\" \\ "
          echo "      admin"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} mongodb status 2>/dev/null`
        [ ".$mongodb_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} mongodb stop 2>/dev/null
        rm -f  $RPM_INSTALL_PREFIX/var/mongodb/run/*       >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/mongodb/log/*       >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/mongodb/db/*        >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/etc/mongodb/mongodb.key >/dev/null 2>&1 || true
%if "%{with_ssl}" == "yes"
        rm -f  $RPM_INSTALL_PREFIX/etc/mongodb/mongodb.pem >/dev/null 2>&1 || true
%endif
    fi
    exit 0

