# -*- 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/influxdata/telegraf 1.31.3 v
revision                    0

homepage                    https://docs.influxdata.com/telegraf

description                 \
    The plugin-driven server agent for collecting & reporting metrics.

long_description            \
    Telegraf is an agent for collecting, processing, aggregating, and writing \
    metrics.  Design goals are to have a minimal memory footprint with a \
    plugin system so that developers in the community can easily add support \
    for collecting metrics. Telegraf is plugin-driven and has the concept of \
    4 distinct plugin types: input plugins, processor plugins, aggregator \
    plugins, and output plugins.

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

checksums                   rmd160  940b30da3c2fd1d2c3700951a6f3c73973458006 \
                            sha256  2c96ae79b136d6f1dfe2571f262192a68c12aa20796061ce71120a6253e6a524 \
                            size    4583800

set telegraf_user           ${name}
set telegraf_conf_dir       ${prefix}/etc/${name}
set telegraf_data_dir       ${prefix}/var/db/${name}
set telegraf_lib_dir        ${prefix}/lib/${name}
set telegraf_log_dir        ${prefix}/var/log/${name}
set telegraf_run_dir        ${prefix}/var/run/${name}

set telegraf_conf_file      ${telegraf_conf_dir}/telegraf.conf
set telegraf_log_file       ${telegraf_log_dir}/telegraf.log
set telegraf_pid_file       ${telegraf_run_dir}/telegraf.pid
set telegraf_plist_src      ${workpath}/org.macports.${name}.plist

installs_libs               no
use_configure               no

add_users                   ${telegraf_user} \
                            group=${telegraf_user} \
                            realname=Telegraf

# Allow fetching dependencies at build time for now.
# See: https://trac.macports.org/ticket/61192
go.offline_build no

build.cmd                   make
build.pre_args              tag=${version}
build.args                  all

use_parallel_build          no

post-extract {

    reinplace "s|/etc/telegraf/telegraf.conf|${telegraf_conf_file}|g" \
              ${worksrcpath}/config/config.go

    copy ${filespath}/org.macports.${name}.plist      ${telegraf_plist_src}
    reinplace "s|@NAME@|${name}|g"                    ${telegraf_plist_src}
    reinplace "s|@USER@|${name}|g"                    ${telegraf_plist_src}
    reinplace "s|@GROUP@|${name}|g"                   ${telegraf_plist_src}
    reinplace "s|@PREFIX@|${prefix}|g"                ${telegraf_plist_src}
    reinplace "s|@CONF_DIR@|${telegraf_conf_dir}|g"   ${telegraf_plist_src}
    reinplace "s|@PID_FILE@|${telegraf_pid_file}|g"   ${telegraf_plist_src}
    reinplace "s|@LOGFILE@|${telegraf_log_file}|g"    ${telegraf_plist_src}
}

destroot {

    copy ${worksrcpath}/${name} ${destroot}${prefix}/bin/

    xinstall  -d -m 0755 ${telegraf_conf_dir}
    xinstall  -d -m 0755 -g ${telegraf_user} ${telegraf_log_dir}

    xinstall  -d -m 0755 -o ${telegraf_user} -g ${telegraf_user} \
              ${telegraf_data_dir}

    xinstall  -d -m 0755 -o ${telegraf_user} -g ${telegraf_user} \
              ${telegraf_lib_dir}

    xinstall  -d -m 0755 -o ${telegraf_user} -g ${telegraf_user} \
              ${telegraf_run_dir}

    touch ${telegraf_log_file}
    file  attributes ${telegraf_log_file} -owner ${telegraf_user} \
          -group ${telegraf_user}

    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
}

destroot.keepdirs-append  ${destroot}${telegraf_conf_dir} \
                          ${destroot}${telegraf_data_dir} \
                          ${destroot}${telegraf_lib_dir} \
                          ${destroot}${telegraf_run_dir}

post-activate {

    if {![file exists ${telegraf_conf_file}]} {
        system "telegraf config > ${telegraf_conf_file}"
    }
}

notes "
To start the Telegraf service, use `port load`: \$ sudo port load ${name}

Once running, the service will log to: ${telegraf_log_file}

Stop and remove the service with: \$ sudo port unload ${name}
"

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