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

PortSystem          1.0
PortGroup           cmake 1.0
PortGroup           active_variants 1.1

name                qca
version             2.1.0
revision            3
set branch          [join [lrange [split ${version} .] 0 1] .]
categories          devel crypto security
maintainers         nomaintainer
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            http://delta.affinix.com/qca/
master_sites        http://delta.affinix.com/download/qca/2.0/

checksums           rmd160 30358bbd182cfeb9eaa696b5b39fa9e6d1f99b4c \
                    sha256 226dcd76138c3738cdc15863607a96b3758a4c3efd3c47295939bcea4e7a9284

# do VPATH (out of source tree) build

cmake.out_of_source yes

if {${name} eq ${subport}} {

    # temporary patch to add including QIODevice to qca_basic.h

    patchfiles-append patch-include_QtCrypto_qca_basic.h.diff

    # fix install directories for certs and cmake files

    patchfiles-append patch-CMakeLists.txt.diff

    # remove unnecessary Carbon #include, which fails on 10.8

    patchfiles-append patch-src_qac_systemstore_mac.cpp.diff

} else {
    # fix top-level CMakeLists.txt to build just plugins
    patchfiles-append patch-CMakeLists.txt_plugins.diff
}

# Qt variants

variant qt4 conflicts qt5 description "Build with qt4" {}

variant qt5 conflicts qt4 description "Build with qt5" {}

if {![variant_isset qt4] && ![variant_isset qt5]} {
    default_variants +qt4
}

# make sure -qt4 wasn't specified alone

if {![variant_isset qt4] && ![variant_isset qt5]} {
    ui_error "\n\nYou must select either the +qt4 or +qt5 variant to build ${subport}\n"
    return -code error "Invalid variant selection"
}

if {${name} eq ${subport}} {
    if {[variant_isset qt4]} {
        PortGroup qt4 1.0
        configure.args-append -DQT4_BUILD=ON
    } else {
        PortGroup qt5 1.0
    }
} else {
    # require qca to also have the selected Qt variant
    if {[variant_isset qt4]} {
        PortGroup qt4 1.0
        require_active_variants port:qca qt4
        configure.args-append -DQT4_BUILD=ON
    } else {
        PortGroup qt5 1.0
        require_active_variants port:qca qt5
    }
}

variant examples description {Include examples in install} {}

# set install directory for mkspec files

configure.args-append \
    -DQCA_FEATURE_INSTALL_DIR=${qt_mkspecs_dir}

# specify various configure options

configure.args-append \
    -DQCA_INSTALL_IN_QT_PREFIX=NO \
    -DQCA_PLUGINS_INSTALL_DIR=${prefix}/share/${name}/plugins

if {${name} eq ${subport}} {

    # build as a framework

    configure.args-append \
        -DOSX_FRAMEWORK=YES

    # specify various configure options

    configure.args-append \
        -DBUILD_PLUGINS=NO \
        -DQCA_MAN_INSTALL_DIR=${prefix}/share/man \
        -DQCA_LIBRARY_INSTALL_DIR=${frameworks_dir} \
        -DCMAKE_MODULES_DIR=${prefix}/share/cmake \
        -DPKGCONFIG_INSTALL_PREFIX=${prefix}/lib/pkgconfig \
        -DQCA_DOC_INSTALL_DIR=${prefix}/share/${name}/doc

}

# allow ccache, if specified by the user

pre-build {
    if {[tbool configure.ccache]} {
        build.post_args "CCACHE=ccache"
    }
}

post-destroot {

    if {${name} eq ${subport}} {

        # install docs

        xinstall -d -m 755 ${destroot}${prefix}/share/doc/${name}
        xinstall -m 644 -W ${worksrcpath} COPYING README TODO \
            ${destroot}${prefix}/share/doc/${name}

        # get qca's version numbers

        set qca_vers [split ${version} "."]
        set qca_major [lindex ${qca_vers} 0]
        set qca_minor [lindex ${qca_vers} 1]
        set qca_patch [lindex ${qca_vers} 2]

        # link includes and libraries for each actual installed framework

        set tf_full ${frameworks_dir}/qca.framework/Versions/${version}

        # link headers into ${prefix}/include, removing
        # directories if they are already there first

        set dr_inc_file ${destroot}${prefix}/include
        ln -s ${tf_full}/Headers ${dr_inc_file}/qca
        ln -s ${tf_full}/Headers ${dr_inc_file}/QtCrypto

        # link libraries into ${dr_libs_dir}, all 4 number variants

        set dr_libs_dir ${destroot}${prefix}/lib
        ln -s ${tf_full}/qca ${dr_libs_dir}/libqca.dylib
        ln -s ${tf_full}/qca ${dr_libs_dir}/libqca.${qca_major}.dylib
        ln -s ${tf_full}/qca ${dr_libs_dir}/libqca.${qca_major}.${qca_minor}.dylib
        ln -s ${tf_full}/qca ${dr_libs_dir}/libqca.${qca_major}.${qca_minor}.${qca_patch}.dylib

        if {[variant_isset examples]} {
            xinstall -d -m 755 ${destroot}${prefix}/share/examples/${name}
            eval file copy [glob ${worksrcpath}/examples/*] \
                ${destroot}${prefix}/share/examples/${name}
        }
    }
}

array set bindings {
    botan       {port:botan       botan}
    cyrus-sasl  {port:cyrus-sasl2 cyrus-sasl}
    gcrypt      {port:libgcrypt   gcrypt}
    gnupg       {port:gnupg       gnupg}
    logger      {""               logger}
    nss         {port:nss         nss}
    ossl        {path:lib/libssl.dylib:openssl ossl}
    softstore   {""               softstore}
}
#    pkcs11      {port:pkcs11      pkcs11}
array set prettynames {
    botan Botan cyrus-sasl Cyrus-SASL gcrypt GCrypt
    gnupg GnuPG logger Logger nss NSS
    ossl OpenSSL pkcs11 pkcs11 softstore SoftStore
}

options qca.plugin
default qca.plugin ""

foreach plugin [lsort [array names bindings]] {
    set arg      [lindex $bindings($plugin) 1]
    set arg_name [lindex [split ${arg} =] 0]
    subport qca-${plugin} {
        if {[lindex $bindings($plugin) 0] ne ""} {
            depends_lib-append [lindex $bindings($plugin) 0]
        }
        configure.args-append  -DWITH_${arg}_PLUGIN=ON
        qca.plugin             ${arg_name}
    }
    if {${qca.plugin} != ${arg_name}} {
        configure.args-append -DWITH_${arg_name}_PLUGIN=OFF
    }
}
if {${qca.plugin} ne ""} {
    description $prettynames(${qca.plugin}) plugin for ${name}
    long_description \
        Qt Cryptographic Architecture. This is the \
        $prettynames(${qca.plugin}) plugin.
    depends_lib-append port:qca
    livecheck.type none
} else {
    livecheck.type  regex
    livecheck.url   ${homepage}
    livecheck.regex "${name}-(\\d+(?:\\.\\d+)*)${extract.suffix}"
}

subport qca-tls {
    PortGroup obsolete 1.0
    long_description "This port is obsolete."
}