%bcond_with		systemd
%bcond_with		python3

%define mariadb_version 10.6.11

Summary:        Synchronous multi-master wsrep provider (replication engine)
Name:           galera
Version:        26.4.13
Release:        1%{?_dist_release}%{?with_systemd:.systemd}
%global         galera_api_version %(echo %{version} | cut -d . -f 1,2)
Group:          servers
Vendor:         Project Vine
Distribution:   Vine Linux

License:        GPLv2
URL:            http://galeracluster.com/
# Actually, the truth is, we do use galera source tarball provided by MariaDB on
# following URL (without macros):
#   https://mirror.vpsfree.cz/mariadb/mariadb-10.2.13/galera-25.3.23/src/galera-25.3.23.tar.gz
Source0:        https://archive.mariadb.org/mariadb-%{mariadb_version}/galera-%{version}/src/%{name}-%{version}.tar.gz
Source1:        garbd.service
Source2:        garbd-wrapper
Source3:        garbd.init

Patch0:         cmake_paths.patch

BuildRequires:  libboost-devel libboost-filesystem libboost-program-options
BuildRequires:  check-devel openssl-devel gcc-c++ asio-devel
BuildRequires:  cmake ninja
%if %{with systemd}
BuildRequires:  systemd
%endif
Requires:       nmap
Provides:       galera(%{galera_api_version})

%if %{with systemd}
Requires(post): systemd
Requires(preun): systemd
Requires(postun): systemd
%else
Requires(post): chkconfig
Requires(preun): chkconfig initscripts
%endif

%description
Galera is a fast synchronous multi-master wsrep provider (replication engine)
for transactional databases and similar applications. For more information
about wsrep API see http://launchpad.net/wsrep. For a description of Galera
replication engine see http://www.codership.com.


%debug_package


%prep
%setup -q
%autopatch -p1


%build
%{set_build_flags}

# Workaround for version 26.4.7
export CXXFLAGS="$CXXFLAGS -Wno-error=format-extra-args"
# ... and for 32 bit arches
export CXXFLAGS="$CXXFLAGS -Wno-error=format"
export CFLAGS="$CFLAGS -Wno-error=format-extra-args -Wno-error=format"
# end of workaroound

%cmake \
  -GNinja \
  -DCMAKE_BUILD_TYPE=RelWithDebInfo \
  -DCMAKE_SKIP_RPATH:BOOL=ON \
  -DINSTALL_LAYOUT=RPM \
  -DCMAKE_RULE_MESSAGES:BOOL=OFF \
  -DBUILD_SHARED_LIBS:BOOL=OFF \
  -DINSTALL_DOCDIR="share/doc/%{name}/" \
  -DINSTALL_GARBD="sbin" \
  -DINSTALL_GARBD-SYSTEMD="sbin" \
  -DINSTALL_CONFIGURATION="/etc/sysconfig/" \
  -DINSTALL_SYSTEMD_SERVICE="lib/systemd/system" \
  -DINSTALL_LIBDIR="%{_lib}/galera" \
  -DINSTALL_MANPAGE="share/man/man8"
%cmake_build


%install
%cmake_install

%if %{with systemd}
mkdir -p %{buildroot}%{_unitdir}
mv %{buildroot}/usr/lib/systemd/system/garb.service %{buildroot}%{_unitdir}/garbd.service
sed -i 's/Alias=garbd.service/Alias=garb.service/g' %{buildroot}%{_unitdir}/garbd.service

sed -i 's;/usr/bin/garb-systemd;/usr/sbin/garb-systemd;g' %{buildroot}%{_unitdir}/garbd.service
#     In the wrapper script:
sed -i 's;/usr/bin/garbd;/usr/sbin/garbd;g' %{buildroot}/usr/sbin/garb-systemd

sed -i 's/User=nobody/User=garb/g' %{buildroot}%{_unitdir}/garbd.service
%else

rm -f %{buildroot}%{_sbindir}/garb-systemd
mkdir -p %{buildroot}/var/run/garbd
install -Dpm644 %{SOURCE3} %{buildroot}%{_initdir}/garbd
%endif
# drop invalid directory
rm -rf %{buildroot}/usr/lib/systemd

cp -f %{buildroot}%{_docdir}/galera/LICENSE.asio ./
rm -f %{buildroot}%{_docdir}/galera/LICENSE.asio
rm -f %{buildroot}%{_docdir}/galera/COPYING


%check
%ctest


%pre
/usr/sbin/useradd -M -r -d /dev/null -s /sbin/nologin -c "Galera Arbitrator Daemon" garb >/dev/null 2>&1 || :

%post
%if %{with systemd}
%systemd_post garbd.service
%else
/sbin/chkconfig --add garbd
%endif

%preun
%if %{with systemd}
%systemd_preun garbd.service
%else
if [ $1 = 0 -o -x /bin/systemctl ]; then
/sbin/service garbd stop >/dev/null 2>/dev/null ||:
/sbin/chkconfig --del garbd
fi
%endif

%postun
%if %{with systemd}
%systemd_postun_with_restart garbd.service
%endif


%files
%license COPYING LICENSE.asio
%doc %{_docdir}/galera/*
%config(noreplace,missingok) %{_sysconfdir}/sysconfig/garb
%dir %{_libdir}/galera
%{_libdir}/galera/*
%{_sbindir}/garbd
%{_mandir}/man8/*
%if %{with systemd}
%{_sbindir}/garb-systemd
%{_unitdir}/garbd.service
%else
%attr(755, garb, garb) %dir /var/run/garbd
%{_initdir}/garbd
%endif


%changelog
* Tue Nov 08 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.13-1
- new upstream release.

* Tue Aug 16 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.12-1
- new upstream release.
- dropped scons stuff and Patch0.
- imported Patch0 from rawhide.

* Sun Feb 13 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.11-1
- new upstream release.

* Sat Aug 07 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.9-1
- new upstream release.

* Sat May 08 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.8-1
- new upstream release.
- dropped ldconfig scriptlets.
- switched buildsystem to cmake and ninja.

* Tue Feb 23 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.7-1
- new upstream release.

* Sat Feb 20 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.6-1
- new upstream release.

* Wed Aug 12 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.5-1
- new upstream release.

* Thu May 14 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.4-1
- new upstream release.

* Sat Apr 25 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.3-2
- rebuilt with boost-1.72.0.

* Wed Nov 06 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.3-1
- new upstream release.

* Fri Aug 30 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 26.4.2-1
- updated to 26.4.2 for MariaDB-10.4.

* Sun May 05 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 25.3.25-4
- initial build for Vine Linux.

* Thu Jan 31 2019 Fedora Release Engineering <releng@fedoraproject.org> - 25.3.25-3
- Rebuilt for https://fedoraproject.org/wiki/Fedora_30_Mass_Rebuild

* Tue Jan 29 2019 Jonathan Wakely <jwakely@redhat.com> - 25.3.25-2
- Rebuilt for Boost 1.69

* Tue Jan 01 2019 Michal Schorm <mschorm@redhat.com> - 25.3.25-1
- Rebase to 25.3.25

* Mon Jul 16 2018 Honza Horak <hhorak@redhat.com> - 25.3.23-5
- Require asio also on rhel

* Fri Jul 13 2018 Honza Horak <hhorak@redhat.com> - 25.3.23-4
- Add explicit gcc-c++ BR
- Use python3-scons

* Fri Jul 13 2018 Honza Horak <hhorak@redhat.com> - 25.3.23-3
- Do not require asio on rhel

* Fri Jul 13 2018 Fedora Release Engineering <releng@fedoraproject.org> - 25.3.23-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_29_Mass_Rebuild

* Fri Feb 16 2018 Michal Schorm <mschorm@redhat.com> - 25.3.23-1
- Update to 25.3.23

* Wed Feb 07 2018 Fedora Release Engineering <releng@fedoraproject.org> - 25.3.22-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_28_Mass_Rebuild

* Fri Nov 24 2017 Honza Horak <hhorak@redhat.com> - 25.3.22-1
- Update to 25.3.22

* Wed Aug 02 2017 Fedora Release Engineering <releng@fedoraproject.org> - 25.3.16-6
- Rebuilt for https://fedoraproject.org/wiki/Fedora_27_Binutils_Mass_Rebuild

* Wed Jul 26 2017 Fedora Release Engineering <releng@fedoraproject.org> - 25.3.16-5
- Rebuilt for https://fedoraproject.org/wiki/Fedora_27_Mass_Rebuild

* Mon Jul 03 2017 Jonathan Wakely <jwakely@redhat.com> - 25.3.16-4
- Rebuilt for Boost 1.64

* Mon May 15 2017 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 25.3.16-3
- Rebuilt for https://fedoraproject.org/wiki/Fedora_26_27_Mass_Rebuild

* Sat Feb 18 2017 Jonathan Wakely <jwakely@redhat.com> - 25.3.16-2
- Use asio-devel instead of bundled asio library

* Fri Feb 10 2017 Fedora Release Engineering <releng@fedoraproject.org> - 25.3.16-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_26_Mass_Rebuild

* Wed Jun 22 2016 Mike Bayer <mbayer@redhat.com> - 25.3.16-1
- Update to 25.3.16

* Wed Feb 03 2016 Fedora Release Engineering <releng@fedoraproject.org> - 25.3.12-4
- Rebuilt for https://fedoraproject.org/wiki/Fedora_24_Mass_Rebuild

* Fri Jan 15 2016 Jonathan Wakely <jwakely@redhat.com> - 25.3.12-3
- Rebuilt for Boost 1.60

* Wed Sep 30 2015 Marcin Juszkiewicz <mjuszkiewicz@redhat.com> - 25.3.12-2
- Remove use of -mtune=native which breaks build on secondary architectures

* Fri Sep 25 2015 Richard W.M. Jones <rjones@redhat.com> - 25.3.12-1
- Update to 25.3.12.
- Should fix the build on 32 bit ARM (RHBZ#1241164).
- Remove ExcludeArch (should have read the BZ more closely).

* Thu Aug 27 2015 Jonathan Wakely <jwakely@redhat.com> - 25.3.10-5
- Rebuilt for Boost 1.59

* Wed Jul 29 2015 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 25.3.10-4
- Rebuilt for https://fedoraproject.org/wiki/Changes/F23Boost159

* Wed Jul 22 2015 David Tardon <dtardon@redhat.com> - 25.3.10-3
- rebuild for Boost 1.58

* Wed Jul 08 2015 Ryan O'Hara <rohara@redhat.com> - 25.3.10-2
- Disable ARM builds (#1241164, #1239516)

* Mon Jul 06 2015 Ryan O'Hara <rohara@redhat.com> - 25.3.10-1
- Update to version 25.3.10

* Wed Jun 17 2015 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 25.3.5-11
- Rebuilt for https://fedoraproject.org/wiki/Fedora_23_Mass_Rebuild

* Mon Jan 26 2015 Petr Machata <pmachata@redhat.com> - 25.3.5-10
- Rebuild for boost 1.57.0

* Thu Nov 27 2014 Richard W.M. Jones <rjones@redhat.com> - 25.3.5-9
- Add aarch64 support.

* Sat Aug 16 2014 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 25.3.5-8
- Rebuilt for https://fedoraproject.org/wiki/Fedora_21_22_Mass_Rebuild

* Sat Jun 07 2014 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 25.3.5-7
- Rebuilt for https://fedoraproject.org/wiki/Fedora_21_Mass_Rebuild

* Fri May 23 2014 Petr Machata <pmachata@redhat.com> - 25.3.5-6
- Rebuild for boost 1.55.0

* Wed Apr 30 2014 Dan HorĂ¡k <dan[at]danny.cz> - 25.3.5-5
- set ExclusiveArch

* Thu Apr 24 2014 Ryan O'Hara <rohara@redhat.com> - 25.3.5-4
- Use strict_build_flags=0 to avoid -Werror
- Remove unnecessary clean section

* Thu Apr 24 2014 Ryan O'Hara <rohara@redhat.com> - 25.3.5-3
- Include galera directories in file list
- Set CPPFLAGS to optflags

* Wed Apr 23 2014 Ryan O'Hara <rohara@redhat.com> - 25.3.5-2
- Fix client certificate verification (#1090604)

* Thu Mar 27 2014 Ryan O'Hara <rohara@redhat.com> - 25.3.5-1
- Update to version 25.3.5

* Mon Mar 24 2014 Ryan O'Hara <rohara@redhat.com> - 25.3.3-2
- Add systemd service

* Sun Mar 09 2014 Ryan O'Hara <rohara@redhat.com> - 25.3.3-1
- Initial build