##
##  pgpool.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 versions
%define       V_openpkg   4.6.3
%define       V_pgpool    4.6.3

#   package information
Name:         pgpool
Summary:      PostgreSQL Connection Pooling
URL:          http://www.pgpool.net/
Vendor:       Tatsuo Ishii
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Database
License:      GPL
Version:      %{V_openpkg}
Release:      20250823

#   list of sources
Source0:      http://www.pgpool.net/mediawiki/images/pgpool-II-%{V_pgpool}.tar.gz
Source1:      rc.pgpool
Patch0:       pgpool.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  postgresql, openssl, getopt
PreReq:       postgresql, openssl, getopt

%description
    PGPool is a connection pool server for PostgreSQL. PGPool runs
    between PostgreSQL's clients (front ends) and servers (back ends).
    A PostgreSQL client can connect to PGPool as if it were a standard
    PostgreSQL server. PGPool caches the connection to the PostgreSQL
    server to reduce the overhead to establish the connection to it.
    Also, PGPool could use two PostgreSQL servers for fail over. If the
    first server goes down, PGPool will automatically switch to the
    secondary server. Moreover, PGPool supports scheduled switch over.

%track
    prog pgpool = {
        version   = %{V_pgpool}
        url       = http://www.pgpool.net/mediawiki/index.php/Downloads
        regex     = pgpool-II-(\d+\.\d+(?:\.\d+)?)\.tar\.gz
    }

%prep
    %setup -q -n pgpool-II-%{V_pgpool}
    %patch -p0

%build
    #   configure program
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O} %{l_cppflags}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    LIBS="-lgetopt -lssl -lcrypto -lcrypt" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --sysconfdir=%{l_prefix}/etc/pgpool \
        --disable-shared

    #   build program
    %{l_make} %{l_mflags -O}

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

    #   install default configuration
    mv  $RPM_BUILD_ROOT%{l_prefix}/etc/pgpool/pcp.conf.sample \
        $RPM_BUILD_ROOT%{l_prefix}/etc/pgpool/pcp.conf
    mv  $RPM_BUILD_ROOT%{l_prefix}/etc/pgpool/pgpool.conf.sample \
        $RPM_BUILD_ROOT%{l_prefix}/etc/pgpool/pgpool.conf
    mv  $RPM_BUILD_ROOT%{l_prefix}/etc/pgpool/pool_hba.conf.sample \
        $RPM_BUILD_ROOT%{l_prefix}/etc/pgpool/pool_hba.conf
    %{l_shtool} subst \
        -e 's;\(logdir = .\).*\(.\)$;\1%{l_prefix}/var/pgpool\2;' \
        -e 's;\(socket_dir = .\).*\(.\)$;\1%{l_prefix}/var/pgpool\2;' \
        -e 's;\(backend_socket_dir = .\).*\(.\)$;\1%{l_prefix}/var/postgresql/run\2;' \
        $RPM_BUILD_ROOT%{l_prefix}/etc/pgpool/pgpool.conf

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

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

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/pgpool/*' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/pgpool'

%files -f files

%clean

