##
##  valkey.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 8.1.3
%define       V_dist 8.1.3

#   package information
Name:         valkey
Summary:      Persistent Key-Value Database
URL:          http://valkey.io/
Vendor:       Salvatore Sanfilippo et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Database
License:      BSD
Version:      %{V_opkg}
Release:      20250707

#   package options
%option       with_cluster  no
%option       with_tls      no

#   list of sources
Source0:      https://github.com/valkey-io/valkey/archive/%{V_dist}.tar.gz
Source1:      rc.valkey
Source2:      valkey.conf
Source3:      valkey.master.conf
Source4:      valkey.slave.conf
Source5:      valkey-tls.conf
Source6:      valkey-tls.sh
Patch0:       valkey.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
%if "%{with_cluster}" == "yes"
PreReq:       ruby
PreReq:       valkey-libs
PreReq:       valkey-libs::with_ruby = yes
%endif
%if "%{with_tls}" == "yes"
PreReq:       stunnel
%endif

%description
    ValKey is an advanced key-value store and fork of Redis. It is
    similar to memcached but the dataset is not volatile, and values can
    be strings, exactly like in memcached, but also lists, sets, and
    ordered sets. All this data types can be manipulated with atomic
    operations to push/pop elements, add/remove elements, perform server
    side union, intersection, difference between sets, and so forth.
    ValKey supports different kind of sorting abilities.

%track
    prog valkey = {
        version   = %{V_dist}
        url       = https://github.com/valkey-io/valkey/releases
        regex     = tree/(\d+\.\d+(\.\d+)?)
    }

%prep
    %setup -q -n valkey-%{V_dist}
    %patch -p0

%build
    #   build program
    %{l_shtool} subst \
        -e 's;/var/run/valkey\.pid;%{l_prefix}/var/valkey/run/valkey.pid;' \
        src/server.h
    %{l_make} %{l_mflags -O} \
        CC="%{l_cc} %{l_cflags -O}" \
        DEBUG=""

%install
    #   create installation directories
    %{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/valkey \
        $RPM_BUILD_ROOT%{l_prefix}/var/valkey/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/valkey/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/valkey/db

    #   install programs
    %{l_shtool} install -c -s -m 755 \
        src/valkey-server \
        src/valkey-cli \
        src/valkey-benchmark \
        src/valkey-sentinel \
        src/valkey-check-aof \
        src/valkey-check-rdb \
        $RPM_BUILD_ROOT%{l_prefix}/bin/
%if "%{with_cluster}" == "yes"
    %{l_shtool} install -c -m 755 \
        -e 's;/usr/bin/env ruby;%{l_prefix}/bin/ruby;g' \
        src/valkey-trib.rb $RPM_BUILD_ROOT%{l_prefix}/bin/valkey-trib
%endif

    #   install run-command script
%if "%{with_cluster}" == "yes"
    cluster=yes
%else
    cluster=no
%endif
%if "%{with_tls}" == "yes"
    tls=yes
%else
    tls=no
%endif
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@cluster@;$cluster;g" \
        -e "s;@tls@;$tls;g" \
        %{SOURCE rc.valkey} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install default configuration
%if "%{with_cluster}" == "yes"
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE valkey.master.conf} \
        %{SOURCE valkey.slave.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/valkey/
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/valkey/db/master \
        $RPM_BUILD_ROOT%{l_prefix}/var/valkey/db/slave
%else
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE valkey.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/valkey/
%endif
%if "%{with_tls}" == "yes"
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE valkey-tls.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/valkey/
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE valkey-tls.sh} $RPM_BUILD_ROOT%{l_prefix}/etc/valkey/
%endif

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/valkey/*' \
%if "%{with_cluster}" == "yes"
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/valkey/*/*' \
%endif
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/valkey/*'

%files -f files

%clean

%post
%if "%{with_tls}" == "yes"
    if [ $1 -eq 1 ]; then
        #   on initial installation, generate initial credentials
        echo "Generate initial TLS credentials..." | \
            %{l_rpmtool} msg -b -t notice
        ( cd $RPM_INSTALL_PREFIX/etc/valkey && %{l_bash} valkey-tls.sh ) || exit $?
    fi
%endif
%if "%{with_cluster}" == "yes"
    if [ $1 -eq 1 ]; then
        #   display information about next steps
        ( echo "For setting up the ValKey cluster of N machines (N >= 3),"
          echo "start ValKey on all nodes and then perform the following command"
          echo "on one of the nodes:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/valkey-trib create\\%{l_nil}"
          echo "      --replicas 1 \\%{l_nil}"
          echo "      <node1>:6379 <node2>:6379 ... <nodeN>:6379 \\%{l_nil}"
          echo "      <node2>:3680 ... <nodeN>:3680 <node1>:3680"
          echo "The current nodes in the ValKey cluster can then be checked with:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/valkey-cli cluster nodes"
          echo "Notice: on each node, two ValKey servers are started: one in master"
          echo "mode, the other in slave mode. This is because a single ValKey"
          echo "instance can be just in one of the two possible modes per time."
          echo "With this and the above valkey-trib(8) schema you get N servers"
          echo "which effectively act in a master/master fashion and where 1"
          echo "server at maximum can break at an time."
        ) | %{l_rpmtool} msg -b -t notice
    fi
%endif
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} valkey status 2>/dev/null`
        [ ".$valkey_active" = .yes ] && %{l_rc} valkey restart
    fi
    exit 0

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

