# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem        1.0
PortGroup         perl5 1.0
PortGroup         cmake 1.1
PortGroup         boost 1.0
PortGroup         legacysupport 1.1

legacysupport.newest_darwin_requires_legacy 17
legacysupport.use_mp_libcxx                 yes

name              gnucash
version           5.16
revision          2
perl5.branches    5.34
categories        gnome x11
license           GPL-2+
maintainers       {dports @drkp} openmaintainer

set branch        [join [lrange [split ${version} .] 0 1] .]

# gnucash embeds a Python interpreter (used for in-app Python reports) that is
# built against a single "primary" Python version; the same build also installs
# the gnucash Python bindings for that version. The remaining versions get
# bindings-only subports. See https://trac.macports.org/ticket/74193
set python_primary  314
set primary_dotted  [string index ${python_primary} 0].[string range ${python_primary} 1 end]
set python_versions {311 312 313}

description       a personal and small-business financial-accounting software
long_description  GnuCash is a personal and small-business \
                  financial-accounting software, freely licensed under the \
                  GNU GPL.  Designed to be easy to use, yet powerful and \
                  flexible, GnuCash allows you to track bank accounts, \
                  stocks, income and expenses. As quick and intuitive to \
                  use as a checkbook register, it is based on professional \
                  accounting principles to ensure balanced books and \
                  accurate reports.

homepage          https://www.gnucash.org
default master_sites {sourceforge:project/gnucash/gnucash%20%28stable%29/${version}}

use_bzip2         yes

distname          ${name}-${version}
worksrcdir        ${name}-${version}

checksums         rmd160  ec0a7dcac373bf2c697160c40bad0937a31a7a8d \
                  sha256  bb9b6c67ebb6a3e06d9ff4f4e0417a97bef0159d26912be9b0e75a32901fd493 \
                  size    15539568

patchfiles-append   macintegration_4-files.patch
patchfiles-append   patch-CMakeLists.txt.diff

# window-reconcile.cpp uses std::unordered_map without including the header;
# fails on older libc++ (macOS 11 and earlier).
patchfiles-append   patch-window-reconcile-unordered-map.diff
post-patch {
    reinplace "s|set(HAVE_OSX_KEYCHAIN 1)||" ${worksrcpath}/CMakeLists.txt
    reinplace "s|-Werror||" ${worksrcpath}/CMakeLists.txt

    # Drop in a patched version of glibconfig.h via
    # https://git.gnome.org/browse/gtk-osx/plain/patches/glib-gint64-long-long.patch
    # because gnucash incorrectly assumes int64_t and gint64 are
    # the same size. See https://trac.macports.org/ticket/55446
    xinstall -d ${workpath}/glib-2.0/include
    file copy ${prefix}/lib/glib-2.0/include/glibconfig.h ${workpath}/glib-2.0/include
    system -W ${workpath}/glib-2.0/include "patch -p1 <${filespath}/patch-glibconfig.h.diff"
}

configure.env-append PKG_CONFIG_GLIB_2_0_LIBDIR=${workpath}

if { ${os.platform} eq "darwin" && ${os.major} <= [option legacysupport.newest_darwin_requires_legacy] &&
        [option legacysupport.use_mp_libcxx] && ${configure.cxx_stdlib} eq "libc++" } {
            # This is required when compiling with guile for older macos
            # The env variable is used in common/cmake_modules/GncAddSchemeTargets.cmake
            configure.env-append DYLD_LIBRARY_PATH=${prefix}/lib/libcxx
        }

boost.version               1.76

compiler.cxx_standard       2017

configure.cppflags-delete   -I${prefix}/include
configure.cppflags-append   -isystem${prefix}/include
configure.cppflags-append -I${worksrcpath}

depends_build-append     port:pkgconfig \
                         port:intltool \
                         port:autoconf \
                         port:automake \
                         port:libtool \
                         port:gtest \
                         port:swig-guile \
                         port:swig-python

depends_lib       port:guile-3.0 \
                  port:perl${perl5.major} \
                  port:p${perl5.major}-date-manip \
                  port:p${perl5.major}-finance-quote \
                  port:p${perl5.major}-json-parse \
                  path:lib/pkgconfig/gtk+-3.0.pc:gtk3 \
                  path:lib/pkgconfig/webkit2gtk-4.1.pc:webkit2-gtk \
                  port:libdbi \
                  port:libdbi-drivers \
                  port:aqbanking6-gtk3 \
                  port:libofx \
                  path:lib/pkgconfig/icu-uc.pc:icu \
                  port:libsecret \
                  port:libxml2

# aqbanking is not universal
universal_variant no

configure.args    -DENABLE_BINRELOC=OFF \
                  -DCMAKE_INSTALL_SYSCONFDIR=${prefix}/etc \
                  -DWITH_PYTHON=ON \
                  -DPython3_EXECUTABLE:FILEPATH=${prefix}/bin/python${primary_dotted} \
                  -DPERL_EXECUTABLE:FILEPATH=${perl5.bin} \
                  -DPOD2MAN_EXECUTABLE:FILEPATH=${prefix}/bin/pod2man-${perl5.major}


if {${subport} eq ${name} || ${subport} eq "gnucash-devel"} {
    conflicts         gnucash gnucash-devel
    conflicts-delete  ${subport}

    # The main port builds the embedded Python module (libgncmod-python, used
    # for in-app Python reports) and the gnucash Python bindings against the
    # primary Python version.
    depends_lib-append  port:python${python_primary}

    post-patch {
        reinplace "s|@@PYTHON_PKGD@@|${frameworks_dir}/Python.framework/Versions/${primary_dotted}/lib/python${primary_dotted}/site-packages|" ${worksrcpath}/CMakeLists.txt
    }

    variant docs description "Install documentation" {
        depends_run-append      port:gnucash-docs
    }

    default_variants    +docs

    post-destroot {
        file delete ${destroot}${prefix}/lib/libgwengui-gtk3.dylib
        file delete ${destroot}${prefix}/share/glib-2.0/schemas/gschemas.compiled
    }

    post-activate {
        system "${prefix}/bin/gtk-update-icon-cache -f -t ${prefix}/share/icons/hicolor"
        system "${prefix}/bin/glib-compile-schemas ${prefix}/share/glib-2.0/schemas"
    }

    notes "gnucash DBI backend support requires the libdbi-drivers port to be built with support for the desired databases. sqlite3 is available by default; for MySQL or PostgreSQL support, ensure that the corresponding variant is selected for libdbi-drivers."
    notes-append "gnucash includes Python ${primary_dotted} support: the embedded Python module for in-app reports and the gnucash Python bindings. Bindings for other Python versions are available via the py*-gnucash ports."
}

# Python bindings subports
foreach pyver ${python_versions} {
    subport py${pyver}-gnucash {
        set pyver_dotted [string index ${pyver} 0].[string range ${pyver} 1 end]

        categories          gnome python
        description         Python ${pyver_dotted} bindings for GnuCash
        long_description    Python ${pyver_dotted} bindings for the GnuCash \
                            financial-accounting application, allowing \
                            programmatic access to GnuCash data files.

        depends_lib             port:gnucash \
                                port:python${pyver}

        configure.args-append   -DWITH_GNUCASH=OFF \
                                -DWITH_SQL=OFF
        configure.args-replace  -DPython3_EXECUTABLE:FILEPATH=${prefix}/bin/python${primary_dotted} \
                                -DPython3_EXECUTABLE:FILEPATH=${prefix}/bin/python${pyver_dotted}

        post-patch {
            reinplace "s|@@PYTHON_PKGD@@|${frameworks_dir}/Python.framework/Versions/${pyver_dotted}/lib/python${pyver_dotted}/site-packages|" ${worksrcpath}/CMakeLists.txt
        }

        post-destroot {
            set python_sp ${frameworks_dir}/Python.framework/Versions/${pyver_dotted}/lib/python${pyver_dotted}/site-packages
            foreach f {_gnucash_core_c.so _sw_core_utils.so _sw_app_utils.so} {
                if {![file exists ${destroot}${python_sp}/gnucash/${f}]} {
                    return -code error "Expected Python binding ${f} not found in ${destroot}${python_sp}/gnucash/"
                }
            }
            # Preserve only the Python module; the main gnucash port provides everything else
            file mkdir ${workpath}/_python_tmp
            file rename ${destroot}${python_sp}/gnucash ${workpath}/_python_tmp/gnucash
            # Remove everything from destroot
            foreach item [glob ${destroot}/*] {
                file delete -force ${item}
            }
            # Reinstall only the Python module
            file mkdir ${destroot}${python_sp}
            file rename ${workpath}/_python_tmp/gnucash ${destroot}${python_sp}/gnucash
            file delete -force ${workpath}/_python_tmp
        }

        livecheck.type      none
    }
}

# The Python ${primary_dotted} bindings are now installed by the main gnucash
# port, so the former py${python_primary}-gnucash subport is obsolete.
subport py${python_primary}-gnucash {
    PortGroup       obsolete 1.0

    replaced_by     gnucash
    obsolete.note   "The GnuCash Python ${primary_dotted} bindings are now provided by the main gnucash port."
}

livecheck.url    https://www.gnucash.org/download.phtml

if {$subport eq $name} {
    livecheck.regex  {Stable release \(([0-9]+(?:\.[0-9]+)+)\)}
}

# There currently isn't a separate beta release.
subport gnucash-devel {
    livecheck.regex  {Stable release \(([0-9]+(?:\.[0-9]+)+)\)}
}
