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

# mirror version from elasticsearch
name                kibana
version             7.16.0
revision            0
categories          databases
maintainers         {ieee.org:s.t.smith @essandess} openmaintainer

description         Your window into the Elastic Stack
long_description    Kibana is your window into the Elastic \
                    Stack. Specifically, it's a browser-based \
                    analytics and search dashboard for Elasticsearch.

supported_archs     x86_64
license             Apache-2
homepage            https://www.elastic.co/

master_sites        https://artifacts.elastic.co/downloads/${name}/
distname            ${name}-${version}-darwin-x86_64

checksums           rmd160  d26695f956fb87426278cbdbd08fdd4a8f1f5fcc \
                    sha256  d6ffff4abfeb04dfdf65e127511a843e0bbe7929cf4bdee0190505dd769fb33b \
                    size    277018484

depends_lib-append \
                    port:elasticsearch

# diff -Naur -I '^ *#' config/kibana.yml.orig config/kibana.yml | sed -E 's|\.orig||' > patch-kibana-yml.diff
patchfiles          patch-kibana-yml.diff

use_configure       no

# Kibana binary distribution requires macOS 10.13 and later
set min_darwin 17
if {${os.platform} eq "darwin" && ${os.major} < ${min_darwin}} {
    known_fail yes
    pre-fetch {
        ui_error "${name} is only supported on macOS 10.[expr ${min_darwin} - 4] or later."
        return -code error "unsupported platform version"
    }
}

pre-build {
    # Change the PREFIX in the config files. They are already patched.
    reinplace s|@PREFIX@|${prefix}|g ${worksrcpath}/config/kibana.yml
}

build {}

set kibanadir       ${prefix}/share/${name}
set confdir         ${prefix}/etc/${name}
set logdir          ${prefix}/var/log/${name}
set piddir          ${prefix}/var/run/${name}
set dbdir           ${prefix}/var/${name}
set user            elasticsearch
set group           ${user}

destroot {
   # Setup the directories used by kibana
   xinstall -m 0755 -o ${user} -g ${group} -d \
       ${destroot}${kibanadir} \
       ${destroot}${kibanadir}/data \
       ${destroot}${kibanadir}/plugins \
       ${destroot}${confdir} \
       ${destroot}${logdir} \
       ${destroot}${piddir} \
       ${destroot}${dbdir}

   # These first two directories are empty, so we need to preserve them
   destroot.keepdirs-append \
       ${destroot}${kibanadir}/data \
       ${destroot}${kibanadir}/plugins \
       ${destroot}${logdir} \
       ${destroot}${piddir} \
       ${destroot}${dbdir}

   foreach f_or_d [glob ${worksrcpath}/*] {
       set basename [file tail ${f_or_d}]
       if {[file isdirectory ${f_or_d}]
           && [regexp {^(data|plugins)$} ${basename}]} {
           continue
       }
       copy ${f_or_d} ${destroot}${kibanadir}
   }

   # Symlink the binary
   ln -s ../share/${name}/bin/${name} ${destroot}${prefix}/bin/${name}
}

post-activate {
   # Give the appropriate permissions on the file system
   system "chown -R ${user}:${group} ${kibanadir}"
   system "chown -R ${user}:${group} ${logdir}"
   system "chown -R ${user}:${group} ${piddir}"
   system "chown -R ${user}:${group} ${dbdir}"
}

startupitem.create  yes
startupitem.logfile ${logdir}/daemon.log
startupitem.executable \
    sudo -u ${user} ${prefix}/bin/${name}