##
##  galera.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:         galera
Summary:      Cluster Daemon and API for Write Set Replication (WSREP)
URL:          http://galeracluster.com/
Vendor:       Codership Oy
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        RDBMS
License:      GPL
Version:      26.4.24
Release:      20251006

#   list of sources
Source0:      http://releases.galeracluster.com/galera-4/source/galera-4-%{version}.tar.gz
Source1:      rc.galera
Source2:      galera.conf

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, gcc::with_cxx = yes, scons
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  boost, check
PreReq:       boost, check

%description
    Galera replication is an implementation of the Write Set Replication
    (WSREP) API. It provides clustering to the MySQL/MariaDB RDBMS.

%track
    prog galera = {
        version   = %{version}
        url       = http://galeracluster.com/downloads/
        regex     = version\s+(\d+\.\d+\.\d+)
    }

%prep
    %setup -q -n galera-4-%{version}

%build
    #   build program
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CXX="%{l_cxx}" \
    CXXFLAGS="%{l_cxxflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags} -lboost_program_options -lboost_system" \
    LIBPATH=%{l_prefix}/lib \
    TERM=vt100 \
    PYTHONPATH=%{l_prefix}/lib/scons \
    scons \
        -Q -j 4 \
        debug=0 \
        system_asio=0 \
        tests=0 \
        .

%install
    #   install program
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin
    %{l_shtool} install -c -s -m 755 \
        garb/garbd $RPM_BUILD_ROOT%{l_prefix}/sbin/

    #   install manual pages
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man8
    %{l_shtool} install -c -m 644 \
        man/garbd.8 $RPM_BUILD_ROOT%{l_prefix}/man/man8/

    #   install library
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/lib/galera
    %{l_shtool} install -c -m 644 \
        libgalera_smm.so $RPM_BUILD_ROOT%{l_prefix}/lib/galera/

    #   install garbd(8) default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/galera
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE galera.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/galera/

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

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/galera/*'

%files -f files

%clean

%post
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} galera status 2>/dev/null`
        [ ".$galera_active" = .yes ] && %{l_rc} galera restart
    fi
    exit 0

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

