PortSystem          1.0
PortGroup           github 1.0

github.setup        prometheus prometheus 2.14.0 v
github.tarball_from archive

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.

homepage            https://prometheus.io/

platforms           darwin
categories          net
license             Apache-2

maintainers         {gmail.com:herby.gillot @herbygillot} openmaintainer

depends_build       port:go \
                    port:promu \
                    port:yarn

build.env           GOPATH=${workpath} \
                    PATH=${workpath}/bin:$env(PATH)

build.target        build

use_configure       no
installs_libs       no
use_parallel_build  no

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  22f7a8eff328b7ac6c1788fb0662f5b67c241b12 \
            sha256  5c40de1961997996ef5b59561a78116a9548235bc77305c05f214b6319a0284d \
            size    12755125

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

post-extract {
    # Link promu into the workpath
    xinstall -d ${workpath}/bin
    ln -s ${prefix}/bin/promu ${workpath}/bin/

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

    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 755 ${worksrcpath}/${name}  ${destroot}${prefix}/bin/${name}
    xinstall -m 755 ${worksrcpath}/promtool ${destroot}${prefix}/bin/promtool
    xinstall -m 755 ${worksrcpath}/tsdb/tsdb ${destroot}${prefix}/bin/tsdb

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

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

    xinstall -m 755 -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 755 \
        ${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 755 ${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 log to:
 ${prom_log_file}
"