%global erlang_minver 20
%global _rabbit_libdir %{_exec_prefix}/lib/rabbitmq
%global debug_package %{nil}
Name:                rabbitmq-server
Version:             3.7.23
Release:             1
License:             MPLv1.1
Source0:             https://github.com/rabbitmq/rabbitmq-server/releases/download/v%{version}/rabbitmq-server_%{version}.orig.tar.xz
Source1:             https://github.com/rabbitmq/rabbitmq-server/releases/download/v%{version}/rabbitmq-server_%{version}.orig.tar.xz.asc
# curl -O https://raw.githubusercontent.com/lemenkov/rabbitmq-server/cdfc661/packaging/RPMS/Fedora/rabbitmq-server.logrotate
Source3:             rabbitmq-server.logrotate
# curl -O https://raw.githubusercontent.com/rabbitmq/rabbitmq-server-release/rabbitmq_v3_6_16/packaging/RPMS/Fedora/rabbitmq-server.tmpfiles
Source5:             rabbitmq-server.tmpfiles
Patch1:              rabbitmq-server-0001-Remove-excessive-sd_notify-code.patch
Patch2:              rabbitmq-server-0002-Add-systemd-notification-support.patch
Patch3:              rabbitmq-server-0003-Allow-guest-login-from-non-loopback-connections.patch
Patch4:              rabbitmq-server-0004-rabbit_prelaunch-must-use-RABBITMQ_SERVER_ERL_ARGS.patch
Patch101:            rabbitmq-common-0001-Use-proto_dist-from-command-line.patch
Patch102:            rabbitmq-common-0002-force-python3.patch
Patch201:            rabbitmq-server-release-0001-Don-t-use-templates.patch
Patch202:            rabbitmq-server-release-0002-Revert-Use-template-in-rabbitmq-script-wrapper-for-R.patch
Patch301:            rabbitmq-amqp1.0-common-0001-force-python3.patch
URL:                 https://www.rabbitmq.com/
BuildRequires:       elixir erlang >= %{erlang_minver} libxslt python3 python3-simplejson rsync
BuildRequires:       systemd xmlto zip
Requires:            logrotate erlang-erts%{?_isa} >= %{erlang_minver}
Requires:            erlang-kernel%{?_isa} >= %{erlang_minver}
Requires:            erlang-eldap%{?_isa} >= %{erlang_minver}
Requires:            erlang-mnesia%{?_isa} >= %{erlang_minver}
Requires:            erlang-os_mon%{?_isa} >= %{erlang_minver}
Requires:            erlang-public_key%{?_isa} >= %{erlang_minver}
Requires:            erlang-sasl%{?_isa} >= %{erlang_minver} erlang-sd_notify%{?_isa}
Requires:            erlang-ssl%{?_isa} >= %{erlang_minver}
Requires:            erlang-stdlib%{?_isa} >= %{erlang_minver}
Requires:            erlang-tools%{?_isa} >= %{erlang_minver}
Requires:            erlang-xmerl%{?_isa} >= %{erlang_minver}
Summary:             The RabbitMQ server
Requires(pre): shadow-utils
Requires(pre): systemd
Requires(post): systemd
Requires(preun): systemd
%description
RabbitMQ is an implementation of AMQP, the emerging standard for high
performance enterprise messaging. The RabbitMQ server is a robust and
scalable implementation of an AMQP broker.

%prep
%setup -q
cd deps/rabbit
%patch1 -p1
%patch2 -p1
%patch3 -p1
%patch4 -p1
cd ../..
cd deps/rabbit_common
%patch101 -p1
%patch102 -p1
cd ../..
%patch201 -p1
%patch202 -p1
cd deps/amqp10_common
%patch301 -p1
cd ../..
rm -f \
	deps/amqp_client/src/rabbit_ct_client_helpers.erl \
	deps/rabbit_common/src/rabbit_ct_broker_helpers.erl \
	deps/rabbit_common/src/rabbit_ct_helpers.erl

%build
make %{?_smp_mflags} VERSION="%{version}" V=1

%install
make install \
	VERSION="%{version}" \
	DESTDIR=%{buildroot} \
	PREFIX=%{_prefix} \
	RMQ_ROOTDIR=%{_rabbit_libdir}
make install-man \
	VERSION="%{version}" \
	DESTDIR=%{buildroot} \
	PREFIX=%{_prefix} \
	RMQ_ROOTDIR=%{_rabbit_libdir}
mkdir -p %{buildroot}%{_localstatedir}/lib/rabbitmq/mnesia
mkdir -p %{buildroot}%{_localstatedir}/log/rabbitmq
install -p -D -m 0644 ./deps/rabbit/docs/rabbitmq-server.service.example %{buildroot}%{_unitdir}/%{name}.service
install -p -D -m 0755 ./scripts/rabbitmq-script-wrapper %{buildroot}%{_sbindir}/rabbitmqctl
install -p -D -m 0755 ./scripts/rabbitmq-script-wrapper %{buildroot}%{_sbindir}/rabbitmq-server
install -p -D -m 0755 ./scripts/rabbitmq-script-wrapper %{buildroot}%{_sbindir}/rabbitmq-plugins
mkdir -p %{buildroot}%{_rabbit_libdir}/bin
for app in rabbitmq-defaults rabbitmq-env rabbitmq-plugins rabbitmq-server rabbitmqctl ; do
	ln -s %{_rabbit_libdir}/lib/rabbitmq_server-%{version}/sbin/${app} %{buildroot}%{_rabbit_libdir}/bin/${app}
done
install -p -D -m 0755 scripts/rabbitmq-server.ocf %{buildroot}%{_exec_prefix}/lib/ocf/resource.d/rabbitmq/rabbitmq-server
install -p -D -m 0755 scripts/rabbitmq-server-ha.ocf %{buildroot}%{_exec_prefix}/lib/ocf/resource.d/rabbitmq/rabbitmq-server-ha
install -p -D -m 0644 %{S:3} %{buildroot}%{_sysconfdir}/logrotate.d/rabbitmq-server
install -p -D -m 0644 ./deps/rabbit/docs/rabbitmq.config.example %{buildroot}%{_sysconfdir}/rabbitmq/rabbitmq.config
install -d %{buildroot}%{_localstatedir}/run/rabbitmq
install -p -D -m 0644 %{SOURCE5} %{buildroot}%{_prefix}/lib/tmpfiles.d/%{name}.conf

%check

%pre
if ! getent group rabbitmq >/dev/null; then
        groupadd -r rabbitmq
fi
if ! getent passwd rabbitmq >/dev/null; then
        useradd -r -g rabbitmq -d %{_localstatedir}/lib/rabbitmq rabbitmq \
            -s /sbin/nologin -c "RabbitMQ messaging server"
fi

%post
%systemd_post %{name}.service
/bin/systemctl daemon-reload

%preun
%systemd_preun %{name}.service
rm -rf %{_localstatedir}/lib/rabbitmq/plugins
for ext in rel script boot ; do
    rm -f %{_rabbit_libdir}/lib/rabbitmq_server-%{version}/ebin/rabbit.$ext
done

%postun
%systemd_postun_with_restart %{name}.service

%files
%dir %attr(0755, rabbitmq, rabbitmq) %{_sysconfdir}/rabbitmq
%config(noreplace) %attr(0644, rabbitmq, rabbitmq) %{_sysconfdir}/rabbitmq/rabbitmq.config
%config(noreplace) %{_sysconfdir}/logrotate.d/rabbitmq-server
%{_sbindir}/rabbitmqctl
%{_sbindir}/rabbitmq-server
%{_sbindir}/rabbitmq-plugins
%{_rabbit_libdir}/
%{_unitdir}/%{name}.service
%dir /usr/lib/ocf/resource.d/rabbitmq/
/usr/lib/ocf/resource.d/rabbitmq/rabbitmq-server
/usr/lib/ocf/resource.d/rabbitmq/rabbitmq-server-ha
%{_tmpfilesdir}/%{name}.conf
%dir %attr(0750, rabbitmq, rabbitmq) %{_localstatedir}/lib/rabbitmq
%dir %attr(0750, rabbitmq, rabbitmq) %{_localstatedir}/log/rabbitmq
%dir %attr(0755, rabbitmq, rabbitmq) %{_localstatedir}/run/rabbitmq
%license LICENSE LICENSE-*
%{_mandir}/man5/rabbitmq-env.conf.5*
%{_mandir}/man8/rabbitmq-diagnostics.8*
%{_mandir}/man8/rabbitmq-echopid.8*
%{_mandir}/man8/rabbitmq-plugins.8*
%{_mandir}/man8/rabbitmq-server.8*
%{_mandir}/man8/rabbitmq-service.8*
%{_mandir}/man8/rabbitmqctl.8*

%changelog
* Thu Sep 3 2020 shaoqiang kang <kangshaoqiang1@huawei.com> - 3.7.23-1
- Package init
