##
##  sysmon.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:         sysmon
Summary:      System Monitoring Tool
URL:          http://www.sysmon.org/
Vendor:       Jared Mauch
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Monitoring
License:      GPL
Version:      0.93
Release:      20150731

#   package options
%option       with_fsl  yes
%option       with_snmp no

#   list of sources
Source0:      ftp://puck.nether.net/pub/jared/sysmon-%{version}.tar.gz
Source1:      sysmon.conf
Source2:      rc.sysmon
Source3:      fsl.sysmon
Patch0:       sysmon.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, MTA, flex
PreReq:       OpenPKG, openpkg >= 20160101, MTA
BuildPreReq:  ncurses, openssl
PreReq:       ncurses, openssl
%if "%{with_snmp}" == "yes"
BuildPreReq:  snmp
PreReq:       snmp
%endif
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif

%description
    Sysmon is a network monitoring tool designed to provide high
    performance and accurate network monitoring. Currently supported
    protocols include SMTP, IMAP, HTTP, NNTP, RADIUS, SNMP, TCP, UDP,
    and ICMP/PING tests.

%track
    prog sysmon = {
        version   = %{version}
        url       = ftp://puck.nether.net/pub/jared/
        regex     = sysmon-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   fix SNMP support
%if "%{with_snmp}" == "yes"
    %{l_shtool} subst \
        -e 's;<ucd-snmp/ucd-snmp;<net-snmp/net-snmp;g' \
        -e 's;RECEIVED_MESSAGE;NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE;g' \
        src/snmp.c
    %{l_shtool} subst -e 's;-lsnmp;-lnetsnmp;g' configure
%else
    %{l_shtool} subst -e 's;test "$USABLE_SNMP" = "yes";false;g' configure
%endif

    #   configure program
    %{l_shtool} subst \
        -e 's;\(#define PIDFILE\).*;\1 "%{l_prefix}/var/sysmon/sysmond.pid";' \
        src/config.h
    echo "ac_cv_lib_curses_refresh=no" >config.cache
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags ncurses}" \
    LDFLAGS="%{l_ldflags} %{l_fsl_ldflags}" \
%if "%{with_snmp}" == "yes"
    LIBS="%{l_fsl_libs} `%{l_prefix}/bin/net-snmp-config --libs`" \
%else
    LIBS="%{l_fsl_libs}" \
%endif
    ./configure \
        --cache-file=./config.cache \
        --prefix=%{l_prefix}

    #   build program
    %{l_make} %{l_mflags}

%install
    #   install program
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/sysmon \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sysmon \
        $RPM_BUILD_ROOT%{l_prefix}/man/man5 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man8 \
        $RPM_BUILD_ROOT%{l_prefix}/var/sysmon
    %{l_shtool} install -c -s -m 755 \
        src/sysmon src/sysmond $RPM_BUILD_ROOT%{l_prefix}/sbin/
    %{l_shtool} install -c -m 644 \
        docs/sysmon.conf.man $RPM_BUILD_ROOT%{l_prefix}/man/man5/sysmon.conf.5
    %{l_shtool} install -c -m 644 \
        docs/sysmon.man $RPM_BUILD_ROOT%{l_prefix}/man/man8/sysmon.8
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE sysmon.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/sysmon/

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

    #   install OSSP fsl configuration
    %{l_shtool} mkdir -f -p -m 755 $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE fsl.sysmon} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/fsl/fsl.sysmon' \
        '%config %{l_prefix}/etc/sysmon/sysmon.conf'

%files -f files

%clean

