# -*- 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
PortGroup           legacysupport 1.1

go.setup            github.com/grafana/grafana 12.2.0 v
revision            0

homepage            https://grafana.com

description         The tool for beautiful monitoring and metric analytics & \
                    dashboards for Graphite, InfluxDB, Prometheus & More

long_description    Grafana allows you to query, visualize, alert on and \
                    understand your metrics no matter where they are stored. \
                    Create, explore, and share dashboards with your team and \
                    foster a data driven culture.

platforms           darwin
categories          net sysutils
license             Apache-2

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

checksums           rmd160  d7e4ed48404c4e3234c51477b054690eccef85d3 \
                    sha256  0d55795b21cf5494e5e3ff33627f7bbb940de9298d0a0aa86831c2f2f15fef1e \
                    size    42237954

depends_build-append \
                    path:bin/node:nodejs16 \
                    port:yarn

# Allow fetching deps at build time for now
go.offline_build no

build.cmd           make
build.target        all

use_configure       no
installs_libs       no
use_parallel_build  no
use_xcode           yes

set grafana_user        ${name}
set grafana_conf_dir    ${prefix}/etc/${name}
set grafana_conf_file   ${grafana_conf_dir}/${name}.ini
set grafana_data_dir    ${prefix}/var/db/${name}
set grafana_sock_dir    ${prefix}/var/run/${name}
set grafana_log_dir     ${prefix}/var/log/${name}
set grafana_share_dir   ${prefix}/share/${name}
set grafana_conf_tpl    ${grafana_share_dir}/conf/${name}.ini
set grafana_plist       org.macports.${name}.plist
set grafana_plist_dir   ${prefix}/etc/LaunchDaemons/org.macports.${name}

add_users               ${grafana_user} group=${grafana_user} realname=Grafana

post-patch {

    copy ${filespath}/${grafana_plist} ${workpath}

    reinplace "s|@USER@|${grafana_user}|g"          ${workpath}/${grafana_plist}
    reinplace "s|@GROUP@|${grafana_user}|g"         ${workpath}/${grafana_plist}
    reinplace "s|@PREFIX@|${prefix}|g"              ${workpath}/${grafana_plist}
    reinplace "s|@CONFFILE@|${grafana_conf_file}|g" ${workpath}/${grafana_plist}
    reinplace "s|@SHAREDIR@|${grafana_share_dir}|g" ${workpath}/${grafana_plist}

}


destroot.keepdirs-append \
    ${destroot}${grafana_conf_dir} \
    ${destroot}${grafana_data_dir} \
    ${destroot}${grafana_sock_dir} \
    ${destroot}${grafana_log_dir}


destroot {
    xinstall -m 755 ${worksrcpath}/bin/${goos}-${goarch}/${name} \
        ${destroot}${prefix}/bin/

    xinstall -m 755 ${worksrcpath}/bin/${goos}-${goarch}/${name}-cli \
        ${destroot}${prefix}/bin/

    xinstall -m 755 ${worksrcpath}/bin/${goos}-${goarch}/${name}-server \
        ${destroot}${prefix}/bin/

    xinstall -d -m 755 ${destroot}${grafana_conf_dir}
    xinstall -d -m 755 ${destroot}${grafana_share_dir}

    xinstall -m 755 -o ${grafana_user} -g ${grafana_user} -d \
        ${destroot}${grafana_data_dir}

    xinstall -m 755 -o ${grafana_user} -g ${grafana_user} -d \
        ${destroot}${grafana_log_dir}

    xinstall -m 755 -o ${grafana_user} -g ${grafana_user} -d \
        ${destroot}${grafana_sock_dir}

    xinstall -d -m 755 ${destroot}${grafana_share_dir}/conf
    xinstall -d -m 755 ${destroot}${grafana_share_dir}/public

    copy {*}[glob ${worksrcpath}/conf/*] \
        ${destroot}${grafana_share_dir}/conf/
    copy {*}[glob ${worksrcpath}/public/*] \
        ${destroot}${grafana_share_dir}/public/

    copy ${destroot}${grafana_share_dir}/conf/defaults.ini \
         ${destroot}${grafana_conf_tpl}

    system -W ${destroot}${grafana_share_dir}/conf/ \
        "patch -p0 < ${filespath}/grafana.ini.diff"

    reinplace "s|@CONFPATH@|${grafana_conf_dir}|g" \
        ${destroot}${grafana_conf_tpl}
    reinplace "s|@DATAPATH@|${grafana_data_dir}|g" \
        ${destroot}${grafana_conf_tpl}
    reinplace "s|@SOCKPATH@|${grafana_sock_dir}|g"   \
        ${destroot}${grafana_conf_tpl}
    reinplace "s|@LOGPATH@|${grafana_log_dir}|g"   \
        ${destroot}${grafana_conf_tpl}

    xinstall -d -m 755 ${destroot}${grafana_plist_dir}

    xinstall -m 0644 -o root -W ${workpath} ${grafana_plist} \
        ${destroot}${grafana_plist_dir}

    xinstall -d -m 755 ${destroot}/Library/LaunchDaemons

    ln -s ${grafana_plist_dir}/${grafana_plist} \
        ${destroot}/Library/LaunchDaemons/${grafana_plist}

}


post-activate {
    if {![file exists ${grafana_conf_file}]} {
        copy ${grafana_conf_tpl} ${grafana_conf_file}
    }
}

notes "
Grafana's configuration file is available at: ${grafana_conf_file}
To enable the Grafana service, use `port load`, as follows:

\$ sudo port load ${name}

Once the service is enabled, Grafana will:

  - listen by default on: http://localhost:3000
  - write logs to: ${grafana_log_dir}

You will need to run grafana-cli with a custom homepath and config setting.
Here is an example of doing so to set the admin password:

\$ sudo -u grafana grafana cli --homepath ${grafana_share_dir} --config ${grafana_conf_dir}/grafana.ini admin reset-admin-password onetwothree

"

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