##
##  aqmoney.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_aqmoney     1.9.5
%define       V_ktoblzcheck 1.54

#   package information
Name:         aqmoney
Summary:      Homebanking Command Line Interface
URL:          http://aqmoney.sourceforge.net/
Vendor:       Martin Preuß
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Finance
License:      GPL
Version:      %{V_aqmoney}
Release:      20231228

#   list of sources
Source0:      http://download.sourceforge.net/aqmoney/aqmoney2-%{V_aqmoney}.tar.gz
Source1:      http://download.sourceforge.net/ktoblzcheck/ktoblzcheck-%{V_ktoblzcheck}.tar.gz
Patch0:       aqmoney.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  aqbanking
PreReq:       aqbanking

%description
    AqMoney is a command line interface for the AqBanking/AqHBCI and
    OpenHBCI libraries in order to perform financial transactions
    through the German HomeBanking Computer Interface (HBCI).

%track
    prog aqmoney = {
        version   = %{V_aqmoney}
        url       = http://sourceforge.net/projects/aqmoney/files/
        regex     = aqmoney2-(__VER__)\.tar\.gz
    }
    prog aqmoney:ktoblzcheck = {
        version   = %{V_ktoblzcheck}
        url       = http://sourceforge.net/projects/ktoblzcheck/files/
        regex     = ktoblzcheck-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -c
    %setup -q -D -T -a 1
    %patch -p0 -d aqmoney2-%{V_aqmoney}

%build
    #   build KtoBlzCheck library
    ( cd ktoblzcheck-%{V_ktoblzcheck}
      CC="%{l_cc}" \
      CXX="%{l_cxx}" \
      CFLAGS="%{l_cflags -O}" \
      CXXFLAGS="%{l_cxxflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          --disable-shared
      %{l_make} %{l_mflags -O}

      #   pre-install it and adjust for temporary usage by AqMoney build (below)
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?

    #   configure and build program
    ( cd aqmoney2-%{version}
      %{l_shtool} subst \
          -e 's;ktoblzcheck_datafile="";;' \
          configure
      CC="%{l_cxx}" \
      CXX="%{l_cxx}" \
      CFLAGS="%{l_cflags -O}" \
      CXXFLAGS="%{l_cxxflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          --with-openhbci-prefix=%{l_prefix} \
          --with-ktoblzcheck-includes=$RPM_BUILD_ROOT%{l_prefix}/include \
          --with-ktoblzcheck-libs=$RPM_BUILD_ROOT%{l_prefix}/lib \
          --with-ktoblzcheck-datafile=%{l_prefix}/share/ktoblzcheck/bankdata.txt \
          --disable-shared \
          --disable-nls
      %{l_make} %{l_mflags -O}
    ) || exit $?

%install

    #   install programs
    ( cd ktoblzcheck-%{V_ktoblzcheck}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?
    ( cd aqmoney2-%{version}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?

    #   workaround problems with AqHBCI plugin run-time
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/aqmoney
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/aqmoney2 \
       $RPM_BUILD_ROOT%{l_prefix}/libexec/aqmoney/aqmoney
    ( echo "#!/bin/sh"
      echo "LD_LIBRARY_PATH=\"%{l_prefix}/lib/openhbci2:/lib:/usr/lib\""
      echo "export LD_LIBRARY_PATH"
      echo "exec %{l_prefix}/libexec/aqmoney/aqmoney \${1+\"\$@\"}"
    ) >$RPM_BUILD_ROOT%{l_prefix}/bin/aqmoney
    chmod a+x $RPM_BUILD_ROOT%{l_prefix}/bin/aqmoney

    #   post-adjust installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    mv $RPM_BUILD_ROOT%{l_prefix}/man/man1/aqmoney2.1 \
       $RPM_BUILD_ROOT%{l_prefix}/man/man1/aqmoney.1
    mv $RPM_BUILD_ROOT%{l_prefix}/include/aqmoney2 \
       $RPM_BUILD_ROOT%{l_prefix}/include/aqmoney
    %{l_shtool} subst \
       -e 's;aqmoney2;aqmoney;g' \
       $RPM_BUILD_ROOT%{l_prefix}/man/man1/aqmoney.1

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

%files -f files

%clean

