# -*- 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
# $Id: Portfile 61995 2009-12-27 07:42:15Z ryandesign@macports.org $

PortSystem        1.0

name              gnucash
version           2.2.9
revision          4
categories        gnome x11
maintainers       nomaintainer
platforms         darwin

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          http://www.gnucash.org/
master_sites      sourceforge \
                  http://www.gnucash.org/pub/gnucash/sources/stable/

# lzma archive for gnucash is 40% smaller than bzip2
use_lzma          yes
checksums         md5     b83c188506f749929a69b48c0632d3b8 \
                  sha1    b7debe12fcb2770f5ec4524ec56c57adbdad19d2 \
                  rmd160  2e16625b59762a52cea09cfa8139138ee21c6fb2

depends_lib       port:p5-xml-parser \
                  path:lib/pkgconfig/glib-2.0.pc:glib2 \
                  port:gconf \
                  port:guile16 \
                  port:slib \
                  port:slib-guile16 \
                  port:popt \
                  port:libgnomeui \
                  port:libgnomeprintui \
                  port:libgtkhtml3 \
                  port:libgsf \
                  port:goffice \
                  port:aqbanking \
                  port:libofx \
                  port:p5-finance-quote

depends_build     port:libtool
depends_run       port:evince

# aqbanking and libgtkhtml3 are not universal
universal_variant no

configure.args    --disable-glibtest \
                  --disable-dependency-tracking --enable-hbci --enable-ofx

patchfiles        patch-configure.diff patch-lib_libc_libc-missing-noop.c.diff \
                  patch-src_bin_gnucash.in.diff \
                  patch-src_quotes_gnc-fq-dump.diff \
                  patch-configure-darwin10.diff

post-patch {
    reinplace "s|DYLD_LIBRARY_PATH|DYLD_FALLBACK_LIBRARY_PATH|g" \
        ${worksrcpath}/configure ${worksrcpath}/src/bin/gnucash.in \
        ${worksrcpath}/src/bin/overrides/gnucash-env.in \
        ${worksrcpath}/src/gnc-test-env
    reinplace "s|@@PREFIX@@|${prefix}|" ${worksrcpath}/src/quotes/gnc-fq-dump
    fs-traverse src_file ${worksrcpath}/src {
        if { [ file isfile ${src_file} ] } {
            reinplace "s|\\<guile\\/|\\<guile16\\/|g"   ${src_file}
            reinplace "s|libguile\\.h|libguile16\\.h|g" ${src_file}
            reinplace "s|exec\\ guile|exec\\ guile16|g" ${src_file}
        }
    }
}

variant without_quotes description {Does not depend on building p5-finance-quote} {
    depends_lib-delete  port:p5-finance-quote
}

variant without_hbci description {Disables HBCI support} {
    depends_lib-delete  port:aqbanking
    configure.args-delete   --enable-hbci
    configure.args-append   --disable-hbci
}

variant without_ofx description {Disables ofx support} {
    depends_lib-delete  port:libofx
    configure.args-delete   --enable-ofx
    configure.args-append   --disable-ofx
}

variant no_x11 {
    depends_run-delete port:evince
    patchfiles-append patch-configure-nox11.diff
}

set gnucash_register_schema ${prefix}/libexec/${name}-gconf-schema.sh

post-destroot {
    # We need to register some stuff to gconf. Just putting the
    # schema files to the right place is not enough.
    # Setup ${prefix}/libexec/${name}-gconf-schema.sh to handle this

    set schemafiles ""
    fs-traverse schema ${destroot}${prefix}/etc/gconf/schemas {
        if {[file isfile $schema]} {
            # The 'string range' bit is to remove the initial ${destroot} stuff
            append schemafiles " [string range $schema [string length ${destroot}] end]"
        }
    }

    if {[string length $schemafiles] > 0} {
        set fh [open ${destroot}${gnucash_register_schema} w]
        puts $fh "#!/bin/sh"
        puts $fh "env GCONF_CONFIG_SOURCE=`${prefix}/bin/gconftool-2 --get-default-source` ${prefix}/bin/gconftool-2 --makefile-install-rule $schemafiles"
        close $fh
        file attributes ${destroot}${gnucash_register_schema} -permissions 0755
    }
}

post-activate {
    if {[file exists ${gnucash_register_schema}]} {
        system "${gnucash_register_schema}"
    }

    if {[variant_isset no_x11]} {
        ui_warn "When you run gnucash, if it pops up a window saying:"
        ui_warn "   An error occurred while loading or saving configuration"
        ui_warn "   information for gnucash."
        ui_warn "it is probably because it cannot connect to"
        ui_warn "the DBus server.  Either place the following in your login"
        ui_warn "shell profile:"
        ui_warn "    eval `dbus-launch --auto-syntax`"
        ui_warn "or invoke gnucash via 'dbus-launch gnucash'. Note that with"
        ui_warn "the latter alternative you may end up with a stray dbus"
        ui_warn "process after you quit gnucash."
    } 
}

livecheck.regex "<title>gnucash .* (\[0-9\]+\\.\[0-9\]*\[02468\]\\.\[0-9\]+) released.*</title>"
