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

go.setup            github.com/prometheus/node_exporter 1.9.1 v
revision            0

homepage            https://prometheus.io/docs/guides/node-exporter

description         Machine-metric exporter for the Prometheus monitoring \
                    system.

long_description    The Prometheus Node Exporter can expose metrics that \
                    Prometheus can scrape, including a wide variety of \
                    system, hardware- and kernel-related metrics.

categories          sysutils
installs_libs       no
license             Apache-2
maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

checksums           rmd160  e6011fd44071093724c1a14f23a39bfe59e31143 \
                    sha256  ac80b13ced462e88f243ad5e98c12bbcee2628bf552c0d19bb5ae125ce09730d \
                    size    353747

set svc_name        prometheus-node-exporter
set prom_user       prometheus
set ne_doc_dir      ${prefix}/share/doc/${svc_name}
set ne_share_dir    ${prefix}/share/${svc_name}
set ne_log_dir      ${prefix}/var/log/${svc_name}
set ne_log_file     ${ne_log_dir}/${name}.log

add_users           ${prom_user} \
                    group=${prom_user} \
                    realname=Prometheus

depends_build-append \
                    port:promu

go.offline_build no

build.cmd           make
build.target        build

use_parallel_build  no

# promu will fail if it cannot capture the current user using the USER
# environment variable.  So if USER is not currently set in the environment,
# we'll set it here for purposes of the build.
if {! [info exists env(USER)]} {
    build.env-append "USER=${macportsuser}"
}

pre-build {
    # Link promu into where the Makefile expects to find it so that it doesn't
    # try to fetch and install promu again on its own.
    file mkdir ${gopath}/bin
    ln -s ${prefix}/bin/promu ${gopath}/bin/
}

post-extract {
    copy  ${filespath}/org.macports.${name}.plist \
          ${workpath}/org.macports.${name}.plist

    reinplace "s|@NAME@|${name}|g" \
        ${workpath}/org.macports.${name}.plist

    reinplace "s|@USER@|${prom_user}|g" \
        ${workpath}/org.macports.${name}.plist

    reinplace "s|@GROUP@|${prom_user}|g" \
        ${workpath}/org.macports.${name}.plist

    reinplace "s|@PREFIX@|${prefix}|g" \
        ${workpath}/org.macports.${name}.plist

    reinplace "s|@LOGFILE@|${ne_log_file}|g" \
        ${workpath}/org.macports.${name}.plist

}

destroot {
    xinstall -m 0755 ${worksrcpath}/${name} ${destroot}${prefix}/bin/${name}

    xinstall -d -m 0755 ${destroot}${ne_doc_dir}
    xinstall -d -m 0755 ${destroot}${ne_share_dir}
    xinstall -d -m 0755 -o ${prom_user} -g ${prom_user} ${destroot}${ne_log_dir}

    touch ${destroot}${ne_log_file}

    file attributes ${destroot}${ne_log_file} \
        -owner ${prom_user} -group ${prom_user}

    foreach _dir {examples text_collector_examples} {
        copy ${worksrcpath}/${_dir} ${destroot}${ne_share_dir}/
    }

    copy {*}[glob ${worksrcpath}/docs/*] ${destroot}${ne_doc_dir}/

    xinstall -d -m 0755 \
        ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}

    xinstall -m 0644 -o root -W ${workpath} org.macports.${name}.plist \
        ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}

    xinstall -d -m 0755 ${destroot}/Library/LaunchDaemons

    ln -s ${prefix}/etc/LaunchDaemons/org.macports.${name}/org.macports.${name}.plist \
        ${destroot}/Library/LaunchDaemons/org.macports.${name}.plist
}

github.livecheck.regex {([0-9.]+)}

notes "
To enable the Prometheus Node Exporter service, use `port load`, as follows:

\$ sudo port load ${name}

When enabled, by default, the service will be available at http://localhost:9100

...and by default will also log to:

  ${ne_log_file}

"
