##
##  aqbanking.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_openpkg     2.3.3
%define       V_aqbanking   2.3.3
%define       V_gwenhywfar  2.6.2
%define       V_openhbci2   1.9.4
%define       V_openhbci1   0.9.18

#   package information
Name:         aqbanking
Summary:      Banking Abstraction Library
URL:          http://www.aquamaniac.de/aqbanking/
Vendor:       Martin Preuß
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Finance
License:      GPL
Version:      %{V_openpkg}
Release:      20080101

#   list of sources
Source0:      http://download.sourceforge.net/aqbanking/aqbanking-%{V_aqbanking}.tar.gz
Source1:      http://download.sourceforge.net/gwenhywfar/gwenhywfar-%{V_gwenhywfar}.tar.gz
Source2:      http://download.sourceforge.net/openhbci/openhbci2-%{V_openhbci2}.tar.gz
Source3:      http://download.sourceforge.net/openhbci/openhbci-%{V_openhbci1}.tar.gz
Patch0:       aqbanking.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, gcc::with_cxx = yes
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  openssl
PreReq:       openssl

%description
    AqBanking is a middle layer between a home banking applicaton and
    various online banking backend libraries which provide various bank
    access methods. AqBanking supports the following banking operations:
    retrieving account balance, retrieving account statements,
    transfers, debit notes and EU transfers.

%track
    prog aqbanking:aqbanking = {
        version   = %{V_aqbanking}
        url       = http://sourceforge.net/projects/aqbanking/files/
        regex     = aqbanking-(\d+\.\d+(\.\d+)+)\.tar\.gz
    }
    prog aqbanking:gwenhywfar = {
        version   = %{V_gwenhywfar}
        url       = http://sourceforge.net/projects/gwenhywfar/files/
        regex     = gwenhywfar-(\d+\.\d+\.\d+)\.tar\.gz
    }
    prog aqbanking:openhbci2 = {
        version   = %{V_openhbci2}
        url       = http://sourceforge.net/projects/openhbci/files/
        regex     = openhbci2-(\d+\.\d+\.\d+)\.tar\.gz
    }
    prog aqbanking:openhbci1 = {
        version   = %{V_openhbci1}
        url       = http://sourceforge.net/projects/openhbci/files/
        regex     = openhbci-(\d+\.\d+\.\d+)\.tar\.gz
    }

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

%build
    #   build Gwhenhywfar
    ( cd gwenhywfar-%{V_gwenhywfar}
      %{l_shtool} subst \
          -e 's;rm -Rf gwenhywfar;#;' \
          configure
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="-I`pwd`/gwenhywfar %{l_cppflags}" \
      LDFLAGS="-L`pwd`/src %{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          --enable-ssl \
          --with-openssl-includes=%{l_prefix}/include \
          --with-openssl-libs=%{l_prefix}/lib \
          --disable-nls \
          --disable-shared

      #   build the library
      %{l_make} %{l_mflags}

      #   pre-install it and adjust for temporary usage by AqBanking build (below)
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
      %{l_shtool} subst \
          -e "s;%{l_prefix};$RPM_BUILD_ROOT%{l_prefix};g" \
          $RPM_BUILD_ROOT%{l_prefix}/bin/gwenhywfar-config
    ) || exit $?

    #   build OpenHBCI v1 (legacy)
    ( cd openhbci-%{V_openhbci1}
      CC="%{l_prefix}/bin/gcc" \
      CXX="%{l_prefix}/bin/g++" \
      CFLAGS="%{l_cflags -O}" \
      CXXFLAGS="%{l_cxxflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          --with-plugin-path=%{l_prefix}/lib/openhbci/plugins \
          --with-gwen-dir=$RPM_BUILD_ROOT%{l_prefix} \
          --with-openssl-includes=%{l_prefix}/include \
          --with-openssl-libs=%{l_prefix}/lib \
          --enable-shared \
          --enable-static
      %{l_make} %{l_mflags}
    ) || exit $?

    #   build OpenHBCI v2 (legacy)
    ( cd openhbci2-%{V_openhbci2}
      %{l_shtool} subst \
          -e 's;^\(hbcixml_LDADD =\);\1 -static ;g' \
          src/tools/hbcixml/Makefile.in
      CC="%{l_prefix}/bin/gcc" \
      CXX="%{l_prefix}/bin/g++" \
      CFLAGS="%{l_cflags -O}" \
      CXXFLAGS="%{l_cxxflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          --with-plugin-path=%{l_prefix}/lib/openhbci2/plugins \
          --with-xmldata-dir=%{l_prefix}/share/openhbci2/xmldata \
          --with-gwen-dir=$RPM_BUILD_ROOT%{l_prefix} \
          --with-openssl-includes=%{l_prefix}/include \
          --with-openssl-libs=%{l_prefix}/lib \
          --enable-shared \
          --enable-static
      %{l_make} %{l_mflags}
    ) || exit $?

    #   build AqBanking
    ( cd aqbanking-%{V_aqbanking}
      echo 'ac_cv_header_iconv_h=no' >config.cache
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="-I`pwd`/aqbanking %{l_cppflags}" \
      LDFLAGS="-L`pwd`/src/libs/aqbanking %{l_ldflags}" \
      LIBS="-lssl -lcrypto" \
      SHELL="%{l_bash}" \
      ./configure \
          --cache-file=./config.cache \
          --prefix=%{l_prefix} \
          --with-gwen-dir=$RPM_BUILD_ROOT%{l_prefix} \
          --disable-chipcard-client \
          --with-backends="aqhbci aqdtaus" \
          --with-frontends="cbanking" \
          --disable-nls \
          --enable-shared \
          --enable-static
      %{l_make} %{l_mflags} \
          SHELL="%{l_bash}"
    ) || exit $?

%install

    #   install Gwhenhywfar
    ( cd gwenhywfar-%{V_gwenhywfar}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?

    #   install OpenHBCI v1 (legacy)
    ( cd openhbci-%{V_openhbci1}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
      strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
      rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/openhbci/plugins/*/*/*.a
      mv $RPM_BUILD_ROOT%{l_prefix}/lib/*.so* \
         $RPM_BUILD_ROOT%{l_prefix}/lib/openhbci/
    ) || exit $?

    #   install OpenHBCI v2 (legacy)
    ( cd openhbci2-%{V_openhbci2}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
      strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
      rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/openhbci2/plugins/*/*/*.a
      mv $RPM_BUILD_ROOT%{l_prefix}/lib/*.so* \
         $RPM_BUILD_ROOT%{l_prefix}/lib/openhbci2/
      mv $RPM_BUILD_ROOT%{l_prefix}/bin/hbcixml \
         $RPM_BUILD_ROOT%{l_prefix}/bin/openhbci2-hbcixml
    ) || exit $?

    #   install AqBanking
    ( cd aqbanking-%{V_aqbanking}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/gwen-public-ca.crt
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/*.so*
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/aqbanking/plugins/0/*/*.a
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc

    #   post-adjust installation
    %{l_shtool} subst \
        -e 's;-laqbanking";-laqbanking -lgwenhywfar";' \
        -e 's;-laqbankingpp";-laqbankingpp -lgwenpp";' \
        -e "s;$RPM_BUILD_ROOT%{l_prefix};%{l_prefix};" \
        $RPM_BUILD_ROOT%{l_prefix}/bin/aqbanking-config
    %{l_shtool} subst \
        -e 's;^\(dlname=.\).*\(.\)$;\1\2;' \
        -e 's;^\(library_names=.\).*\(.\)$;\1\2;' \
        $RPM_BUILD_ROOT%{l_prefix}/lib/lib*.la

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

%files -f files

%clean

