##
##  libdbi.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_dbi         0.8.3
%define       V_dbi_drivers 0.8.3-1

#   package information
Name:         libdbi
Summary:      Database Independent (DBI) Abstraction Layer for C
URL:          http://libdbi.sourceforge.net/
Vendor:       David A. Parker
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      LGPL
Version:      %{V_dbi}
Release:      20080607

#   package options
%option       with_sqlite  yes
%option       with_mysql   no
%option       with_pgsql   no

#   list of sources
Source0:      http://download.sourceforge.net/libdbi/libdbi-%{V_dbi}.tar.gz
Source1:      http://download.sourceforge.net/libdbi-drivers/libdbi-drivers-%{V_dbi_drivers}.tar.gz
Patch0:       libdbi.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
%if "%{with_sqlite}" == "yes"
BuildPreReq:  sqlite
PreReq:       sqlite
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql, openssl
PreReq:       mysql, openssl
%endif
%if "%{with_pgsql}" == "yes"
BuildPreReq:  postgresql, openssl
PreReq:       postgresql, openssl
%endif

%description
    libdbi implements a database-independent abstraction layer in C,
    similar to the DBI/DBD layer in Perl. Writing one generic set of
    code, programmers can leverage the power of multiple databases and
    multiple simultaneous database connections by using this framework.
    Currently the SQLite, MySQL, and PostgreSQL RDBMS are supported.

%track
    prog libdbi = {
        version   = %{V_dbi}
        url       = http://sourceforge.net/projects/libdbi/files/
        regex     = libdbi-(__VER__)\.tar\.gz
    }
    prog libdbi:drivers = {
        version   = %{V_dbi_drivers}
        url       = http://sourceforge.net/projects/libdbi-drivers/files/
        regex     = libdbi-drivers-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -c
    %setup -q -D -T -a 1
    %patch -p0

%build
    ( cd libdbi-[0-9]*
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          --disable-shared
      %{l_make} %{l_mflags -O}
    ) || exit $?
    ( cd libdbi-drivers-*
      LIBS=""
%if "%{with_mysql}" == "yes"
      LIBS="$LIBS -lssl -lcrypto -lm"
%endif
%if "%{with_pgsql}" == "yes"
      LIBS="$LIBS -lssl -lcrypto -lcrypt"
%endif
      LIBS="$LIBS `pwd`/../libdbi-%{V_dbi}/src/.libs/libdbi.a"
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      LIBS="$LIBS" \
      ./configure \
          --prefix=%{l_prefix} \
          --with-dbi-incdir=`pwd`/../libdbi-%{V_dbi}/include \
          --with-dbi-libdir=`pwd`/../libdbi-%{V_dbi}/src \
%if "%{with_sqlite}" == "yes"
          --with-sqlite3 \
          --with-sqlite3-dir=%{l_prefix} \
          --with-sqlite3-incdir=%{l_prefix}/include \
          --with-sqlite3-libdir=%{l_prefix}/lib \
%endif
%if "%{with_mysql}" == "yes"
          --with-mysql \
          --with-mysql-dir=%{l_prefix} \
          --with-mysql-incdir=%{l_prefix}/include \
          --with-mysql-libdir=%{l_prefix}/lib/mysql \
%endif
%if "%{with_pgsql}" == "yes"
          --with-pgsql \
          --with-pgsql-dir=%{l_prefix} \
          --with-pgsql-incdir=%{l_prefix}/include/postgresql \
          --with-pgsql-libdir=%{l_prefix}/lib \
%endif
          --disable-static \
          --enable-shared
      %{l_make} %{l_mflags -O}
    ) || exit $?

%install
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share/libdbi
    ( cd libdbi-[0-9]*
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?
    ( cd libdbi-drivers-*
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/libdbi
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%doc %{l_prefix}/share/libdbi/*.pdf'

%files -f files

%clean

