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

# use the qt4 group; set 'building_qt4' so that the portgroup
# does not include certain parts
set building_qt4    1
PortGroup           qmake 1.0

name                qt4-mac-postgresql16-plugin
version             4.8.7
revision            1
set branch          [join [lrange [split ${version} .] 0 1] .]
categories          aqua databases
platforms           macosx
maintainers         {michaelld @michaelld} openmaintainer
license             {LGPL-2.1 GPL-3}
homepage            http://www.qt.io/

master_sites        http://download.qt.io/official_releases/qt/${branch}/${version}/

distname            qt-everywhere-opensource-src-${version}

checksums           rmd160 afb5e5a99388e6429faca59cb5000054feffd166 \
                    sha256 e2882295097e47fe089f8ac741a95fef47e0a73a3f3cdf21b56990638f626ea0

dist_subdir         qt4-mac

# when using 'building_qt4' and qmake, have to include dependency on qt4-mac
depends_lib-append  port:qt4-mac

qt4.debug_variant   no

set DB              psql
set DRIVER          src/sql/drivers/${DB}
set PLUGIN          src/plugins/sqldrivers/${DB}

extract.post_args   | tar -xf - \
                    ${distname}/${DRIVER} \
                    ${distname}/${PLUGIN} \
                    ${distname}/src/plugins/sqldrivers/qsqldriverbase.pri \
                    ${distname}/src/plugins/qpluginbase.pri \
                    ${distname}/src/qt_targets.pri

configure.dir       ${worksrcpath}/${PLUGIN}
configure.env-append \
                    QMAKESPEC=${qt_mkspecs_dir}/macx-g++

set mp.ports        {postgresql15 postgresql16}
foreach mp.port ${mp.ports} {

    lappend mp.names "qt4-mac-${mp.port}-plugin"
}
foreach mp.name ${mp.names} {

    set idx             [lsearch ${mp.names} ${mp.name}]
    set mp.port     [lindex ${mp.ports} $idx]
    set mp.conflicts  [lreplace ${mp.names} $idx $idx]

    if {[string compare ${mp.name} ${name}] == 0 && [string compare ${mp.name} ${subport}] == 0} {

        description         Qt PostgreSQL database plugin for ${mp.port}
        long_description    ${description}
        conflicts-append    {*}${mp.conflicts}
        depends_lib-append  port:${mp.port}
        configure.args-append INCLUDEPATH+="${prefix}/include ${prefix}/include/${mp.port} ${prefix}/include/${mp.port}/server ${prefix}/include/Qt" \
                            LIBS+="-L${prefix}/lib -L${prefix}/lib/${mp.port} -lpq"
        configure.post_args psql.pro

        variant debug description {Build both release and debug plugin} {}

        pre-configure {
            configure.args-append CONFIG+=\"${qt_arch_types}\"
        }

        post-patch {
            if {![variant_isset debug]} {
                reinplace "s@debug_and_release@release@" \
                    ${worksrcpath}/src/plugins/qpluginbase.pri
            }
        }

    } else {

        subport ${mp.name} {
        
            description         Qt PostgreSQL database plugin for ${mp.port}
            long_description    ${description}
            conflicts-append    {*}${mp.conflicts}
            depends_lib-append  port:${mp.port}
            configure.args-append INCLUDEPATH+="${prefix}/include ${prefix}/include/${mp.port} ${prefix}/include/${mp.port}/server ${prefix}/include/Qt" \
                                LIBS+="-L${prefix}/lib -L${prefix}/lib/${mp.port} -lpq"
            configure.post_args psql.pro

            variant debug description \
                {Build both release and debug plugin} {}

            pre-configure {
                configure.args-append CONFIG+=\"${qt_arch_types}\"
            }

            post-patch {
                if {![variant_isset debug]} {
                    reinplace "s@debug_and_release@release@" \
                        ${worksrcpath}/src/plugins/qpluginbase.pri
                }
            }
        }
    }
}

build.dir           ${worksrcpath}/${PLUGIN}
build.target        {}

livecheck.type      regex
livecheck.url       http://download.qt.io/official_releases/qt/${branch}/
livecheck.regex     >(\[0-9.\]+)/<