# -*- 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

name            dbus-python
version         1.3.2

set python_versions {27 35 36 37 38 39 310 311 312 313 314}

# this default version should stay synchronized with python_get_default_version
#    in the python PortGroup
set python_default_version 314

maintainers     {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
license         MIT
categories      devel python
description     Python bindings for the dbus message bus system.
long_description \
    {*}${description}
homepage        https://www.freedesktop.org/wiki/Software/dbus/

if {${name} eq ${subport}} {
    PortGroup   stub 1.0

    revision    2
    supported_archs noarch
    platforms       any

    # dbus-python as a stub port that depends on the default dbus-pythonXY
    depends_lib port:${name}${python_default_version}
}

foreach python_version ${python_versions} {
    revision    1

    set python_branch [string index ${python_version} 0].[string range ${python_version} 1 end]

    subport ${name}${python_version} {

        master_sites    https://dbus.freedesktop.org/releases/dbus-python/

        if {${python_version} == 27} {
            version             1.2.18

            checksums           rmd160  5552b124fb35d5271b8a4813e8635cbf1bdb5836 \
                                sha256  92bdd1e68b45596c833307a5ff4b217ee6929a1502f5341bae28fd120acf7260 \
                                size    578204

            patchfiles-append dynamic_lookup-11.patch
        } else {

            checksums           rmd160  344f6d035fc8ca806585b6d566221d4769794fab \
                                sha256  ad67819308618b5069537be237f8e68ca1c7fcc95ee4a121fe6845b1418248f8 \
                                size    605495
        }

        distname        ${name}-${version}

        depends_build   port:pkgconfig

        depends_lib                               \
            port:dbus                             \
            port:gettext-runtime                  \
            path:lib/pkgconfig/glib-2.0.pc:glib2  \
            port:python${python_version}

        set python_prefix ${frameworks_dir}/Python.framework/Versions/${python_branch}

        configure.pre_args-replace --prefix=${prefix} --prefix=${python_prefix}

        configure.args                                                    \
            --docdir=${prefix}/share/doc/${subport}                       \
            --disable-documentation

        set includedir_base ${python_prefix}/include/python${python_branch}
        set python_config ${prefix}/bin/python${python_branch}-config

        pre-configure {
            # determine any abiflags; works for Python 3+ only; will
            # catch for Python 2.7 and we know there are no flags.
            if {[catch {set abiflags [exec ${python_config} --abiflags]}]} {
                set abiflags ""
            }
            configure.args-append --includedir=${includedir_base}${abiflags}
        }

        configure.pkg_config_path  ${python_prefix}/lib/pkgconfig

        configure.python    ${prefix}/bin/python${python_branch}

        if {${python_version} > 39} {
            variant doc description {Enable documentation building} {}
        }
        # ${python_version} and ${python_branch} do not have the right value inside variant block,
        #    so put it here.
        if {${python_version} > 39 && [variant_isset doc]} {
            configure.args-replace --disable-documentation --enable-documentation
            depends_build-append                           \
                port:py${python_version}-sphinx            \
                port:py${python_version}-sphinx_rtd_theme
        }

        variant examples description {Install examples} {
            post-destroot {
                set docdir ${prefix}/share/doc/${subport}
                set exdir ${docdir}/examples
                xinstall -d ${destroot}${exdir}
                xinstall -m 644 {*}[glob ${worksrcpath}/examples/*] ${destroot}${exdir}
            }
        }

        # pyXY-gobject, which is required to test, is only supported on certain versions of python
        if { [lsearch -exact {27 34 35 36 37 38 39} ${python_version}] != -1 } {
            variant test {Run tests}

            test.run yes
            test.target check
        }

        # ${python_version} and ${python_branch} do not have the right value inside variant block,
        #    so put it here.
        if { [variant_exists test] } {
            if { [variant_isset test] } {
                depends_build-append port:py${python_version}-gobject
            } else {
                pre-test {
                    ui_error "test variant must be activated to enable test support."
                    error "Please enable test variant."
                }
            }
        }

        platform darwin 9 {
            # see https://trac.macports.org/ticket/13782
            # see https://trac.macports.org/ticket/12966
            post-patch {
                reinplace "s|-export-symbols-regex.*|\\\\|g" \
                    ${worksrcpath}/Makefile.in
            }
        }
    }
}

if {${name} eq ${subport}} {
    livecheck.url   https://dbus.freedesktop.org/releases/dbus-python/
} else {
    livecheck.type  none
}
