# -*- 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.14
revision          3
perl5.branches    5.34
categories        gnome x11
license           GPL-2+
maintainers       {dports @drkp} openmaintainer

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

set python_versions {311 312 313 314}

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  bae54a6bd9ef6b8beeac8eaa9a0c7cd0d37de4b6 \
                  sha256  0c6fd20214da86a9a0443359f7b62d9a2bd4ed802fd680853da4b757a371ac91 \
                  size    16406297

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

    # 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.0.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=OFF \
                  -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}

    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 Python modules have moved to the py*-gnucash ports and are no longer installed by default."
}

# 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-replace  -DWITH_PYTHON=OFF -DWITH_PYTHON=ON
        configure.args-append   -DWITH_GNUCASH=OFF \
                                -DWITH_SQL=OFF \
                                -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
    }
}

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]+)+)\)}
}
