# -*- 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                subversion-pythonbindings
version             1.14.3
revision            0

categories          devel python
license             Apache-2
maintainers         nomaintainer

description         Python bindings for the subversion version control system (svn)
long_description    Subversion (svn) is a version control system designed to be \
                    as similar to cvs(1) as possible, while fixing many \
                    outstanding problems with cvs(1). The python bindings \
                    provide access to subversion API from python.

homepage            https://subversion.apache.org/

master_sites        apache:subversion
use_bzip2           yes

checksums           rmd160  18fb11f2e65213a2878b57ef807864b245f57cc5 \
                    sha256  949efd451a09435f7e8573574c71c7b71b194d844890fa49cd61d2262ea1a440 \
                    size    8569985

distname            subversion-${version}
dist_subdir         subversion

use_parallel_build  no

set python.versions {310 311 312}
foreach python_version ${python.versions} {
    set p_idx [lsearch ${python.versions} ${python_version}]
    set other_pythons [lreplace ${python.versions} $p_idx $p_idx]

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

    subport ${name}-${python_ver_dot} {
        set python_version_dot ${python_ver_dot}
        set python_version_no_dot [string map {. ""} ${python_ver_dot}]
        foreach c_python ${other_pythons} {
            set c_python_ver_dot [string index ${c_python} 0].[string range ${c_python} 1 end]
            conflicts-append ${name}-${c_python_ver_dot}
        }

        if {${python_version} >= 312} {
            depends_build-append \
                port:py${python_version_no_dot}-setuptools
        }
    }
}

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

    # depend on a default perl version
    depends_lib     port:${name}-3.13
    # avoid conflict with old version when upgrading
    replaced_by     ${name}-3.13

    livecheck.type  regex
    livecheck.url   https://svn.apache.org/repos/asf/subversion/tags/
    livecheck.regex "(\\d+\\.\\d+\\.\\d+)/"
} else {
    depends_build-append \
                    port:pkgconfig \
                    port:py3c \
                    port:swig3

    depends_lib-append  \
                    port:apr \
                    port:apr-util \
                    port:cyrus-sasl2 \
                    port:db48 \
                    port:expat \
                    port:gettext \
                    port:libcomerr \
                    port:libiconv \
                    port:libmagic \
                    port:libutf8proc \
                    port:lz4 \
                    port:python${python_version_no_dot} \
                    port:serf1 \
                    port:sqlite3 \
                    port:subversion \
                    port:zlib

    patchfiles      dynamic_lookup-11.patch

    post-patch {
        system "export LANG=C; cd ${worksrcpath}; find build* Makefile.in subversion/bindings/swig/python -type f -print0 | xargs -0 sed -i -e 's/libsvn_swig_py/libsvn_swig_py${python_version_no_dot}/g'"
        file rename \
            ${worksrcpath}/subversion/bindings/swig/python/libsvn_swig_py \
            ${worksrcpath}/subversion/bindings/swig/python/libsvn_swig_py${python_version_no_dot}
    }

    configure.args      --with-berkeley-db=:${prefix}/include/db48:${prefix}/lib/db48:db-4.8 \
                        --without-gnome-keyring \
                        --without-apxs \
                        --without-boost \
                        --with-swig=${prefix}/bin/swig3

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

    build.target        swig-py
    destroot.target     install-swig-py DESTDIR=${destroot}

    test.run            yes
    test.target         check-swig-py

    pre-test {
        set x {}
        fs-traverse dir ${worksrcpath}/subversion {
            if {[file tail ${dir}] == ".libs" && [file isdirectory ${dir}]} {
                lappend x ${dir}
                continue
            }
        }
        test.env-append DYLD_LIBRARY_PATH=[join ${x} ":"]
    }

    pre-configure {
        reinplace "s|hardcode_direct=yes|hardcode_direct=no|g" \
            ${worksrcpath}/configure
    }

    post-configure {
        reinplace "s|need_relink=yes|need_relink=no|g" \
            ${worksrcpath}/libtool
    }

    variant no_bdb description {Build without support for BerkeleyDB repositories} {
        depends_lib-delete      port:db48
        configure.args-delete   --with-berkeley-db=:${prefix}/include/db48:${prefix}/lib/db48:db-4.8
        configure.args-append   --without-berkeley-db
    }

    post-destroot {
        set PYTHON_PATH ${frameworks_dir}/Python.framework/Versions/${python_version_dot}/lib/python${python_version_dot}

        xinstall -m 0755 -d ${destroot}${PYTHON_PATH}/site-packages
        xinstall -m 0755 ${filespath}/svn-python.pth \
            ${destroot}${PYTHON_PATH}/site-packages
        reinplace "s|@PREFIX@|${prefix}|g" \
             ${destroot}${PYTHON_PATH}/site-packages/svn-python.pth
        reinplace "s|@PYTHON_BRANCH@|${python_version_dot}|g" \
             ${destroot}${PYTHON_PATH}/site-packages/svn-python.pth

        file rename "${destroot}${prefix}/lib/svn-python" \
            "${destroot}${prefix}/lib/svn-python${python_version_dot}"
    }

    # CarbonCore/MacTypes.h: No such file or directory
    platform puredarwin {
        configure.args-append   --disable-keychain
    }
}
