##
##  postgresql-madlib.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_madlib 1.12
%define       V_eigen  3.3.4
%define       V_pyxb   1.2.6

#   package information
Name:         postgresql-madlib
Summary:      PostgreSQL Extension: MADlib
URL:          http://madlib.apache.org/
Vendor:       Apache Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Apache
Version:      %{V_madlib}
Release:      20171216

#   list of sources
Source0:      http://www.apache.org/dist/madlib/%{V_madlib}/apache-madlib-%{V_madlib}-src.tar.gz
Source1:      http://bitbucket.org/eigen/eigen/get/%{V_eigen}.tar.gz
Source2:      https://download.sourceforge.net/pyxb/PyXB-%{V_pyxb}.tar.gz
Patch0:       postgresql-madlib.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, flex, bison, cmake, gcc, gcc::with_cxx = yes
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  postgresql, postgresql::with_python = yes, python, boost
PreReq:       postgresql, postgresql::with_python = yes, python, boost

%description
    This is a PostgreSQL extension providing MADlib, a SQL-based Machine
    Learning library.

%track
    prog postgresql-madlib:madlib = {
        version   = %{V_madlib}
        url       = http://madlib.apache.org/download.html
        regex     = apache-madlib-(\d+\.\d+(?:\.\d+)?)-src\.tar\.gz
    }
    prog postgresql-madlib:eigen = {
        version   = %{V_eigen}
        url       = http://eigen.tuxfamily.org/index.php?title=Main_Page
        regex     = (\d+\.\d+(?:\.\d+)?)\.tar\.gz
    }
    prog postgresql-madlib:pyxb = {
        version   = %{V_pyxb}
        url       = https://sourceforge.net/projects/pyxb/files/pyxb/
        regex     = PyXB-(\d+\.\d+(?:\.\d+)?)\.tar\.gz
    }

%prep
    %setup -q -n apache-madlib-%{version}-src
    %patch -p0

%build
    #   configure program
    ./configure \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_INSTALL_PREFIX="%{l_prefix}/libexec/postgresql-madlib" \
        -DCMAKE_C_COMPILER="%{l_cc}" \
        -DCMAKE_C_FLAGS="%{l_cflags} %{l_cppflags}" \
        -DCMAKE_EXE_LINKER_FLAGS="%{l_ldflags}" \
        -DCMAKE_CXX_COMPILER="%{l_cxx}" \
        -DEIGEN_TAR_SOURCE=%{SOURCE1} \
        -DPYXB_TAR_SOURCE=%{SOURCE2}

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

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

    #   strip down installation
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/libexec/postgresql-madlib/ports/greenplum
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/libexec/postgresql-madlib/ports/hawq
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/libexec/postgresql-madlib/doc
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/libexec/postgresql-madlib/[A-Z]*
    rm -f  $RPM_BUILD_ROOT%{l_prefix}/libexec/postgresql-madlib/licenses/MADlib.txt

    #   provide wrapper executable
    %{l_shtool} subst \
        -e 's;/bin/bash;%{l_bash};g' \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/postgresql-madlib/bin/madpack
    (   echo "#!/bin/sh"
        echo "exec %{l_prefix}/libexec/postgresql-madlib/bin/madpack \${1+\"\$@\"}"
    ) >madpack.sh
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin
    %{l_shtool} install -c -m 755 \
        madpack.sh $RPM_BUILD_ROOT%{l_prefix}/bin/madpack

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

