# -*- 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.13.0 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

depends_build-append \
                    port:golangci-lint \
                    port:promu \
                    port:pnpm

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  e35f765d9a50ea4ce972ea3ef273071f8d06b94f \
                    sha256  fd0bfdc1390e71c3eeb05532293f9e2a1279e75d3546aa86dc4881627c9e143d \
                    size    5939062

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.]+)}
