##
##  postgresql-repmgr.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 version
%define       V_opkg   5.3.3
%define       V_subdir 5.3.3

#   package information
Name:         postgresql-repmgr
Summary:      PostgreSQL Replication Manager
URL:          http://repmgr.org/
Vendor:       2ndQuadrant
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      GPL
Version:      %{V_opkg}
Release:      20221023

#   list of sources
Source0:      http://repmgr.org/download/repmgr-%{V_opkg}.tar.gz
Source1:      rc.postgresql-repmgr
Source2:      postgresql-repmgr-setup.sh
Source3:      postgresql-repmgr-pgsql.conf
Source4:      postgresql-repmgr-pghba.conf
Patch0:       postgresql-repmgr.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  postgresql, readline, ncurses
PreReq:       postgresql, readline, ncurses

%description
    repmgr helps DBAs and System Administrators manage a cluster of
    PostgreSQL databases. By taking advantage of the Hot Standby
    capability introduced in PostgreSQL 9, repmgr greatly simplifies the
    process of setting up and managing databases with high availability
    and scalability requirements.

%track
    prog postgresql-repmgr = {
        version   = %{V_opkg}
        url       = http://repmgr.org/
        regex     = repmgr-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n repmgr-%{V_subdir}
    %patch -p0

%build
    #   build program
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}"
    ./configure \
        --prefix=%{l_prefix}
    %{l_make} %{l_mflags -O} USE_PGXS=1

%install
    #   install program
    %{l_make} %{l_mflags} install USE_PGXS=1 DESTDIR=$RPM_BUILD_ROOT

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   move binaries and provide wrapper
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/postgresql-repmgr
    mv  $RPM_BUILD_ROOT%{l_prefix}/bin/* \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/postgresql-repmgr/
    (   echo "#!/bin/sh"
        echo "exec %{l_prefix}/libexec/postgresql-repmgr/repmgr \\%{l_nil}"
        echo "    -f %{l_prefix}/etc/postgresql-repmgr/postgresql-repmgr.conf \\%{l_nil}"
        echo "    \${1+\"\$@\"}"
    ) >$RPM_BUILD_ROOT%{l_prefix}/bin/repmgr
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/bin/repmgr
    (   echo "#!/bin/sh"
        echo "exec %{l_prefix}/libexec/postgresql-repmgr/repmgrd \\%{l_nil}"
        echo "    -f %{l_prefix}/etc/postgresql-repmgr/postgresql-repmgr.conf \\%{l_nil}"
        echo "    \${1+\"\$@\"}"
    ) >$RPM_BUILD_ROOT%{l_prefix}/bin/repmgrd
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/bin/repmgrd

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/postgresql-repmgr
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        repmgr.conf.sample \
        $RPM_BUILD_ROOT%{l_prefix}/etc/postgresql-repmgr/postgresql-repmgr.conf
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE postgresql-repmgr-pgsql.conf} \
        %{SOURCE postgresql-repmgr-pghba.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/postgresql-repmgr/

    #   install setup script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE postgresql-repmgr-setup.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/postgresql-repmgr-setup

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

    #   install run-time directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/postgresql-repmgr/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/postgresql-repmgr/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/postgresql-repmgr/db

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/postgresql-repmgr/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %dir %{l_prefix}/var/postgresql-repmgr/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   add hook to PostgreSQL configuration
        pgsql="$RPM_INSTALL_PREFIX/var/postgresql/db/postgresql.conf"
        if [ -f $pgsql ]; then
            ( echo "include = '$RPM_INSTALL_PREFIX/etc/postgresql-repmgr-pgsql.conf'" ) | \
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -a -i "$RPM_INSTALL_PREFIX:postgresql-repmgr" -p "#" $pgsql
        fi
        pghba="$RPM_INSTALL_PREFIX/var/postgresql/db/pg_hba.conf"
        if [ -f $pghba ]; then
            cat $RPM_INSTALL_PREFIX/etc/postgresql-repmgr-pghba.conf | \
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -a -i "$RPM_INSTALL_PREFIX:postgresql-repmgr" -p "#" $pghba
        fi

        #   display final hints on initial installation
        ( echo "1. To complete this installation ON THE MASTER please start"
          echo "   PostgreSQL and initialize the RepMgr database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/postgresql-repmgr-setup install"
          echo "     \$ vi $RPM_INSTALL_PREFIX/etc/postgresql-repmgr/postgresql-repmgr.conf"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/repmgr primary register"
          echo ""
          echo "2. To complete this installation ON THE SLAVE(S) please stop"
          echo "   PostgreSQL and initialize the RepMgr database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql stop"
          echo "     \$ rm -rf $RPM_INSTALL_PREFIX/var/postgresql/db"
          echo "     \$ vi $RPM_INSTALL_PREFIX/etc/postgresql-repmgr/postgresql-repmgr.conf"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/repmgr -h node1 -U repmgr -d repmgr standby clone"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/repmgr standby register"
          echo ""
          echo "3. Check the cluster status with:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/repmgr cluster show"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} postgresql-repmgr status 2>/dev/null`
        [ ".$postgresql_repmgr_active" = .yes ] && %{l_rc} postgresql-repmgr restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        %{l_rc} postgresql-repmgr stop 2>/dev/null

        #   remove hook from PostgreSQL configuration
        pgsql="$RPM_INSTALL_PREFIX/var/postgresql/db/postgresql.conf"
        if [ -f $pgsql ]; then
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -r -i "$RPM_INSTALL_PREFIX:postgresql-repmgr" -p "#" $pgsql
        fi
        pghba="$RPM_INSTALL_PREFIX/var/postgresql/db/pg_hba.conf"
        if [ -f $pghba ]; then
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -r -i "$RPM_INSTALL_PREFIX:postgresql-repmgr" -p "#" $pghba
        fi

        #   remove database
        $RPM_INSTALL_PREFIX/sbin/postgresql-repmgr-setup uninstall >/dev/null 2>&1 || true

        #   remove run-time files
        rm -f  $RPM_INSTALL_PREFIX/var/postgresql-repmgr/log/* >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/postgresql-repmgr/run/* >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/postgresql-repmgr/db/*  >/dev/null 2>&1 || true
    fi
    exit 0

