# -*- 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/influxdata/influxdb 2.7.10 v
# Delete this on next update to use golang PortGroup's default ('archive')
github.tarball_from         tarball
go.offline_build            no
revision                    0

homepage                    https://influxdata.com

description                 Scalable datastore for metrics, events, and \
                            real-time analytics

long_description            InfluxDB is an open source time series platform. \
                            This includes APIs for storing and querying data, \
                            processing it in the background for ETL, monitoring \
                            and/or alerting purposes, user dashboards, \
                            visualizing and exploring the data and more.

categories                  sysutils net
installs_libs               no
license                     MIT
maintainers                 {gmail.com:herby.gillot @herbygillot} \
                            openmaintainer

# Build script (build.py) requires a git checkout
fetch.type                  git

# Add local gopath/bin to PATH
build.env-append            PATH=$env(PATH):${gopath}/bin

depends_build-append        bin:node:nodejs22 \
                            bin:npm:npm10 \
                            port:cargo \
                            path:libexec/coreutils/libstdbuf.so:coreutils \
                            port:pkgconfig \
                            bin:protoc:protobuf3-cpp \
                            port:rust \
                            port:yarn

set influxdb_user           ${name}
set influxdb_conf_dir       ${prefix}/etc/${name}
set influxdb_data_dir       ${prefix}/var/db/${name}
set influxdb_log_dir        ${prefix}/var/log/${name}
set influxdb_share_dir      ${prefix}/share/${name}

set influxdb_conf_file      ${influxdb_conf_dir}/config.toml
set influxdb_example_conf   ${influxdb_share_dir}/config.toml.example
set influxdb_log_file       ${influxdb_log_dir}/influxdb.log
set influxdb_plist_src      ${workpath}/org.macports.${name}.plist

add_users                   ${influxdb_user} \
                            group=${influxdb_user} \
                            realname=InfluxDB \
                            home=${influxdb_data_dir}

build.cmd                   make
use_parallel_build          no
use_xcode                   yes

post-extract {
    copy ${filespath}/org.macports.influxdb.plist ${influxdb_plist_src}

    copy ${filespath}/config.toml.example ${workpath}/
}

patch {
    reinplace "s|@DATA_DIR@|${influxdb_data_dir}|g" \
        ${workpath}/config.toml.example

    reinplace "s|@NAME@|${name}|g"                    ${influxdb_plist_src}
    reinplace "s|@USER@|${influxdb_user}|g"           ${influxdb_plist_src}
    reinplace "s|@GROUP@|${influxdb_user}|g"          ${influxdb_plist_src}
    reinplace "s|@PREFIX@|${prefix}|g"                ${influxdb_plist_src}
    reinplace "s|@CONF_FILE@|${influxdb_conf_file}|g" ${influxdb_plist_src}
    reinplace "s|@LOGFILE@|${influxdb_log_file}|g"    ${influxdb_plist_src}

    foreach _script {
        scripts/fetch-ui-assets.sh
        scripts/ci/test-downgrade.sh
    } {
        reinplace -E "s|sha256sum|${prefix}/bin/gsha256sum|g" \
            ${worksrcpath}/${_script}
    }
}

destroot {

    copy {*}[glob ${worksrcpath}/bin/${goos}/*] ${destroot}${prefix}/bin/

    xinstall  -d -m 0755 ${destroot}${influxdb_conf_dir}
    xinstall  -d -m 0755 ${destroot}${influxdb_share_dir}
    xinstall  -d -m 0755 -o ${influxdb_user} -g ${influxdb_user} \
              ${destroot}${influxdb_data_dir}
    xinstall  -d -m 0755 -g ${influxdb_user} ${destroot}${influxdb_log_dir}

    copy ${workpath}/config.toml.example ${destroot}${influxdb_example_conf}

    touch ${destroot}${influxdb_log_file}
    file attributes ${destroot}${influxdb_log_file} -owner ${influxdb_user}

    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
}

destroot.keepdirs-append  ${destroot}${influxdb_conf_dir} \
                          ${destroot}${influxdb_data_dir}

post-activate {

    if {![file exists ${influxdb_conf_file}]} {
        copy ${influxdb_example_conf} ${influxdb_conf_file}
    }
}

notes "
ATTENTION: the InfluxDB CLI is no longer packaged as part of InfluxDB.
Please install the influx-cli port.

To start the InfluxDB service, use `port load`:

    \$ sudo port load ${name}
    \$ influx # if you installed the CLI via the influx-cli port

`port unload` will stop and remove the service:

    \$ sudo port unload ${name}

After starting the service, you can find InfluxDB's configuration file at:

    ${influxdb_conf_file}
"

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