# -*- 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/prometheus 3.0.1 v
go.offline_build    no
revision            0

homepage            https://prometheus.io

description         \
    The Prometheus monitoring system and time series database

long_description    \
    Prometheus is an open-source systems monitoring and alerting toolkit \
    originally built at SoundCloud. Since its inception in 2012, many \
    companies and organizations have adopted Prometheus, and the project has \
    a very active developer and user community.

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

patchfiles-append \
                    patch-build-use-gnutar.diff

depends_build-append \
                    port:golangci-lint \
                    path:bin/npm:npm10 \
                    port:promu \
                    port:yarn \
                    port:gnutar

platform darwin {
    if {${os.major} < 13} {
        depends_build-replace \
                    path:bin/node:nodejs16 \
                    path:bin/node:nodejs14
    }
}

build.cmd           make
build.pre_args-append \
                    GOLANGCI_LINT=${prefix}/bin/golangci-lint \
                    PROMU=${prefix}/bin/promu
build.args          build

use_parallel_build  no
use_xcode           yes

set prom_user       ${name}
set prom_conf_dir   ${prefix}/etc/${name}
set prom_conf_file  ${prom_conf_dir}/prometheus.yml
set prom_data_dir   ${prefix}/var/db/${name}
set prom_doc_dir    ${prefix}/share/doc/${name}
set prom_share_dir  ${prefix}/share/${name}
set prom_log_dir    ${prefix}/var/log/${name}
set prom_log_file   ${prom_log_dir}/${name}.log

checksums           rmd160  2a8027b9a3db1aa51b311377a2e271e2c2390bd5 \
                    sha256  bd40fd54d5a8f63cbbf5355ee056d23efce29c4af3fbf3fd754238c5d2a27425 \
                    size    4959310

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

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

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

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

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

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

    reinplace "s|@CONF_FILE@|${prom_conf_file}|g" \
        ${workpath}/org.macports.prometheus.plist

    reinplace "s|@DATA_DIR@|${prom_data_dir}|g" \
        ${workpath}/org.macports.prometheus.plist

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

destroot.keepdirs-append ${destroot}${prom_data_dir} \
                         ${destroot}${prom_conf_dir}

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

    xinstall -d -m 0755 ${destroot}${prom_conf_dir}
    xinstall -d -m 0755 ${destroot}${prom_doc_dir}
    xinstall -d -m 0755 ${destroot}${prom_share_dir}

    xinstall -m 0755 -o ${prom_user} -g ${prom_user} -d \
        ${destroot}${prom_data_dir}

    xinstall -m 0755 -o ${prom_user} -g ${prom_user} -d \
        ${destroot}${prom_log_dir}

    touch ${destroot}${prom_log_file}

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

    copy ${filespath}/example-prometheus-config.yml \
        ${destroot}${prom_share_dir}/

    copy {*}[glob ${worksrcpath}/docs/*] ${destroot}${prom_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

}

post-activate {
    if {![file exists ${prom_conf_file}]} {
        copy  ${prom_share_dir}/example-prometheus-config.yml ${prom_conf_file}
    }
}

notes "
To change Prometheus configuration, you can make edits to:

 ${prom_conf_file}

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

\$ sudo port load ${name}

Once enabled, the service will:

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

  - log to: ${prom_log_file}

"

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