##
##  memcached.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:         memcached
Summary:      Memory Caching Daemon
URL:          http://www.memcached.org/
Vendor:       Brad Fitzpatrick
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        System
License:      GPL
Version:      1.6.39
Release:      20250729

#   list of sources
Source0:      http://www.memcached.org/files/memcached-%{version}.tar.gz
Source1:      rc.memcached
Source2:      memcached.conf
Source3:      memcached-sasldb.sh
Patch0:       memcached.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, perl, gcc, make
PreReq:       OpenPKG, openpkg >= 20160101, perl
BuildPreReq:  libevent, sasl, libexecinfo
PreReq:       libevent, sasl, libexecinfo

%description
    Memcached is a high-performance, distributed memory object caching
    system, generic in nature, but intended for use in speeding up
    dynamic web applications by alleviating database load.

%track
    prog memcached = {
        version   = %{version}
        url       = http://www.memcached.org/
        regex     = memcached-(\d+(\.\d+)+)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   build package
    libs=""
    case "%{l_platform -t}" in
        *-linux* ) libs="$libs -lrt" ;;
    esac
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    LIBS="$libs" \
    ./configure \
        --prefix=%{l_prefix} \
        --with-libevent=%{l_prefix} \
        --enable-sasl \
        --enable-sasl-pwdb \
        --disable-coverage
    %{l_make} %{l_mflags -O}

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 775 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man8 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/memcached \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sasl/sasl.d \
        $RPM_BUILD_ROOT%{l_prefix}/var/memcached/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/memcached/log

    #   install program
    %{l_shtool} install -c -s -m 775 \
        memcached $RPM_BUILD_ROOT%{l_prefix}/sbin/
    %{l_shtool} install -c -m 644 \
        doc/memcached.1 $RPM_BUILD_ROOT%{l_prefix}/man/man8/memcached.8
    %{l_shtool} install -c -m 775 \
        -e 's;/usr/bin/perl;%{l_prefix}/bin/perl;g' \
        scripts/memcached-tool $RPM_BUILD_ROOT%{l_prefix}/bin/
    %{l_shtool} install -c -m 644 \
        scripts/memcached-tool.1 $RPM_BUILD_ROOT%{l_prefix}/man/man1/

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

    #   install default SASL configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE memcached.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sasl/sasl.d/

    #   install SASL database utility
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE memcached-sasldb.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/memcached-sasldb

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} `cat perl-openpkg-files` \
        '%config %attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/etc/memcached' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/memcached/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        echo "Initializing Memcached SASL database" | \
            %{l_rpmtool} msg -b -t notice
        su - %{l_nusr} -c \
            "echo memcached | $RPM_INSTALL_PREFIX/sbin/memcached-sasldb passwd -p memcached"
        #   display some initial hints, too
        (   echo "Configure your Memcached SASL users with:"
            echo "    \$ $RPM_INSTALL_PREFIX/sbin/memcached-sasldb passwd <username>"
            echo "A standard username/password pair is pre-existing:"
            echo "    memcached/memcached"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} memcached status 2>/dev/null`
        [ ".$memcached_active" = .yes ] && %{l_rc} memcached restart
    fi
    exit 0

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

