##
##  heartbeat.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.
##

#   FIXME: rse: OSSP uuid still not supported in Heartbeat 2.x

#   package information
Name:         heartbeat
Summary:      High-Availability Communication Daemon
URL:          http://linux-ha.org/heartbeat/
Vendor:       Alan Roberston
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        System
License:      GPL
Version:      2.0.8
Release:      20120713

#   list of sources
Source0:      http://linux-ha.org/download/heartbeat-%{version}.tar.gz
Source1:      ha.cf
Source2:      haresources
Source3:      authkeys
Source4:      rc.heartbeat
Patch0:       heartbeat.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, perl, python, pkgconfig, make, gcc
PreReq:       OpenPKG, openpkg >= 20160101, perl, python
BuildPreReq:  libnet, libxml, uuid, glib, snmp, curl, openssl, gettext, libiconv, getopt, ncurses
PreReq:       libnet, libxml, uuid, glib, snmp, curl, openssl, gettext, libiconv, getopt, ncurses

%description
    The heartbeat program is one of the core components of the Linux
    High-Availability (Linux-HA) project. It provides "warm" failover
    between two nodes in either automatic or controlled way. It uses
    redundant communication links via Ethernet or serial line in
    broadcast, multicast and unicast mode.

%track
    prog heartbeat = {
        version   = %{version}
        url       = http://linux-ha.org/download/index.html
        regex     = heartbeat-(\d+\.[02468]\.\d+)\.tar\.gz
    }

%prep
    #   unpack and patch package
    %setup -q
    %patch -p0
    %{l_shtool} subst -s -q \
        -e 's;/ha\.d/;/;g' \
        -e 's;/ha\.d;;g' \
        `find . -type f -print`

%build
    #   configure package
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    LIBS="-lintl -liconv -lgetopt" \
    ./configure \
        --prefix=%{l_prefix} \
        --sysconfdir=%{l_prefix}/etc/heartbeat \
        --libdir=%{l_prefix}/lib/heartbeat \
        --datadir=%{l_prefix}/share/heartbeat \
        --localstatedir=%{l_prefix}/var/heartbeat \
        --with-initdir=%{l_prefix}/etc/rc.d \
        --with-group-name=%{l_musr} \
        --with-group-id=%{l_muid} \
        --with-ccmuser-name=%{l_musr} \
        --with-ccmuser-id=%{l_muid} \
        --with-uuid=any \
        --enable-static \
        --enable-shared

    #   build package components
    %{l_make} %{l_mflags -O}

%install
    #   install package components
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

    #   strip down installation
    strip  $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/etc/heartbeat/README.config
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/heartbeat
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/include
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/*.sh
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/etc/heartbeat/logrotate.d
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/etc/heartbeat/conf
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib/heartbeat/heartbeat/cts
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/man/man8/*ldirectord*.8
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/sbin/*ldirectord*
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/lib/heartbeat/heartbeat/plugins/*/*.a
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/lib/heartbeat/stonith/plugins/*/*.a
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/lib/heartbeat/pils/plugins/*/*.a

    #   move executables into libexec and wrap them because of shared libraries
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/heartbeat
    for prog in \
        apphbd ha_config hb_standby hb_takeover checkpointd recoverymgrd \
        heartbeat ipfail meatclient stonith cl_status ccm ccm_testclient; do
        if [ -f $RPM_BUILD_ROOT%{l_prefix}/bin/$prog ]; then
            mv $RPM_BUILD_ROOT%{l_prefix}/bin/$prog \
               $RPM_BUILD_ROOT%{l_prefix}/libexec/heartbeat/$prog
        elif [ -f $RPM_BUILD_ROOT%{l_prefix}/sbin/$prog ]; then
            mv $RPM_BUILD_ROOT%{l_prefix}/sbin/$prog \
               $RPM_BUILD_ROOT%{l_prefix}/libexec/heartbeat/$prog
        elif [ -f $RPM_BUILD_ROOT%{l_prefix}/lib/heartbeat/heartbeat/$prog ]; then
            mv $RPM_BUILD_ROOT%{l_prefix}/lib/heartbeat/heartbeat/$prog \
               $RPM_BUILD_ROOT%{l_prefix}/libexec/heartbeat/$prog
        fi
        ( echo "#!/bin/sh"
          echo "LD_LIBRARY_PATH=\"%{l_prefix}/lib/heartbeat:%{l_prefix}/lib:/usr/lib:/lib\""
          echo "export LD_LIBRARY_PATH"
          echo "exec %{l_prefix}/libexec/heartbeat/$prog \"\$@\""
        ) >$RPM_BUILD_ROOT%{l_prefix}/sbin/$prog
        chmod 755 $RPM_BUILD_ROOT%{l_prefix}/sbin/$prog
    done

    #   create run-time directory
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/heartbeat

    #   install default configuration files
    l_hostname=`%{l_shtool} echo -e %h`
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e "s;@l_hostname@;$l_hostname;g" \
        %{SOURCE ha.cf} %{SOURCE haresources} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/heartbeat/
    %{l_shtool} install -c -m 600 %{l_value -s -a} \
        %{SOURCE authkeys} $RPM_BUILD_ROOT%{l_prefix}/etc/heartbeat/

    #   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.heartbeat} \
        $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/heartbeat/ha.cf' \
        '%config %{l_prefix}/etc/heartbeat/haresources' \
        '%config %{l_prefix}/etc/heartbeat/authkeys'

%files -f files

%clean

