# -*- 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
conflicts         gnucash gnucash-devel
conflicts-delete  ${subport}
version           5.13
perl5.branches    5.34
categories        gnome x11
license           GPL-2+
maintainers       {dports @drkp} openmaintainer
platforms         darwin

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

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  eb5f2bee7e73960b4af6d95ae7a54b04c8b7ccec \
                  sha256  082eecc332b722f223d3f2512626ebe5ef63f94aaf5c409e87526c654464aef5 \
                  size    15343812

patchfiles-append   macintegration_4-files.patch
patchfiles-append   patch-CMakeLists.txt.diff
post-patch {
    reinplace "s|set(HAVE_OSX_KEYCHAIN 1)||" ${worksrcpath}/CMakeLists.txt
    reinplace "s|-Werror||" ${worksrcpath}/CMakeLists.txt

    reinplace "s|@@PYTHON_PKGD@@|${frameworks_dir}/Python.framework/Versions/${py_ver}/lib/python${py_ver}/site-packages|" ${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

set py_ver                  3.11
set py_ver_nodot            [string map {. {}} ${py_ver}]

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.0.pc:webkit2-gtk \
                  port:libdbi \
                  port:libdbi-drivers \
                  port:aqbanking6-gtk3 \
                  port:libofx \
                  port:python${py_ver_nodot} \
                  path:lib/pkgconfig/icu-uc.pc:icu \
                  port:libsecret \
                  port:libxml2

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

default_variants    +docs

# aqbanking is not universal
universal_variant no

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

# force use of the requested python
configure.args-append -DPython3_EXECUTABLE:FILEPATH=${prefix}/bin/python${py_ver}

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"
}

if {$subport eq $name} {
    livecheck.regex "/${name} .*/(\[0-9\]+\\.\[0-9\]*\[0-9\])/"
}

# There currently isn't a separate beta release.
subport gnucash-devel {
    livecheck.regex "/${name} .*/(\[0-9\]+\\.\[0-9\]*\[0-9\])/"
}

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."
