%bcond_with systemd

# Check for status of man pages
# http://code.google.com/p/redis/issues/detail?id=202

# Commit IDs for the (unversioned) redis-doc repository
# https://fedoraproject.org/wiki/Packaging:SourceURL "Commit Revision"
#global doc_commit 4f1da37c03daa943f2cf3cd5fda8dbd1bf1f22d7
%global doc_commit 0ee00bbc97dda36d09a2559f20dafd236d5426a2
%global short_doc_commit %(c=%{doc_commit}; echo ${c:0:7})

# %%{rpmmacrodir} not usable on EL-6
%global macrosdir %(d=%{_rpmconfigdir}/macros.d; [ -d $d ] || d=%{_sysconfdir}/rpm; echo $d)

%ifarch %{ix86} x86_64 ppc
# available only on selected architectures
%global with_perftools 1
%endif

Name:             redis
Version:          6.0.9
Release:          1%{?_dist_release}%{?with_systemd:.systemd}
Summary:          A persistent in-memory key-value database
Summary(ja):      永続化可能なキー・バリュー型インメモリデータベース
Group:            servers
Vendor:           Project Vine
Distribution:     Vine Linux
Packager:         daisuke

License:          BSD
URL:              https://redis.io
Source0:          https://github.com/redis-io/redis/archive/%{version}.tar.gz#/%{name}-%{version}.tar.gz
Source1:          %{name}.logrotate
Source2:          %{name}-sentinel.service
Source3:          %{name}.service
Source4:          %{name}-sentinel.init
Source5:          %{name}.init
Source6:          %{name}-shutdown
Source7:          %{name}-limit-systemd
Source8:          %{name}-limit-init
Source9:          macros.%{name}
Source10:         https://github.com/redis/%{name}-doc/archive/%{doc_commit}/%{name}-doc-%{short_doc_commit}.tar.gz

# To refresh patches:
# tar xf redis-xxx.tar.gz && cd redis-xxx && git init && git add . && git commit -m "%%{version} baseline"
# git am %%{patches}
# Then refresh your patches
# git format-patch HEAD~<number of expected patches>
# Update configuration for Fedora
# https://github.com/antirez/redis/pull/3491 - man pages
Patch0001:         0001-1st-man-pageis-for-redis-cli-redis-benchmark-redis-c.patch
# https://github.com/antirez/redis/pull/3494 - symlink
Patch0002:         0002-install-redis-check-rdb-as-a-symlink-instead-of-dupl.patch

BuildRoot:	  %{_tmppath}/%{name}-%{version}-root
%if %{?with_perftools}
BuildRequires:    gperftools-devel
%endif

Requires:         logrotate
%if %{with systemd}
BuildRequires:     systemd
Requires(post):    systemd
Requires(preun):   systemd
Requires(postun):  systemd
%else
Requires(post):   chkconfig
Requires(postun): initscripts
Requires(preun):  chkconfig
Requires(preun):  initscripts
%endif
Requires(pre):    shadow-utils


%global redis_modules_abi 1
%global redis_modules_dir %{_libdir}/%{name}/modules
Provides:          redis(modules_abi)%{?_isa} = %{redis_modules_abi}

%description
Redis is an advanced key-value store. It is similar to memcached but the data
set 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. Redis supports
different kind of sorting abilities.


%package           devel
Summary:           Development header for Redis module development
Group:             programming
# Header-Only Library (https://fedoraproject.org/wiki/Packaging:Guidelines)
Provides:          %{name}-static = %{version}-%{release}

%description       devel
Header file required for building loadable Redis modules. Detailed
API documentation is available in the redis-doc package.


%package           doc
Summary:           Documentation for Redis including man pages
Group:             documentation
License:           CC-BY-SA
BuildArch:         noarch

# http://fedoraproject.org/wiki/Packaging:Conflicts "Splitting Packages"
Conflicts:         redis < 4.0

%description       doc
Manual pages and detailed documentation for many aspects of Redis use,
administration and development.


%debug_package


%prep
%setup -q -b 10
%setup -q
mv ../%{name}-doc-%{doc_commit} doc
%patch0001 -p1
%patch0002 -p1

mv deps/lua/COPYRIGHT    COPYRIGHT-lua
mv deps/hiredis/COPYING  COPYING-hiredis

# Configuration file changes
sed -i -e 's|^logfile .*$|logfile /var/log/redis/redis.log|g' redis.conf
sed -i -e 's|^logfile .*$|logfile /var/log/redis/sentinel.log|g' sentinel.conf
sed -i -e 's|^dir .*$|dir /var/lib/redis|g' redis.conf

sed -i -e 's|^daemonize no$|daemonize yes|g' redis.conf
sed -i -e 's|^protected-mode no$|protected-mode yes|g' redis.conf
sed -i -e 's|^# bind 127\.0\.0\.1$|bind 127.0.0.1|g' redis.conf

# Module API version safety check
api=`sed -n -e 's/#define REDISMODULE_APIVER_[0-9][0-9]* //p' src/redismodule.h`
if test "$api" != "%{redis_modules_abi}"; then
   : Error: Upstream API version is now ${api}, expecting %%{redis_modules_abi}.
   : Update the redis_modules_abi macro, the rpmmacros file, and rebuild.
   exit 1
fi

%global make_flags	DEBUG="" V="echo" LDFLAGS="%{?__global_ldflags}" CFLAGS+="%{optflags} -fPIC" INSTALL="install -p" PREFIX=%{buildroot}%{_prefix}


%build
make %{?_smp_mflags} %{make_flags} all

%check
# make test

%install
make %{make_flags} install

# Filesystem.
install -d %{buildroot}%{_sharedstatedir}/%{name}
install -d %{buildroot}%{_localstatedir}/log/%{name}
install -d %{buildroot}%{_localstatedir}/run/%{name}
install -d %{buildroot}%{redis_modules_dir}

# Install logrotate file.
install -pDm644 %{S:1} %{buildroot}%{_sysconfdir}/logrotate.d/%{name}

# Install configuration files.
mkdir -p %{buildroot}%{_sysconfdir}/%{name}
install -pDm640 %{name}.conf %{buildroot}%{_sysconfdir}/%{name}/%{name}.conf
install -pDm640 sentinel.conf %{buildroot}%{_sysconfdir}/%{name}/%{name}-sentinel.conf

%if %{with systemd}
# Install systemd unit files.
mkdir -p %{buildroot}%{_unitdir}
install -pm644 %{S:3} %{buildroot}%{_unitdir}
install -pm644 %{S:2} %{buildroot}%{_unitdir}

# Install systemd limit files (requires systemd >= 204)
install -p -D -m 644 %{S:7} %{buildroot}%{_sysconfdir}/systemd/system/%{name}.service.d/limit.conf
install -p -D -m 644 %{S:7} %{buildroot}%{_sysconfdir}/systemd/system/%{name}-sentinel.service.d/limit.conf
%else
mkdir -p %{buildroot}%{_initdir}
install -pm755 %{S:4} %{buildroot}%{_initdir}/%{name}-sentinel
install -pm755 %{S:5} %{buildroot}%{_initdir}/%{name}
install -pm755 %{S:8} %{buildroot}%{_initdir}/%{name}-limit
%endif


# Fix non-standard-executable-perm error.
chmod 755 %{buildroot}%{_bindir}/%{name}-*

# Install redis-shutdown
install -pDm755 %{S:6} %{buildroot}%{_libexecdir}/%{name}-shutdown

# Install redis module header
install -pDm644 src/%{name}module.h %{buildroot}%{_includedir}/%{name}module.h

# Install man pages
man=$(dirname %{buildroot}%{_mandir})
for page in man/man?/*; do
    install -Dpm644 $page $man/$page
done
ln -s redis-server.1 %{buildroot}%{_mandir}/man1/redis-sentinel.1
ln -s redis.conf.5   %{buildroot}%{_mandir}/man5/redis-sentinel.conf.5

# Install documentation and html pages
doc=$(echo %{buildroot}/%{_docdir}/%{name})
for page in 00-RELEASENOTES BUGS CONTRIBUTING MANIFESTO; do
    install -Dpm644 $page $doc/$page
done
for page in $(find doc -name \*.md | sed -e 's|.md$||g'); do
    base=$(echo $page | sed -e 's|doc/||g')
    install -Dpm644 $page.md $doc/$base.md
done

# Install rpm macros for redis modules
mkdir -p %{buildroot}%{macrosdir}
install -pDm644 %{S:9} %{buildroot}%{macrosdir}/macros.%{name}


%post
%if %{with systemd}
%systemd_post %{name}.service
%systemd_post %{name}-sentinel.service
%else
if [ $1 -eq 1 ]; then
	/sbin/chkconfig --add %{name}
	/sbin/chkconfig --add %{name}-sentinel
fi
%endif

%pre
getent group %{name} &> /dev/null || \
groupadd -r %{name} &> /dev/null
getent passwd %{name} &> /dev/null || \
useradd -r -g %{name} -d %{_sharedstatedir}/%{name} -s /sbin/nologin \
-c 'Redis Database Server' %{name} &> /dev/null
exit 0

%preun
%if %{with systemd}
%systemd_preun %{name}.service
%systemd_preun %{name}-sentinel.service
%else
if [ $1 -eq 0 -o -x /sbin/systemctl ]; then
	/sbin/service %{name} stop ||:
	/sbin/service %{name}-sentinel stop ||:
	/sbin/chkconfig --del %{name}
	/sbin/chkconfig --del %{name}-sentinel
fi
%endif

%postun
%if %{with systemd}
%systemd_postun_with_restart %{name}.service
%systemd_postun_with_restart %{name}-sentinel.service
%else
if [ $1 -gt 0 ]; then
	if [ -x /sbin/systemctl ]; then
		/sbin/service %{name} stop ||:
	else
		/sbin/service %{name} condrestart ||:
	fi
fi
%endif


%files
%{!?_licensedir:%global license %%doc}
%license COPYING
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%dir %{_sysconfdir}/%{name}
%attr(0640, redis, root) %config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf
%attr(0640, redis, root) %config(noreplace) %{_sysconfdir}/%{name}/%{name}-sentinel.conf
%dir %attr(0750, redis, redis) %{_libdir}/%{name}
%dir %attr(0750, redis, redis) %{redis_modules_dir}
%dir %attr(0750, redis, redis) %{_sharedstatedir}/%{name}
%dir %attr(0750, redis, redis) %{_localstatedir}/log/%{name}
%exclude %{macrosdir}
%exclude %{_includedir}
%exclude %{_docdir}/%{name}/*
%{_bindir}/%{name}-*
%{_libexecdir}/%{name}-*
%{_mandir}/man1/%{name}*
%{_mandir}/man5/%{name}*
%if %{with systemd}
%{_unitdir}/%{name}.service
%{_unitdir}/%{name}-sentinel.service
%dir %{_sysconfdir}/systemd/system/%{name}.service.d
%config(noreplace) %{_sysconfdir}/systemd/system/%{name}.service.d/limit.conf
%dir %{_sysconfdir}/systemd/system/%{name}-sentinel.service.d
%config(noreplace) %{_sysconfdir}/systemd/system/%{name}-sentinel.service.d/limit.conf
%else
%{_initdir}/*
%endif
%dir %attr(0755, redis, redis) %{_localstatedir}/run/%{name}

%files devel
%license COPYING
%license COPYRIGHT-lua
%license COPYING-hiredis
%{_includedir}/%{name}module.h
%{macrosdir}/*

%files doc
%docdir %{_docdir}/%{name}
%{_docdir}/%{name}


%changelog
* Sat Dec 19 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 6.0.9-1
- new upstream release.

* Thu Jul 02 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 6.0.5-1
- new upstream release.

* Fri Apr 10 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.0.8-1
- new upstream release.

* Sun Sep 08 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.0.5-1
- new upstream release.

* Mon Mar 11 2013 Daisuke SUZUKI <daisuke@linux.or.jp> 2.6.10-1
- update to 2.6.10

* Sat Oct 27 2012 Daisuke SUZUKI <daisuke@linux.or.jp> 2.4.15-1
- initial build for Vine Linux

* Sat Jul 21 2012 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 2.4.15-3
- Rebuilt for https://fedoraproject.org/wiki/Fedora_18_Mass_Rebuild

* Sun Jul 08 2012 Silas Sewell <silas@sewell.org> - 2.4.15-2
- Remove TODO from docs

* Sun Jul 08 2012 Silas Sewell <silas@sewell.org> - 2.4.15-1
- Update to redis 2.4.15

* Sat May 19 2012 Silas Sewell <silas@sewell.org> - 2.4.13-1
- Update to redis 2.4.13

* Sat Mar 31 2012 Silas Sewell <silas@sewell.org> - 2.4.10-1
- Update to redis 2.4.10

* Fri Feb 24 2012 Silas Sewell <silas@sewell.org> - 2.4.8-1
- Update to redis 2.4.8

* Sat Feb 04 2012 Silas Sewell <silas@sewell.org> - 2.4.7-1
- Update to redis 2.4.7

* Wed Feb 01 2012 Fabian Deutsch <fabiand@fedoraproject.org> - 2.4.6-4
- Fixed a typo in the spec

* Tue Jan 31 2012 Fabian Deutsch <fabiand@fedoraproject.org> - 2.4.6-3
- Fix .service file, to match config (Type=simple).

* Tue Jan 31 2012 Fabian Deutsch <fabiand@fedoraproject.org> - 2.4.6-2
- Fix .service file, credits go to Timon.

* Thu Jan 12 2012 Fabian Deutsch <fabiand@fedoraproject.org> - 2.4.6-1
- Update to 2.4.6
- systemd unit file added
- Compiler flags changed to compile 2.4.6
- Remove doc/ and Changelog

* Sun Jul 24 2011 Silas Sewell <silas@sewell.org> - 2.2.12-1
- Update to redis 2.2.12

* Fri May 06 2011 Dan Horák <dan[at]danny.cz> - 2.2.5-2
- google-perftools exists only on selected architectures

* Sat Apr 23 2011 Silas Sewell <silas@sewell.ch> - 2.2.5-1
- Update to redis 2.2.5

* Sat Mar 26 2011 Silas Sewell <silas@sewell.ch> - 2.2.2-1
- Update to redis 2.2.2

* Wed Feb 09 2011 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 2.0.4-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_15_Mass_Rebuild

* Sun Dec 19 2010 Silas Sewell <silas@sewell.ch> - 2.0.4-1
- Update to redis 2.0.4

* Tue Oct 19 2010 Silas Sewell <silas@sewell.ch> - 2.0.3-1
- Update to redis 2.0.3

* Fri Oct 08 2010 Silas Sewell <silas@sewell.ch> - 2.0.2-1
- Update to redis 2.0.2
- Disable checks section for el5

* Sat Sep 11 2010 Silas Sewell <silas@sewell.ch> - 2.0.1-1
- Update to redis 2.0.1

* Sat Sep 04 2010 Silas Sewell <silas@sewell.ch> - 2.0.0-1
- Update to redis 2.0.0

* Thu Sep 02 2010 Silas Sewell <silas@sewell.ch> - 1.2.6-3
- Add Fedora build flags
- Send all scriplet output to /dev/null
- Remove debugging flags
- Add redis.conf check to init script

* Mon Aug 16 2010 Silas Sewell <silas@sewell.ch> - 1.2.6-2
- Don't compress man pages
- Use patch to fix redis.conf

* Tue Jul 06 2010 Silas Sewell <silas@sewell.ch> - 1.2.6-1
- Initial package