##
##  redis-libs.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_hiredis_tgz        0.14.1
%define       V_hiredis_dir        0.14.1
%define       V_perl_redis         1.981
%define       V_perl_redis_hiredis 0.11.0
%define       V_predis             1.1.1
%define       V_phpredis           5.3.2
%define       V_rubyredis          4.5.0

#   package information
Name:         redis-libs
Summary:      Redis Client Libraries
URL:          http://redis.io/clients
Vendor:       Salvatore Sanfilippo et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Open Source
Version:      0
Release:      20211015

#   package options
%option       with_perl   no
%option       with_php    no
%option       with_ruby   no

#   list of sources
Source0:      https://github.com/redis/hiredis/archive/v%{V_hiredis_tgz}.tar.gz
Source1:      http://www.cpan.org/authors/id/D/DA/DAMS/Redis-%{V_perl_redis}.tar.gz
Source2:      http://www.cpan.org/authors/id/N/NE/NEOPHENIX/Redis-hiredis-%{V_perl_redis_hiredis}.tar.gz
Source3:      http://github.com/nrk/predis/archive/v%{V_predis}.tar.gz
Source4:      https://github.com/phpredis/phpredis/archive/%{V_phpredis}.tar.gz
Source5:      https://rubygems.org/downloads/redis-%{V_rubyredis}.gem
Patch0:       redis-libs.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make
PreReq:       OpenPKG, openpkg >= 20160101
%if "%{with_perl}" == "yes"
BuildPreReq:  perl, perl-openpkg
PreReq:       perl
%endif
%if "%{with_php}" == "yes"
BuildPreReq:  php, php::with_session = yes
PreReq:       php, php::with_session = yes
%endif
%if "%{with_ruby}" == "yes"
BuildPreReq:  ruby, ruby-gems
PreReq:       ruby, ruby-gems
%endif

%description
    This is a set of Redis client libraries for the programming
    languages C, Java, Scala, Perl and PHP.

%track
    prog redis-libs:hiredis = {
        version   = %{V_hiredis_tgz}
        url       = https://github.com/redis/hiredis/releases
        regex     = v(__VER__)\.tar\.gz
    }
    prog redis-libs:perl-redis = {
        version   = %{V_perl_redis}
        url       = http://www.cpan.org/authors/id/M/ME/MELO/
        regex     = Redis-(__VER__)\.tar\.gz
    }
    prog redis-libs:perl-redis-hiredis = {
        version   = %{V_perl_redis_hiredis}
        url       = http://www.cpan.org/authors/id/N/NE/NEOPHENIX/
        regex     = Redis-hiredis-(__VER__)\.tar\.gz
    }
    prog redis-libs:predis = {
        version   = %{V_predis}
        url       = http://github.com/nrk/predis/releases
        regex     = v(__VER__)\.tar\.gz
    }
    prog redis-libs:phpredis = {
        version   = %{V_phpredis}
        url       = https://github.com/phpredis/phpredis/releases
        regex     = (\d+\.\d+\.\d+)\.tar\.gz
    }
    prog redis-libs:rubyredis = {
        version   = %{V_rubyredis}
        url       = https://github.com/redis/redis-rb/releases
        regex     = v(\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q -c
%if "%{with_perl}" == "yes"
    %setup -q -T -D -a 1
    %setup -q -T -D -a 2
%endif
%if "%{with_php}" == "yes"
    %setup -q -T -D -a 3
    %setup -q -T -D -a 4
%endif
    %patch -p0

%build
    ( cd hiredis-%{V_hiredis_dir}
      %{l_make} %{l_mflags} \
          CC="%{l_cc} %{l_cflags -O}" DEBUG="" \
          libhiredis.a
    ) || exit $?
%if "%{with_perl}" == "yes"
    ( cd Redis-%{V_perl_redis}
      %{l_prefix}/bin/perl-openpkg prepare
      %{l_prefix}/bin/perl-openpkg configure build
    ) || exit $?
    ( cd Redis-hiredis-%{V_perl_redis_hiredis}
      %{l_prefix}/bin/perl-openpkg prepare
      %{l_prefix}/bin/perl-openpkg configure build
    ) || exit $?
%endif
%if "%{with_php}" == "yes"
    ( cd phpredis-%{V_phpredis}
      %{l_prefix}/bin/phpize
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      LIBS="-lssl -lcrypto" \
      GREP="grep" \
      ./configure \
          --prefix=%{l_prefix} \
          --with-php-config=%{l_prefix}/bin/php-config \
          --enable-shared \
          --disable-static
      %{l_make} %{l_mflags -O}
    ) || exit $?
%endif

%install
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/include/redis/adapters \
        $RPM_BUILD_ROOT%{l_prefix}/lib
    %{l_shtool} install -c -m 755 \
        hiredis-%{V_hiredis_dir}/libhiredis.a \
        $RPM_BUILD_ROOT%{l_prefix}/lib/
    %{l_shtool} install -c -m 644 \
        hiredis-%{V_hiredis_dir}/*.h \
        $RPM_BUILD_ROOT%{l_prefix}/include/redis/
    %{l_shtool} install -c -m 644 \
        hiredis-%{V_hiredis_dir}/adapters/*.h \
        $RPM_BUILD_ROOT%{l_prefix}/include/redis/adapters/
%if "%{with_perl}" == "yes"
    %{l_prefix}/bin/perl-openpkg -d Redis-%{V_perl_redis} install
    %{l_prefix}/bin/perl-openpkg -d Redis-hiredis-%{V_perl_redis_hiredis} install
    %{l_prefix}/bin/perl-openpkg -F perl-openpkg-files fixate cleanup
%else
    >perl-openpkg-files
%endif
%if "%{with_php}" == "yes"
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/lib/redis/Predis
    cp -r predis-%{V_predis}/src/* \
        $RPM_BUILD_ROOT%{l_prefix}/lib/redis/Predis/
    %{l_shtool} install -c -m 755 \
        phpredis-%{V_phpredis}/.libs/redis.so \
        $RPM_BUILD_ROOT%{l_prefix}/lib/redis/phpredis.so
%endif
%if "%{with_ruby}" == "yes"
    gem install --build-root $RPM_BUILD_ROOT %{SOURCE redis-%{V_rubyredis}.gem}
%endif
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std} `cat perl-openpkg-files`

%files -f files

%clean

%post
%if "%{with_php}" == "yes"
    if [ ".$1" = .1 ]; then
        #   add hook to PHP configuration
        phpini="$RPM_INSTALL_PREFIX/etc/php/php.ini"
        if [ -f $phpini ]; then
            echo "extension = $RPM_INSTALL_PREFIX/lib/redis/phpredis.so" |\
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -a -i "$RPM_INSTALL_PREFIX:redis-libs" -p ";" $phpini
        fi
        #   add hook to PHP-FPM configuration
        phpini="$RPM_INSTALL_PREFIX/etc/php-fpm/php-fpm.ini"
        if [ -f $phpini ]; then
            echo "extension = $RPM_INSTALL_PREFIX/lib/redis/phpredis.so" |\
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -a -i "$RPM_INSTALL_PREFIX:redis-libs" -p ";" $phpini
        fi
        #   add hook to Apache/PHP configuration
        phpini="$RPM_INSTALL_PREFIX/etc/apache/apache-php.ini"
        if [ -f $phpini ]; then
            echo "extension = $RPM_INSTALL_PREFIX/lib/redis/phpredis.so" |\
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -a -i "$RPM_INSTALL_PREFIX:redis-libs" -p ";" $phpini
        fi
    fi
%endif

%postun
%if "%{with_php}" == "yes"
    if [ ".$1" = .0 ]; then
        #   remove hook from PHP configuration
        phpini="$RPM_INSTALL_PREFIX/etc/php/php.ini"
        if [ -f $phpini ]; then
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -r -i "$RPM_INSTALL_PREFIX:redis-libs" -p ";" $phpini
        fi
        #   remove hook from PHP-FPM configuration
        phpini="$RPM_INSTALL_PREFIX/etc/php-fpm/php-fpm.ini"
        if [ -f $phpini ]; then
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -r -i "$RPM_INSTALL_PREFIX:redis-libs" -p ";" $phpini
        fi
        #   remove hook from Apache/PHP configuration
        phpini="$RPM_INSTALL_PREFIX/etc/apache/apache-php.ini"
        if [ -f $phpini ]; then
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -r -i "$RPM_INSTALL_PREFIX:redis-libs" -p ";" $phpini
        fi
    fi
%endif

