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

# Qt version handling logic
global Qt_Major
subport ${name}-qt5     {}

if {[string match "${name}-qt5*" ${subport}]} {
    PortGroup           qt5 1.0
    set Qt_Major        qt5
    version             2.3.4
    checksums           rmd160  88589c5b4f2a87cb048b5a90ff39256ec996fdb1 \
                        sha256  6b695881a7e3fd95f73aaee6eaeab96f6ad17e515e9c2b3d4b3272d7862ff5c4 \
                        size    737072
} else {
    PortGroup           qt4 1.0
    set Qt_Major        qt4
    # last release with Qt4 support:
    version             2.2.1
    # set the epoch because we've had to downgrade
    epoch               202111
    checksums           rmd160  dddc3cf240dc5424b9df13fc1bf41c8e04f3b814 \
                        sha256  d716d2d8e3ed8d95bbdb061f03081d7d032206f746a30a4d29d72196f50e7b02 \
                        size    691676
}
# best included after the required Qt PG
PortGroup               cmake 1.1

categories              devel crypto security
maintainers             {gmail.com:rjvbertin @RJVB} openmaintainer
license                 LGPL-2.1
description             Qt Cryptographic Architecture
long_description        Qt Cryptographic Architecture provides an easy API for the following \
                        features: SSL/TLS, X509, SASL, RSA, Hashing (SHA1, MD5), Ciphers \
                        (BlowFish, 3DES, AES).  Functionality is supplied via plugins.

platforms               darwin

homepage                https://userbase.kde.org/QCA
master_sites            kde:stable/qca/${version}
use_xz                  yes

patch.pre_args-append   -N
patch.pre_args-replace  -p0 -p1

switch ${Qt_Major} {
    qt4 {
        patchfiles-append \
                        patch-qca221-ossl.diff \
                        qt4/patch-installname.diff \
                        qt4/patch-cmakeminver.diff
        if {${os.platform} eq "darwin" && ${os.major} < 11} {
            patchfiles-append \
                        patch-unbreak-qca_systemstore.diff
        }
        configure.args-append \
                        -DQT4_BUILD:BOOL=ON
    }
    qt5 {
        if {${os.platform} eq "darwin" && ${os.major} < 10 } {
            ui_warn "This port currently requires Qt 5.3 or up, so OS X 10.6 or newer"
            known_fail yes
        }
        patchfiles-append \
                        patch-qca-ossl.diff \
                        patch-missing-headers.diff \
                        qt5/patch-qca210-qt550.diff \
                        qt5/patch-support-older-qt5.diff
        configure.args-append \
                        -DQCA_SUFFIX:STRING="qt5"
        build.post_args-append -wk
    }
    default {
        ui_error "unsupported Qt version \"${Qt_Major}\""
        return -code error "Unsupported Qt version"
    }
}

# the QCA plugins have been rolled into the main source tarball and are built by default
# when their dependencies are available. Allow this for all plugins that were not already
# available through an individual port; no reason to impose new dependencies on our dependents.
# This is mostly of interest for Qt5-based dependents, evidently.
configure.args-append   -DCMAKE_INSTALL_PREFIX:PATH=${qt_dir} \
                        -DQCA_PLUGINS_INSTALL_DIR:PATH=${qt_plugins_dir} \
                        -DQCA_LIBRARY_INSTALL_DIR:PATH=${qt_libs_dir} \
                        -DQCA_BINARY_INSTALL_DIR:PATH=${qt_bins_dir} \
                        -DQCA_FEATURE_INSTALL_DIR:PATH=${qt_mkspecs_dir}/features \
                        -DQCA_INCLUDE_INSTALL_DIR:PATH=${qt_includes_dir} \
                        -DQCA_PRIVATE_INCLUDE_INSTALL_DIR:PATH=${qt_includes_dir} \
                        -DQCA_DOC_INSTALL_DIR:PATH=${qt_docs_dir} \
                        -DQCA_MAN_INSTALL_DIR:PATH=${prefix}/share/man \
                        -DPKGCONFIG_INSTALL_PREFIX:PATH=${qt_pkg_config_dir} \
                        -DOSX_FRAMEWORK:BOOL=OFF

# botan requires C++11
compiler.cxx_standard   2011
configure.cxxflags-append \
                        -std=c++11

post-destroot {
    if {${subport} eq ${name} || ${subport} eq "${subport}-qt5"} {
        # install docs
        xinstall -d -m 755 ${destroot}${prefix}/share/doc/${subport}
        xinstall -m 644 -W ${worksrcpath} COPYING README TODO \
            ${destroot}${prefix}/share/doc/${subport}
    }

    if {${subport} eq "${name}"} {
        # install cmake file (taken from port 'kdelibs4'). To use this
        # file, include '-DCMAKE_MODULE_PATH=...' in configure.args during
        # the initial 'configure' stage (which uses CMake), to the full
        # path of where the CMake files are installed.
        xinstall -d -m 755 ${destroot}${qt_cmake_module_dir}
        xinstall -m 644 -W ${filespath} FindQCA2.cmake \
            ${destroot}${qt_cmake_module_dir}

    }
}

# avoid installing the examples into Qt's binary tree
variant examples description {Include examples in install} {
    post-destroot {
        xinstall -d -m 755 ${destroot}${prefix}/share/${subport}/examples/
        eval file copy [glob ${worksrcpath}/examples/*] \
            ${destroot}${prefix}/share/${subport}/examples/
    }
}

### Plugin subports:
### The ossl, cyrus-sasl and gnupg plugins used to be standalone ports
### are now implemented as subports. The other plugins are built together with
### the main ports.
###
# Qt4 receives no suffix:
set qt.versions     {"" "-qt5"}
foreach qv ${qt.versions} {
    # the main ports
    if {${subport} eq "${name}${qv}"} {
        # revbump for botan2
        revision                2
        depends_lib-append      port:botan \
                                port:libgcrypt \
                                port:nss
        configure.args-append   -DBUILD_PLUGINS:STRING="botan\;gcrypt\;logger\;nss\;softstore"
    }

    # plugins depend on the corresponding main port and the libraries of which they expose the Functionality
    subport ${name}${qv}-ossl {
        PortGroup               openssl 1.0
        license                 LGPL-2.1+
        depends_lib             port:${name}${qv}
        if {${Qt_Major} eq "qt4"} {
            openssl.branch      1.1
        }
        configure.args-append   -DBUILD_PLUGINS:STRING="ossl"
        build.dir               ${workpath}/build/plugins/qca-ossl
    }
    subport ${name}${qv}-cyrus-sasl {
        license                 LGPL-2.1+
        depends_lib             port:${name}${qv} port:cyrus-sasl2
        configure.args-append   -DBUILD_PLUGINS:STRING="cyrus-sasl"
        build.dir               ${workpath}/build/plugins/qca-cyrus-sasl
    }
    subport ${name}${qv}-gnupg {
        license                 LGPL-2.1+
        depends_lib             port:${name}${qv}
        configure.args-append   -DBUILD_PLUGINS:STRING="gnupg"
        build.dir               ${workpath}/build/plugins/qca-gnupg
    }
    subport ${name}${qv}-pkcs11 {
        PortGroup               openssl 1.0
        license                 LGPL-2.1+
        depends_lib             port:${name}${qv} port:nss port:pkcs11-helper
        if {${Qt_Major} eq "qt4"} {
            # qca-pkcs11 will build (and link) against OpenSSL3 but its dependencies don't
            # so it seems wise to avoid mixing OpenSSL versions.
            openssl.branch      1.1
        }
        configure.args-append   -DBUILD_PLUGINS:STRING="pkcs11"
        build.dir               ${workpath}/build/plugins/qca-pkcs11
    }
}

livecheck.type  regex
livecheck.url   https://download.kde.org/stable/qca
livecheck.regex "(2\\.\\d+(?:\\.\\d+)+(?:\\.\\d+)*)\\/"
