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

# restrict to latest supported version for Qt4 and Python 2.7 and 3.5
if {[string first "qt4" ${subport}] != -1 || [string first "py" ${subport}] != -1} {
    PortGroup        deprecated 1.0

    deprecated.upstream_support no

    version         2.11.6
    checksums       rmd160  bc00b95128956424a2f3466d25c4dd9f03169c0b \
                    sha256  e7346057db47d2fb384467fafccfcb13aa0741373c5d593bc72b55b2f0dd20a7 \
                    size    3027764
    revision        1
    distname        QScintilla-${version}
} else {
    version         2.13.3
    checksums       rmd160  93dfab93f42efc3434531e77669bd97b0f8f4cde \
                    sha256  711d28e37c8fccaa8229e8e39a5b3b2d97f3fffc63da10b71c71b84fa3649398 \
                    size    3060031
    revision        0
    distname        QScintilla_src-${version}
}

categories          devel
license             GPL-3
maintainers         {michaelld @michaelld} {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
description         QScintilla is a port to Qt of Neil Hodgson's Scintilla C++ editor control.
homepage            https://www.riverbankcomputing.com/software/qscintilla/

long_description    QScintilla is a port to Qt of Neil Hodgson's Scintilla C++ editor control. \
                    As well as features found in standard text editing components,      \
                    QScintilla includes features especially useful when editing and     \
                    debugging source code. These include support for syntax styling,    \
                    error indicators, code completion and call tips. The selection      \
                    margin can contain markers like those used in debuggers to indicate \
                    breakpoints and the current line. Styling choices are more open     \
                    than with many editors, allowing the use of proportional fonts,     \
                    bold and italics, multiple foreground and background colours and    \
                    multiple fonts.

master_sites        https://www.riverbankcomputing.com/static/Downloads/QScintilla/${version}

compiler.cxx_standard \
                    2011

foreach qt_major {4 5} {
    subport ${name}-qt${qt_major} {
        if {${qt_major} eq 4} {
            PortGroup qmake 1.0

            worksrcdir ${worksrcdir}/Qt4Qt5

            # temporary path to fix necessary inclusion of QUrl
            patchfiles-append patch-Qt4Qt5_qsciscintillabase.cpp.diff

            # fixup @rpath in the library's install_name
            patchfiles-append patch-Qt4Qt5-qscintilla.pro.diff
        } else {
            PortGroup qmake${qt_major} 1.0

            worksrcdir ${worksrcdir}/src

            # fixup @rpath in the library's install_name
            patchfiles-append patch-src-qscintilla.pro.diff

            # https://trac.macports.org/ticket/65293
            if { ${os.platform} eq "darwin" && (( ${os.major} >= 15 && ${os.major} <= 16 ) || ${os.major} >= 20 ) } {
                use_xcode   yes
            }

            qt5.min_version 5.6
        }

        if {${qt_major} eq 4} {
            # enable C++11
            configure.cxxflags-append -std=c++11
            # respect configure.cxx_stdlib when linking
            if {[string match *clang* ${configure.cxx}] && ${configure.cxx_stdlib} ne ""} {
                configure.ldflags-append -stdlib=${configure.cxx_stdlib}
            }
        } else {
            qt${qt_major}.depends_component qtmacextras
            configure.args-append CONFIG+=absolute_library_soname
            configure.args-append CONFIG+=c++11
        }

        # work around build issue with qt4 version and QtAccessibility
        if {${qt_major} eq 4} {
            configure.args-append DEFINES+=QT_NO_ACCESSIBILITY
        }
    }

    subport ${name}-designer-qt${qt_major} {
        if {${qt_major} eq 4} {
            PortGroup qmake 1.0

            worksrcdir ${worksrcdir}/designer-Qt4Qt5
        } else {
            PortGroup qmake${qt_major} 1.0

            worksrcdir ${worksrcdir}/designer

            # https://trac.macports.org/ticket/65293
            if { ${os.platform} eq "darwin" && (( ${os.major} >= 15 && ${os.major} <= 16 ) || ${os.major} >= 20 ) } {
                use_xcode   yes
            }

            qt5.min_version 5.6
        }

        description       Qt Designer plugin for Qt${qt_major} QScintilla
        long_description  {*}${description}

        patchfiles-append   patch-add_debug.diff

        if {${qt_major} eq 4} {
            if {[variant_isset debug]} {
                configure.args-append CONFIG+="debug_and_release build_all"
            } else {
                configure.args-append CONFIG+=release
            }
        } else {
            qt${qt_major}.depends_component qttools
            if {[variant_isset debug]} {
                require_active_variants ${name}-qt${qt_major} debug
            }
        }

        depends_lib-append        \
            port:${name}-qt${qt_major}
    }

    subport py-${name}-qt${qt_major} {
        PortGroup stub 1.0

        supported_archs   noarch
        platforms         any
        categories-append python
        depends_lib port:py27-qscintilla-qt${qt_major}
    }

    set python_versions {27 35 36 37 38 39 310}

    # Python bindings for the latest upstream Qt5 version are in py-pyqt5-scintilla
    if {${qt_major} eq 5} {
        set python_versions {27}
    }

    foreach pver ${python_versions} {
        subport py${pver}-${name}-qt${qt_major} {
            if {${qt_major} eq 4} {
                PortGroup qt4 1.0

            } else {
                PortGroup qmake${qt_major} 1.0

                # the Python bindings below require the latest qscintilla-qt5 version
                conflicts py38-pyqt5-scintilla py39-pyqt5-scintilla

                # https://trac.macports.org/ticket/65293
                if { ${os.platform} eq "darwin" && (( ${os.major} >= 15 && ${os.major} <= 16 ) || ${os.major} >= 20 ) } {
                    use_xcode   yes
                }

                qt5.min_version 5.6
            }
            categories-append   python
            description         Python bindings for Qt${qt_major} QScintilla
            long_description    Python bindings for Qt${qt_major} QScintilla, \
                a port to Qt of Neil Hodgson's Scintilla C++ editor control.

            worksrcdir ${worksrcdir}/Python

            # allow configure.py to respect --spec=... argument
            patchfiles-append patch-configure.py.diff

            depends_lib-append                  \
                port:qscintilla-qt${qt_major}   \
                port:python${pver}              \
                port:py${pver}-sip4              \
                port:py${pver}-pyqt${qt_major}

            if {${qt_major} eq 4} {
                pre-configure {
                    set cache [open "${configure.dir}/.qmake.cache" w 0644]
                    puts ${cache} "CONFIG += ${qt_arch_types}"
                    close ${cache}
                }
            }

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

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

            # --prefix is not recognized
            configure.pre_args

            configure.cmd       ${configure.python} configure.py
            configure.args-append --verbose                \
                --pyqt=PyQt${qt_major}                     \
                --sip=${prefix}/bin/sip-${python_ver_dot}  \
                --qmake=${qt_qmake_cmd}                    \
                --no-dist-info

            if {${qt_major} eq 4} {
                configure.args-append \
                    --spec=${qt_qmake_spec}
            } else {
                qt5.spec_cmd --spec=
            }

            # --disable-dependency-tracking is not recognized
            configure.universal_args-delete --disable-dependency-tracking

            post-destroot {
                move \
                    ${destroot}${qt_data_dir}/qsci/api/python/QScintilla2.api \
                    ${destroot}${qt_data_dir}/qsci/api/python/QScintilla2-Python${python_ver_dot}.api
            }
        }
    }
}

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

    revision    2
    supported_archs noarch
    platforms       any

    variant qt4 description "build Qt4 version of ${name}" {
        depends_lib-append \
            port:qscintilla-qt4 \
            port:qscintilla-designer-qt4
    }

    variant qt5 description "build Qt5 version of ${name}" {
        depends_lib-append \
            port:qscintilla-qt5 \
            port:qscintilla-designer-qt5
    }

    default_variants +qt4
}

livecheck.type      regex
livecheck.url       ${homepage}download
livecheck.regex     QScintilla/\(\[0-9.\]+\)/ChangeLog
