# -*- 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/alertmanager 0.27.0 v
revision            0

homepage            https://prometheus.io/docs/alerting/alertmanager

description         The Alertmanager handles alerts sent by client \
                    applications such as the Prometheus server.

long_description    AlertManager takes care of deduplicating, grouping, and \
                    routing them to the correct receiver integration such as \
                    email, PagerDuty, or OpsGenie. It also takes care of \
                    silencing and inhibition of alerts.

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

checksums           rmd160  4d7f79ff7f187c070e0daaf0d94f84b3c7566d63 \
                    sha256  c21ede46b0471adb4d7bd75f09e57d44d969801717a6e0ef36ee37e4cdf25dd8 \
                    size    2868365

depends_build-append \
                    port:promu

# Allow Go to fetch dependencies at build time
go.offline_build no

build.cmd           make
build.target        build

use_parallel_build  no

set svc_name        prometheus-alertmanager
set prom_user       prometheus
set am_config_dir   ${prefix}/etc/${svc_name}
set am_config_file  ${am_config_dir}/config.yml
set am_data_dir     ${prefix}/var/db/${svc_name}
set am_share_dir    ${prefix}/share/${svc_name}
set am_log_dir      ${prefix}/var/log/${svc_name}
set am_log_file     ${am_log_dir}/${name}.log

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

# 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
}

patch {
    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@|${am_log_file}|g" \
        ${workpath}/org.macports.${name}.plist

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

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

destroot.keepdirs   ${destroot}${am_config_dir} \
                    ${destroot}${am_data_dir}

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

    xinstall -d -m 0755 ${destroot}${am_config_dir}
    xinstall -d -m 0755 ${destroot}${am_share_dir}
    xinstall -d -m 0755 -o ${prom_user} -g ${prom_user} ${destroot}${am_log_dir}
    xinstall -d -m 0755 -o ${prom_user} -g ${prom_user} ${destroot}${am_data_dir}

    touch ${destroot}${am_log_file}

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

    copy ${worksrcpath}/doc ${destroot}${am_share_dir}/
    copy ${worksrcpath}/examples ${destroot}${am_share_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
}

post-activate {
    if {![file exists ${am_config_file}]} {
        copy  ${am_share_dir}/doc/examples/simple.yml ${am_config_file}
    }
}

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

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

\$ sudo port load ${name}

When enabled, the service will:

  - listen by default on http://localhost:9093

  - log to: ${am_log_file}

Configuration for AlertManager can be found at:

  ${am_config_file}

"
