##
##  grafana.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 version
%define       V_grafana_base 4.6.3
%define       V_grafana_snap 20180131

#   package information
Name:         grafana
Summary:      Metrics Dashboard for InfluxDB
URL:          http://grafana.org/
Vendor:       Torkel Ödegaard
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      Apache
Version:      %{V_grafana_base}.%{V_grafana_snap}
Release:      20180131

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/grafana/grafana-%{V_grafana_snap}.tar.xz
Source1:      rc.grafana
Source2:      grafana.ini
Patch0:       grafana.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go, rsync
PreReq:       OpenPKG, openpkg >= 20160101

%description
    Grafana is an open source, feature rich metrics dashboard and graph
    editor for the statistic storage databases Graphite, ElasticSearch,
    OpenTSDB, Prometheus and InfluxDB.

%track
    prog grafana = {
        version   = %{V_grafana_base}
        url       = https://grafana.com/grafana/download
        regex     = grafana-(\d+\.\d+(?:\.\d+)*)\.
    }

%prep
    %setup -q -n grafana
    %patch -p0

%build
    #   patch paths
    %{l_shtool} subst %{l_value -s -a} \
        src/github.com/grafana/grafana/pkg/setting/setting.go

    #   build program
    (   export GOPATH=`pwd`
        cd src/github.com/grafana/grafana
        go run build.go build
    ) || exit $?

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

    #   install frontend files
    rsync -ra src/github.com/grafana/grafana/public/* \
        $RPM_BUILD_ROOT%{l_prefix}/lib/grafana/

    #   install backend program
    %{l_shtool} install -c -m 755 \
        src/github.com/grafana/grafana/bin/grafana-server \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/
    %{l_shtool} install -c -m 755 \
        src/github.com/grafana/grafana/bin/grafana-cli \
        $RPM_BUILD_ROOT%{l_prefix}/bin/

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

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   give hint on initial installation
        ( echo "To complete your Grafana installation, please start the server:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc grafana start"
          echo "Then connect to Grafana with username \"admin\" and password \"admin\":"
          echo "    http://localhost:3000/"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} grafana status 2>/dev/null`
        [ ".$grafana_active" = .yes ] && %{l_rc} grafana restart
    fi
    exit 0

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

