# -*- 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
PortGroup               compiler_blacklist_versions 1.0
PortGroup               github 1.0
PortGroup               legacysupport 1.1
PortGroup               openssl 1.0

# Needed for strnlen and MAP_ANONYMOUS
legacysupport.newest_darwin_requires_legacy 14

name                    opensc
github.setup            OpenSC OpenSC 0.26.1
github.tarball_from     releases
revision                1

checksums               rmd160  fe5fd963c544bc013c99b32c6bad9b768603e6bd \
                        sha256  f16291a031d86e570394762e9f35eaf2fcbc2337a49910f3feae42d54e1688cb \
                        size    2416935

categories              security
license                 LGPL-2.1
maintainers             nomaintainer

description             Tools and libraries for smart cards
long_description        OpenSC provides a set of libraries and utilities to \
                        work with smart cards. Its main focus is on cards that \
                        support cryptographic operations, and facilitate their \
                        use in security applications such as authentication, \
                        mail encryption and digital signatures. \
                        OpenSC implements the PKCS#11 API so applications \
                        supporting this API (such as Mozilla Firefox and \
                        Thunderbird) can use it. \
                        On the card OpenSC implements the PKCS#15 standard and \
                        aims to be compatible with every software/card that \
                        does so, too.

distname                opensc-${version}

patch.pre_args-replace  -p0 -p1
patchfiles-append       patch-winscard.diff
#patchfiles-append       patch-pgp-and-tools.diff

# https://trac.macports.org/ticket/65709
compiler.blacklist-append *gcc-4.* {clang < 421} *clang-3.*

compiler.c_standard     2011

# Needed to enable C extensions, specifically memset_s
configure.cflags-append \
                        -D__STDC_WANT_LIB_EXT1__=1

configure.checks.implicit_function_declaration.whitelist-append \
                        strchr

depends_build-append \
                        port:docbook-xsl-nons \
                        port:gengetopt \
                        port:help2man \
                        port:libxslt \
                        port:pkgconfig

depends_lib-append \
                        port:openpace \
                        port:zlib

use_autoreconf          yes
configure.args-append \
                        --disable-openpace \
                        --disable-readline \
                        --disable-silent-rules \
                        --disable-static \
                        --disable-strict \
                        --enable-doc \
                        --enable-pcsc \
                        --enable-sm \
                        --with-xsl-stylesheetsdir=${prefix}/share/xsl/docbook-xsl-nons

pre-configure {
    reinplace -W ${worksrcpath} \
        "s|No Git revision info available|OpenSC-${version} (MacPorts)|g" \
        configure.ac

    set pkcs11_provider     opensc-pkcs11.so
    if {[variant_isset p11kit]} {
        set pkcs11_provider p11-kit-proxy.dylib
    }
    configure.args-append   --with-pkcs11-provider=${prefix}/lib/${pkcs11_provider}
}

post-destroot {
    system "${worksrcpath}/MacOSX/libtool-bundle ${destroot}${prefix}/lib/opensc-pkcs11.so ${destroot}${prefix}/lib"

    # Install module config so opensc will be auto-discovered by p11-kit.
    set p11_module_configs ${prefix}/share/p11-kit/modules
    xinstall -d ${destroot}${p11_module_configs}
    xinstall -m 644 ${filespath}/p11kit-opensc.module ${destroot}${p11_module_configs}/opensc.module
}

variant eac description {Enable Extended Access Control (EAC) v2 support} {
    depends_lib-append      port:openpace
    configure.args-replace  --disable-openpace --enable-openpace
}

variant p11kit description {Use p11-kit as default PKCS#11 module} {
    depends_run             port:p11-kit
}

variant readline description {Enable readline support} {
    depends_lib-append      port:readline
    configure.args-replace  --disable-readline --enable-readline
}

default_variants        +readline

# Skip -rc and other pre-release tags
github.livecheck.regex  {([\d.]+)}