##
##  sec.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_dist 2.9.3
%define       V_opkg 2.9.3

#   package information
Name:         sec
Summary:      Simple Event Correlator
URL:          http://kodu.neti.ee/~risto/sec/
Vendor:       Risto Vaarandi
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        System
License:      Open Source
Version:      %{V_opkg}
Release:      20241102

#   list of sources
Source0:      http://download.sourceforge.net/simple-evcorr/sec-%{V_dist}.tar.gz
Source1:      rc.sec
Source2:      sec.conf
Source3:      sec.rule

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

%description
    SEC is a tool that was designed to solve event correlation tasks
    in network and system management. Event correlation is a process
    where a stream of primitive events is processed in order to detect
    composite events that correspond to event patterns in the event
    stream. After startup SEC reads lines from files, named pipes,
    or standard input, matches the lines with regular expressions to
    recognize input events, and correlates events according to the rules
    that are specified in its configuration file. SEC can be configured
    to produce its output by executing user-specified shell commands,
    and by using utilities like snmptrap(1) or snmpnotify(1), SEC can
    generate network management events as output. Other options for
    producing output events are described further in this man page.

%track
    prog sec = {
        version   = %{V_dist}
        url       = http://sourceforge.net/projects/simple-evcorr/files/
        regex     = sec-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n sec-%{V_dist}

%build

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sec \
        $RPM_BUILD_ROOT%{l_prefix}/var/sec

    #   install program and manual page
    %{l_shtool} install -c -m 755 \
        -e 's;/usr/bin/perl;%{l_prefix}/bin/perl;' \
        sec $RPM_BUILD_ROOT%{l_prefix}/bin/sec
    %{l_shtool} install -c -m 644 \
        sec.man $RPM_BUILD_ROOT%{l_prefix}/man/man1/sec.1

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

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.sec} $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/sec/*'

%files -f files

%clean

%post
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} sec status 2>/dev/null`
    [ ".$sec_active" = .yes ] && %{l_rc} sec restart
    exit 0

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

