##
##  kapacitor.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
%define       V_kapacitor_base  1.5.9
%define       V_kapacitor_snap  20210402

#   package information
Name:         kapacitor
Summary:      Time-Series Data Alerting
URL:          https://influxdata.com/time-series-platform/kapacitor/
Vendor:       InfluxData
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      MIT
Version:      %{V_kapacitor_base}.%{V_kapacitor_snap}
Release:      20210402

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/kapacitor/kapacitor-%{V_kapacitor_snap}.tar.xz
Source1:      rc.kapacitor
Source2:      kapacitor.conf

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

%description
    Kapacitor is a framework for processing, monitoring, and alerting on
    time series data.

%track
    prog kapacitor = {
        version   = %{V_kapacitor_base}
        url       = https://github.com/influxdata/kapacitor/releases
        regex     = (\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q -n kapacitor

%build
    #   build program
    (   export GOPATH=`pwd`
        cd $GOPATH/src/github.com/influxdata/kapacitor
        go build -v -o kapacitor  cmd/kapacitor/*.go
        go build -v -o kapacitord cmd/kapacitord/*.go
    ) || exit $?

%install
    #   create directory 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/kapacitor/kapacitor.d \
        $RPM_BUILD_ROOT%{l_prefix}/var/kapacitor/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/kapacitor/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/kapacitor/db/replay \
        $RPM_BUILD_ROOT%{l_prefix}/var/kapacitor/db/tasks

    #   install program
    %{l_shtool} install -c -s -m 755 \
        src/github.com/influxdata/kapacitor/kapacitor \
        $RPM_BUILD_ROOT%{l_prefix}/bin/
    %{l_shtool} install -c -s -m 755 \
        src/github.com/influxdata/kapacitor/kapacitord \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/

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

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

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

%files -f files

%clean

%post
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} kapacitor status 2>/dev/null`
        [ ".$kapacitor_active" = .yes ] && %{l_rc} kapacitor restart
    fi
    exit 0

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

