# -*- 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                fetch-crl
version             3.0.21
categories          security net
platforms           any
supported_archs     noarch
license             Apache-2

maintainers         {petr @petrrr} openmaintainer

description         Download tool for Certificate Revocation Lists

long_description    \
    The fetch-crl utility will retrieve certificate revocation lists (CRLs) \
    for a set of installed trust anchors, based on crl_url files or \
    IGTF-style info files. It will install these for use with OpenSSL, NSS \
    or third-party tools.

homepage            https://wiki.nikhef.nl/grid/FetchCRL3

master_sites        https://dist.eugridpma.info/distribution/util/fetch-crl3/

checksums           rmd160  8ed2e0ed632dfd96e936855141617f82d3cf5f14 \
                    sha256  19a96b95a1c22da9d812014660744c6a31aac597b53ac17128068a77c269cde8 \
                    size    46656

use_configure       no

# The main port installs the tools
subport ${name} {
    patch {
        reinplace "s|/etc|${prefix}/etc|g" README

        reinplace "s|/etc|${prefix}/etc|g" fetch-crl
        reinplace "s|/var|${prefix}/var|g" fetch-crl
        reinplace "s|/etc|${prefix}/etc|g" fetch-crl.8
        reinplace "s|/etc|${prefix}/etc|g" fetch-crl.cnf
        # Note:
        # `fetch-crl.cnf.example` is not patched
    }

    destroot.keepdirs   ${destroot}${prefix}/var/cache/fetch-crl
    destroot.destdir    \
        PREFIX=${destroot}${prefix} \
        ETC=${destroot}${prefix}/etc \
        CACHE=${destroot}${prefix}/var/cache \
        RELEASE=${name}

    pre-deactivate {
        ui_msg "Purging fetch-crl cache ...                              "
        foreach {filename} [ glob ${prefix}/var/cache/fetch-crl/*.state ] {
            delete $filename
        }
    }

    livecheck.type      regex
    livecheck.url       ${master_sites}
    livecheck.regex     ">${name}-(\\d+\\.\\d+\\.\\d+)\\${extract.suffix}<"
}

# The subport provides a launchd item
subport ${name}-launchd {
    revision            0
    # update descriptions
    description         Creates a launchd entry for fetch-crl utility
    long_description    {*}${description}. {*}${long_description}

    platforms           {darwin any}

    depends_run         port:fetch-crl

    distfiles
    worksrcdir      ${subport}

    startupitem.create  no
    startupitem.type    launchd
    startupitem.name    ${name}
    set plist_macports  ${prefix}/etc/${startupitem.location}
    set plist_system    /Library/${startupitem.location}

    extract.mkdir   yes
    extract {
        copy ${filespath}/${startupitem.plist} ${worksrcpath}/
    }

    configure {
        reinplace "s|@@LABEL@@|${startupitem.uniquename}|g" \
            ${worksrcpath}/${startupitem.plist}
        reinplace "s|@@PREFIX@@|${prefix}|g" \
            ${worksrcpath}/${startupitem.plist}
    }

    build {}

    destroot {
        xinstall -d ${destroot}${plist_macports}/${startupitem.uniquename}
        xinstall -d ${destroot}${plist_system}
        xinstall -m 644 ${worksrcpath}/${startupitem.plist} \
            ${destroot}${plist_macports}/${startupitem.uniquename}
        if {${startupitem.install}} {
            ln -s ${plist_macports}/${startupitem.uniquename}/${startupitem.plist} \
                ${destroot}${plist_system}
        } else {
            ln -s ${plist_macports}/${startupitem.uniquename}/${startupitem.plist} \
                ${destroot}${plist_macports}
        }
    }

    livecheck.type  none
}