#
#
%undefine _missing_build_ids_terminate_build
%global _unpackaged_files_terminate_build 1

%define _prefix            /usr
%define mandir             /usr/share/man
%define sysconfdir         /etc/indimail

%if %{defined _project}
# define to 1 if building on openSUSE build service
%define build_on_obs       1
%define fast_mode          1
%else
%define build_on_obs       0
%define fast_mode          1
%global _hardened_build    1
%endif

%if %{defined nodebug}
%global debug_package %{nil}
%endif

%if %build_on_obs == 1
%global packager           Manvendra Bhangui <Manvendra Bhangui <logalert@indimail.org>>
%endif

Summary: Logfile monitoring, alert and analysis tool
Name: logalert
Version: 0.4.0
Release: 1.1%{?dist}
#Release: 1.<B_CNT>

%if %build_on_obs == 1
License: GPL-3.0+
%else
License: GPLv3
%endif
URL: http://www.indimail.org
Group: Productivity/Networking/Email/Utilities
Source0: http://downloads.sourceforge.net/indimail/%{name}-%{version}.tar.gz
Source1: http://downloads.sourceforge.net/indimail/%{name}-rpmlintrc

%if %build_on_obs == 1
BuildRequires: -post-build-checks  
%endif

BuildRequires: gcc gcc-c++ make autoconf automake
%if %{defined suse_version} || %{defined sles_version}
BuildRequires: bison
%endif
%if 0%{?fedora_version} > 27 || 0%{?centos_version} > 700 || 0%{?rhel_version} > 700 || 0%{?almalinux_version} > 700 || 0%{?rocky_version} > 700 || 0%{?fedora} > 30 || 0%{?rhel} > 7 || %{defined amzn} || %{defined mgaversion} || %{defined openEuler}
BuildRequires: libtirpc libtirpc-devel
%endif
%if 0%{?suse_version} >= 1550 || 0%{?sle_version} >= 150300
BuildRequires: libtirpc3 libtirpc-devel
%endif
BuildRequires: pcre2-devel libqmail-devel
BuildRequires: flex coreutils bison
Requires: coreutils grep /bin/sh glibc

%description
logalert is a logfile monitoring tool which executes a specific action
whenever it matches a string (pattern) occurrence.

It reads an entire file (or starts at the end, just like tail -f), keeps
track  of any changes, waiting for a specific pattern (a syslog process
error, a user login, ...) and fires an action you define when that happens.

logalert uses regular expression to match the pattern occurrence and executes
commands  via shell interaction. It deals fine with logrotation and temporary
removal normally used by sysadmins.

%prep
%setup -q

%build
(
echo "---------------- INFORMATION ------------------------"
echo target         %_target
echo target_alias   %_target_alias
echo target_cpu     %_target_cpu
echo target_os      %_target_os
echo target_vendor  %_target_vendor
echo Building %{name}-%{version}-%{release} Build %{_build} OS %{_os} lib=%{_lib} libdir=%{_libdir}
echo "------------------------------------------------------"
) > %{name}-rpm.info
%configure --prefix=%{_prefix}  \
  --sysconfdir=%{sysconfdir} \
  --mandir=%{mandir} \
  --datarootdir=%{_prefix}/share/indimail
%{__make} %{?_smp_mflags}

%install
%if %{defined nodebug}
%{__make} DESTDIR=%{buildroot} install-strip
%else
%{__make} DESTDIR=%{buildroot} install
%endif
install -m 0644 %{name}-rpm.info %{buildroot}%{sysconfdir}/%{name}-rpm.info
/bin/rm -f %{name}-rpm.info

%files
%defattr(-,root,root)

%attr(0755,root,root)                          %{_prefix}/bin/logalert
%attr(0755,root,root)                          %{_prefix}/bin/logclient
%attr(0755,root,root)                          %{_prefix}/bin/incrmesg
%attr(0755,root,root)                          %{_prefix}/bin/showbytes
%attr(0755,root,root)                          %{_prefix}/sbin/rpclog
%attr(0755,root,root)                          %{_prefix}/sbin/logsrv
%attr(0644,root,root)                          %{sysconfdir}/logalert.perms
%attr(0444,root,root)                          %{sysconfdir}/%{name}-release
%attr(0444,root,root)                          %{sysconfdir}/%{name}-rpm.info

%doc %attr(0644,root,root)                     %{_prefix}/share/indimail/doc/logalert/LICENSE
%doc %attr(0644,root,root)                     %{_prefix}/share/indimail/doc/logalert/README.md

%doc %attr(0644,root,root)                     %{_mandir}/man1/incrmesg.1.*
%doc %attr(0644,root,root)                     %{_mandir}/man1/logclient.1.*
%doc %attr(0644,root,root)                     %{_mandir}/man8/logalert.8.*
%doc %attr(0644,root,root)                     %{_mandir}/man8/logsrv.8.*
%doc %attr(0644,root,root)                     %{_mandir}/man8/showbytes.8.*
%doc %attr(0644,root,root)                     %{_mandir}/man8/rpclog.8.*

%post
argv1=$1
if [ -z "$argv1" ] ; then
  argv1=0
fi
if [ $argv1 -eq 2 ] ; then # upgrade
  # we are doing upgrade
  exit 0
fi
if [ -f /etc/rpc ] ; then
  grep 536870913 /etc/rpc > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    echo "rpclog      536870913 rpclog" >> /etc/rpc
  fi
fi
%changelog
* Fri May 10 2024 12:49:15 +0000 Manvendra Bhangui <logalert@indimail.org> 0.4.0-1.1%{?dist}
Release 0.4.0-1.1 Start 13/04/2023 End 15/10/2023
- Refactored logsrv.c, logclient.c incrmesg.c, rpclog.c, showbytes.c -
  https://github.com/mbhangui/indimail-virtualdomains/pull/29
- logclient.c: added -v, -p option
- logsrv.c: removed -c option
- rpclog.c: print RPC program number, version during startup.
- logclient.c: Ensure qmail/tls.h gets included
- logclient.c: fixed compilation warnings
- parser.y: added missing prototype for yylex()