##
##  kibana.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         kibana
Summary:      Kibana Extension for ElasticSearch
URL:          https://www.elastic.co/products/kibana
Vendor:       Shay Banon
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Database
License:      Apache
Version:      9.1.5
Release:      20251008

#   list of sources
Source0:      https://artifacts.elastic.co/downloads/kibana/kibana-%{version}-linux-x86_64.tar.gz
Source1:      rc.kibana
Source2:      kibana.yml
Source3:      kibana-tls.sh

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, node, node-openpkg
PreReq:       OpenPKG, openpkg >= 20160101, node, cfssl, x509
PreReq:       elasticsearch
PreReq:       elasticsearch-xpack

%description
    This is Kibana, a web user interface for ElasticSearch.

%track
    prog kibana = {
        version   = %{version}
        url       = https://www.elastic.co/downloads/kibana
        regex     = kibana-(\d+\.\d+\.\d+)-linux-x86_64\.tar\.gz
    }

%prep
    %setup -q -n kibana-%{version}

%build
    #   remove embedded Node (of Linux)
    rm -rf node

    #   remove problematic files
    rm -rf "node_modules/file-saver/demo 10.23.13"
    rm -rf "node_modules/vega-lib/build sync recursive"

    #   reduce distribution
    rm -f [A-Z]*.txt
    %{l_prefix}/bin/node-openpkg squeeze -n "default:safe"

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/kibana \
        $RPM_BUILD_ROOT%{l_prefix}/lib/kibana \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/kibana/plugins \
        $RPM_BUILD_ROOT%{l_prefix}/var/kibana/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/kibana/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/kibana/log

    #   install program
    cp -rp * \
        $RPM_BUILD_ROOT%{l_prefix}/lib/kibana/
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib/kibana/config
    ln -s ../../etc/kibana \
        $RPM_BUILD_ROOT%{l_prefix}/lib/kibana/config
    rmdir $RPM_BUILD_ROOT%{l_prefix}/lib/kibana/plugins
    ln -s ../../libexec/kibana/plugins \
        $RPM_BUILD_ROOT%{l_prefix}/lib/kibana/plugins
    rmdir $RPM_BUILD_ROOT%{l_prefix}/lib/kibana/data
    ln -s ../../var/kibana/data \
        $RPM_BUILD_ROOT%{l_prefix}/lib/kibana/data

    #   install wrapper commands
    for prog in kibana kibana-plugin kibana-keystore; do
        (   echo "#!/bin/sh"
            echo "PATH=\"%{l_prefix}/bin:\$PATH\""
            echo "exec %{l_prefix}/lib/kibana/bin/$prog \${1+\"\$@\"}"
        ) >$prog
        %{l_shtool} install -c -m 755 \
            $prog $RPM_BUILD_ROOT%{l_prefix}/bin/$prog
    done

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE kibana.yml} $RPM_BUILD_ROOT%{l_prefix}/etc/kibana/

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.kibana} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install TLS tool
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_bash@;%{l_bash};g' \
        %{SOURCE kibana-tls.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/kibana-tls

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/kibana/*' \
        '%dir %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/kibana' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/kibana' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/kibana/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   create initial Kibana keystore
        echo "Creating initial Kibana keystore" | \
            %{l_rpmtool} msg -b -t notice
        su - %{l_rusr} -c \
            "LC_CTYPE=C; export LC_CTYPE; umask 077; \
            $RPM_INSTALL_PREFIX/bin/kibana-keystore create"

        #   create initial SSL/TLS certificates/keys
        echo "Generating SSL/TLS Certificates/Keys" | %{l_rpmtool} msg -b -t notice
        $RPM_INSTALL_PREFIX/bin/kibana-tls localhost 127.0.0.1
        ( echo "Kibana was configured with a standard TLS certificate/key pair."
          echo "for \"localhost\" and \"127.0.0.1\". For production use, you usually let"
          echo "Kibana listen on an external IP address. For this the TLS"
          echo "certificate/key pair has to be regenerated with for instance:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/kibana-tls \\%{l_nil}"
          echo "      www.example.com 192.168.0.1"
        ) | %{l_rpmtool} msg -b -t notice

        #   display final hints on initial installation
        (   echo "By default, Kibana runs its server process on IPv4 address"
            echo "127.0.0.1, TCP port 5601. Start the Kibana server by running:"
            echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc kibana start"
            echo "Connect to Kibana via its Web UI through the URL:"
            echo "    https://localhost:5601/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} kibana status 2>/dev/null`
        [ ".$kibana_active" = .yes ] && %{l_rc} kibana restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove runtime files
        %{l_rc} kibana stop 2>/dev/null
        rm -f  $RPM_INSTALL_PREFIX/etc/kibana/*.keystore >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/kibana/run/*      >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/kibana/log/*      >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/kibana/data/*     >/dev/null 2>&1 || true
    fi
    exit 0

