# DNF or YUM ?
%if 0%{?fedora} >= 22 || 0%{?rhel} >= 8
%global dnfyum dnf
%else
%global dnfyum yum
%endif

# Use python3.9 on CentOS 8
%if 0%{?rhel} == 8
#%%global python3_pkgversion 39
# force to use unversioned python3
%global __python3 /usr/bin/python3
%endif

%global sagator_gpg_key https://www.salstar.sk/pub/sagator/SAGATOR-GPG-KEY
%global salstar_gpg_key https://www.salstar.sk/gpg/salstar.sk-GPG-KEY

# use alternative locate on new systems, force to mlocate on centos 7
%if 0%{?rhel} == 7
%global xlocate mlocate
%else
%global xlocate (plocate or mlocate or locate)
%endif

%global nagios_plugins %{_libdir}/nagios/plugins

Name:          salpack
Version:       __VERSION__
Release:       1%{?dist}
Summary:       SAL's default settings
BuildArch:     noarch

Group:         System Environment/Base
License:       GPLv2+
URL:           https://www.salstar.sk/pub/salpack/
Source:        https://www.salstar.sk/pub/salpack/salpack.tar.gz
BuildRoot:     %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)

%if 0%{?fedora} >= 30 || 0%{?rhel} >= 8
BuildRequires: python3
%else
BuildRequires: python2
%endif
Requires:      crontabs, util-linux, ethtool
#Requires:      ntp
%if "%{dnfyum}" == "dnf"
Requires:      dnf, dnf-plugins-core
%else
Obsoletes:     yum-cron > 3.2
Requires:      yum-utils
%endif
Requires:      rsync, mc, joe, bc, file, tar, gzip
Requires:      tmpwatch
Requires:      logrotate
Requires:      net-tools
# Required for logwatch, ...
Requires:      postfix
Requires:      bash-completion, pv, newt
%if 0%{?fedora} >= 14 || 0%{?rhel} >= 8
Requires:      man-db
%else
Requires:      man
%endif
%if 0%{?rhel} >= 7 || 0%{?fedora} >= 18
BuildRequires: systemd
%if 0%{?rhel} >= 9 || 0%{?fedora} >= 32
Requires:      (iptables-services or nftables)
%else
Requires:      iptables-services
%endif
%endif
Requires:      dmidecode
# obsolete this package to fix upgrade path
%if 0%{?rhel} == 8
Obsoletes:     sysvinit-tools > 2.87
%endif

%description
SAL's default system settings.


%package mysql
Summary:  salpack mysql package
Group:    Applications/Databases
Provides: %{name}-mysql-or-mariadb = %{version}-%{release}
Requires: %{name}, mysql-server
%if 0%{?rhel} > 7 || 0%{?fedora} >= 20
Requires: python3-PyMySQL
%else
Requires: MySQL-python
%endif

%description mysql
SAL's default MySQL packages.


%package mariadb
Summary:  salpack mariadb package
Group:    Applications/Databases
Provides: %{name}-mysql-or-mariadb = %{version}-%{release}
Requires: %{name}
%if 0%{?rhel} == 6
Requires: mariadb55
%else
Requires: mariadb-server
#Obsoletes: %%{name}-mysql <= %%{version}-%%{release}
#Conflicts: %%{name}-mysql <= %%{version}-%%{release}
%endif
%if 0%{?rhel} > 7 || 0%{?fedora} >= 20
Requires: python3-PyMySQL
%else
Requires: MySQL-python
%endif

%description mariadb
SAL's default MariaDB packages.


%package postgres
Summary:  salpack postgresql package
Group:    Applications/Databases
Provides: %{name}-pgsql = %{version}-%{release}
Requires: %{name}, %{name}-nrpe, postgresql-server
Requires: php-pgsql

%description postgres
SAL's default PostgreSQL packages.


%package mail
Summary:  salpack mail packages
Group:    System Environment/Base
Requires: %{name}, %{name}-mysql-or-mariadb
Requires: postfix, dovecot, mutt
Requires: %{xlocate}
Requires: cyrus-sasl-md5, cyrus-sasl-plain
Requires: postfix-perl-scripts
# SAGATOR
Requires: sagator, clamav-lib, clamav-update, unarj
Requires: spamassassin, perl-Razor-Agent
%if 0%{?rhel} <= 6 || 0%{?fedora} >= 20
Requires: arc, pyzor   
%endif
#Requires: rrdtool-python
#Requires: sagator-release
Requires: logwatch

%description mail
SAL's default mail packages.


%package apache
Summary:  SAL's apache web packages
Group:    System Environment/Base
Provides: %{name}-www = %{version}-%{release}
Provides: %{name}-web = %{version}-%{release}
Obsoletes: %{name}-web <= %{version}-%{release}
Requires: %{name}, %{name}-mysql-or-mariadb
Suggests: salpack-php
Requires: httpd, mod_ssl
Requires: %{xlocate}
Requires: logwatch

%description apache
SAL's default apache web packages.


%package nginx
Summary:  SAL's nginx web packages
Group:    System Environment/Base
Requires: %{name}, %{name}-mysql-or-mariadb
Requires: nginx, php-fpm
Requires: php-cli, php-mbstring, php-mysqli, php-pear, php-gd
Requires: php-xml, php-soap, php-json
Requires: %{xlocate}
Requires: logwatch

%description nginx
SAL's default nginx web packages.


%package php
Summary:  SAL's php packages
Group:    System Environment/Base
Requires: %{name}, %{name}-mysql-or-mariadb
Requires: php, php-mbstring, php-mysqli, php-pear, php-gd
Requires: php-xml, php-soap, php-json
#Requires: phpMyAdmin

%description php
SAL's default php packages.


%package stats
Summary:  SAL's stats packages (nagios)
Group:    System Environment/Base
Requires: %{name}
Requires: httpd, mod_ssl, postfix, telnet, mrtg, rrdtool
%if 0%{?rhel} >= 9
Requires: s-nail
%else
Requires: mailx
%endif
Requires: nagios
Requires: nagios-plugins nagios-plugins-icmp nagios-plugins-dns
Requires: nagios-plugins-users nagios-plugins-udp
Requires: nagios-plugins-ping nagios-plugins-dhcp nagios-plugins-dummy
Requires: nagios-plugins-load nagios-plugins-nagios nagios-plugins-smtp
Requires: nagios-plugins-procs nagios-plugins-http nagios-plugins-disk
Requires: nagios-plugins-mysql nagios-plugins-tcp nagios-plugins-ssh
Requires: nagios-plugins-nrpe
#Requires: nagios-plugins-check_sip
Requires: nagios-plugins-ifoperstatus nagios-plugins-ifstatus
#Requires: trafgrapher

%description stats
SAL's stats packages. Including:
  nagios
  mrtg
  rrdtool


%package nrpe
Summary:  Nagios NRPE daemon and required plugins
Group:    System Environment/Base
Requires: %{name}
Requires: bind-utils
Requires: nrpe nagios-plugins-users nagios-plugins-load
Requires: nagios-plugins-disk nagios-plugins-procs
Requires: nagios-plugins-mailq nagios-plugins-tcp

%description nrpe
Nagios NRPE daemon and required plugins.


%package kvm
Summary:  KVM virtualization (host)
Group:    System Environment/Base
Provides: %{name}-virt = %{version}-%{release}
Provides: %{name}-host = %{version}-%{release}
Requires: %{name}
# qemu-kvm required to define access modes for kvm module
Requires: qemu-kvm
Requires: libvirt, virt-install
Requires: device-mapper-multipath, sg3_utils
#Requires: bridge-utils # not provided on centos8
Requires: acpid
Requires: postfix, iotop, telnet
Requires: %{xlocate}
%if 0%{?rhel} > 7 || 0%{?fedora} > 26
Requires: wireshark-cli
%else
Requires: wireshark
%endif
Requires: ngrep
Requires: virt-top

%description kvm
KVM virtualization server for virtual host system.

%package vm
Summary:  Virtual machine (guest)
Group:    System Environment/Base
Provides: %{name}-guest = %{version}-%{release}
Requires: %{name}
# do not install linux-firmware for guest, it's big and useless
Provides: linux-firmware = 30000000-1
#Obsoletes: linux-firmware < 30000000
#Conflicts: linux-firmware < 30000000

%description vm
Virtualization for virtual machines: guests.


%if 0%{?rhel} > 7 || 0%{?fedora} > 27
%package  python3-turbogears
Summary:  SAL's TurboGears2 dependencies for python3
Group:    Development/Libraries

Provides: %{name}-TurboGears = %{version}-%{release}
Provides: %{name}-TurboGears2 = %{version}-%{release}
Provides: %{name}-turbogears2 = %{version}-%{release}
Provides: %{name}-tg2 = %{version}-%{release}

Requires: %{name}-mysql-or-mariadb
Requires: python3-TurboGears2
Requires: python3-mako
Requires: python3-netaddr
Requires: python3-repoze-tm2, python3-repoze-who
Requires: python3-sqlalchemy >= 1.3.2, python3-alembic
Requires: python3-argon2-cffi
Requires: python3-pwquality
Requires: python3-pytz
Requires: python3-tzlocal
Requires: python3-PyMySQL
Requires: python3-net-snmp
#Requires: python3-qrcode


%description python3-turbogears
SAL's TurboGears2 dependencies for python3.
%endif


%package  ipxe
Summary:  iPXE boot kernel
Group:    System Environment/Base

%description ipxe
iPXE boot kernel

GRUB2 config (/etc/grub.d/40_custom):
menuentry 'PXE boot' --class os {
        insmod part_msdos
        insmod ext2
        set root='(hd0,msdos1)'
        linux16 /boot/ipxe.lkrn
}


%prep
%setup -q -n mypack
# fix python shebangs
%if 0%{?rhel} >= 8 || 0%{?fedora} >= 28
  sed -i 's|^#!/usr/bin/python2|#!/usr/bin/python3|' \
    */*/*.py */*/*/*.py usr/lib/nagios/plugins/check_*
%else
  sed -i 's|^#!/usr/bin/python3|#!/usr/bin/python2|' \
    */*/*.py */*/*/*.py usr/lib/nagios/plugins/check_*
  # use versioned doc for el6 and el7
  sed -i 's|sagator-core|sagator-core-*|' etc/nrpe.d/mail.cfg
%endif

%build
# rename all .py files in bin
for i in usr/bin/*.py; do
  mv -v ${i} ${i%.py}
done
%if 0%{?rhel} >= 6
sed -i 's|/fedora/|/epel/|g' etc/yum.repos.d/salstar.repo
%endif
%if 0%{?rhel} != 7
rm -f etc/yum.repos.d/centos8upgrade.repo
rm -f etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-8 etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8 \
  etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux
%endif
%if 0%{?rhel} != 8
rm -f etc/yum.repos.d/epel9upgrade.repo
%endif
%if 0%{?rhel} != 9
rm -f etc/yum.repos.d/epel10upgrade.repo
%endif

%if 0%{?rhel} <= 8
echo "yum/dnf repo: sagator (old)"
sed -i \
  's|^gpgkey=.*$|gpgkey=%{sagator_gpg_key}|g' \
  etc/yum.repos.d/salstar.repo
%endif
%if 0%{?rhel} == 9
echo "yum/dnf repo: sagator (old) + salstar (new)"
sed -i \
  's|^gpgkey=.*$|gpgkey=%{sagator_gpg_key}\n\t%{salstar_gpg_key}|g' \
  etc/yum.repos.d/salstar.repo 
%endif

%install
rm -rf $RPM_BUILD_ROOT
# 32/64 bit?
if [ -d /usr/lib64 ]; then
  mv usr/lib usr/lib64
  mkdir usr/lib
  mv usr/lib64/systemd usr/lib/
  for i in etc/nrpe.d/*; do
    sed -i 's|/usr/lib/nagios/plugins/|/usr/lib64/nagios/plugins/|g' $i
  done
fi
mkdir -p $RPM_BUILD_ROOT
cp -arf boot etc var usr $RPM_BUILD_ROOT
%if 0%{?rhel}
sed -i 's|/download/mirrors/salstar|/download/mirrors/salstar-centos|' \
  $RPM_BUILD_ROOT/etc/yum.repos.d/salstar.repo
rm -f $RPM_BUILD_ROOT/etc/yum.repos.d/upjs.repo
%endif
# update logwatch script for centos<=6
%if 0%{?rhel} == 6
  sed -i 's/http_rc_detail_rep_/http_rc_detail_rep-/g' \
    $RPM_BUILD_ROOT/etc/logwatch/conf/services/http.conf
%endif
rm -f $RPM_BUILD_ROOT%{_bindir}/*.py[co]


%post
if [ -x /usr/sbin/semanage -a -x /usr/bin/chcon ]; then
  semanage fcontext -a -t rpm_exec_t %{_bindir}/%{dnfyum}-autoupdate 2>/dev/null || :
  chcon -t rpm_exec_t %{_bindir}/%{dnfyum}-autoupdate || :
fi

# do not remove /etc/sysconfig/iptables
%posttrans
if [ -f /etc/sysconfig/iptables.rpmsave ]; then
  if [ ! -f /etc/sysconfig/iptables ]; then
    cp -a /etc/sysconfig/iptables.rpmsave /etc/sysconfig/iptables
  fi
fi

%triggerin nrpe -- cciss_vol_status
chgrp nrpe /usr/bin/cciss_vol_status
chmod 4710 /usr/bin/cciss_vol_status

# restart php after update of imagemagick or graphicsmagick
%if 0%{?fedora} >= 20 || 0%{?rhel} >= 7
%triggerin apache -- GraphicsMagick, php-pecl-gmagick, ImageMagick, php-pecl-imagick
systemctl try-restart php-fpm.service 2> /dev/null
%triggerin nginx -- GraphicsMagick, php-pecl-gmagick, ImageMagick, php-pecl-imagick
systemctl try-restart php-fpm.service 2> /dev/null
%endif

%files
%defattr(-,root,root,-)
%doc %{_datadir}/doc/%{name}/*
%config %{_sysconfdir}/pki/rpm-gpg/*
%config(noreplace) %{_sysconfdir}/yum.repos.d/*.repo
%if 0%{?rhel} >= 7 || 0%{?fedora} >= 16
%exclude %{_sysconfdir}/yum.repos.d/upgradetool.repo
%config(noreplace) %{_sysconfdir}/sysctl.d/salpack-sysrq.conf
%else
%exclude %{_sysconfdir}/sysctl.d
%endif
%if 0%{?fedora} > 0 || 0%{?rhel} >= 8
%exclude %{_sysconfdir}/yum.repos.d/elasticsearch.repo
%exclude %{_sysconfdir}/pki/rpm-gpg/GPG-KEY-elasticsearch
%endif
%config %{_sysconfdir}/sysconfig/iptables.default
%dir %{_sysconfdir}/mrtg
%config(noreplace) %{_sysconfdir}/mrtg/*
%{_var}/www/mrtg/example.html
%exclude %{_sysconfdir}/httpd
%exclude %{_sysconfdir}/nagios
%{_datadir}/logwatch/dist.conf/services/latest-kernel.conf
%{_datadir}/logwatch/scripts/services/latest-kernel
%{_datadir}/bash-completion/completions/salpack-configure-all
%{_bindir}/salpack-functions
%{_bindir}/salpack-configure-all
%{_bindir}/salpack-static-network
%{_bindir}/salpack-configure-firewall
%{_bindir}/salpack-configure-console
%{_bindir}/salpack-tcp-redirect
%{_bindir}/salpack-mailq
%{_bindir}/salpack-maillog
%{_bindir}/salpack-rebuild-md
%{_bindir}/salpack-sync
%{_bindir}/salpack-raid-speed
%{_bindir}/salpack-migrate-users
%{_bindir}/salpack-grub2-fix
%{_bindir}/salpack-scsi-rescan
%{_bindir}/salpack-rpm-by-size
%{_bindir}/salpack-os-upgrade
%{_bindir}/salpack-prepare-le
%{_bindir}/igrep
%{_bindir}/fail_blocker
%{_bindir}/disable_ht_cores
%{_bindir}/inventory-agent
%{_bindir}/ssli
%{_bindir}/restricted-rsync
%{_bindir}/backuppc_size
# conntrack logger
%{_bindir}/conntrack_logger
%if 0%{?rhel} >= 7 || 0%{?fedora} >= 16
%{_unitdir}/conntrack-logger.service
%{_unitdir}/fail-blocker.service
%else
%exclude /usr/lib/systemd
%endif
%dir %{_var}/log/conntrack
# autoupdate
%config(noreplace) %{_sysconfdir}/yum/autoupdate.conf
%{_sysconfdir}/cron.daily/0%{dnfyum}-autoupdate.cron
%{_bindir}/%{dnfyum}-autoupdate
%if "%{dnfyum}" == "dnf"
%exclude %{_sysconfdir}/cron.daily/0yum-autoupdate.cron
%exclude %{_bindir}/yum-autoupdate
%else
%exclude %{_sysconfdir}/cron.daily/0dnf-autoupdate.cron
%exclude %{_bindir}/dnf-autoupdate
%endif
# ssrv
%{_datadir}/bash-completion/completions/ssrv
%{_bindir}/ssrv
# nw
%{_datadir}/bash-completion/completions/nw
%{_bindir}/nw
%{_bindir}/salpack-nw

%files mail
%defattr(-,root,root,-)
%{_bindir}/salpack-delq
%{_bindir}/salpack-shield
%{_bindir}/db4dump
%{_bindir}/db4diff
%{_bindir}/db4passwd

%files mysql
%defattr(-,root,root,-)
%if 0%{?rhel} >= 7 || 0%{?fedora} >= 16
%config(noreplace) %{_sysconfdir}/systemd/system/mysqld.service.d/*
%else
%exclude %{_sysconfdir}/systemd/system/mysqld.service.d
%exclude %{_sysconfdir}/systemd/system/mariadb.service.d
%endif
%{_bindir}/salpack-configure-mysql
%{_bindir}/salpack-mysql-size

%files mariadb
%defattr(-,root,root,-)
%if 0%{?rhel} >= 7 || 0%{?fedora} >= 16
%config(noreplace) %{_sysconfdir}/systemd/system/mariadb.service.d/*
%else
%exclude %{_sysconfdir}/systemd/system/mysqld.service.d
%exclude %{_sysconfdir}/systemd/system/mariadb.service.d
%endif
%{_bindir}/salpack-configure-mysql
%{_bindir}/salpack-mysql-size

%files postgres
%defattr(-,root,root,-)
%{nagios_plugins}/check_pgrepl

%files apache
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/logwatch/conf/services/http.conf
%config(noreplace) %{_sysconfdir}/logrotate.d/virtualhost
#%%config(noreplace) %%{_sysconfdir}/httpd/split-logfile.py
#%%exclude %%{_sysconfdir}/httpd/split-logfile.py[co]
%config(noreplace) %{_sysconfdir}/cron.daily/mod_md
%dir %{_var}/log/apache/_
%{_bindir}/salpack-httpd-kill
%{_bindir}/salpack-acme-tiny
%{_bindir}/squidtop
%{_bindir}/md-message

%files nginx
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/logwatch/conf/services/http.conf
%{_bindir}/salpack-acme-tiny
%{_bindir}/squidtop

%files php

%files stats
%defattr(-,root,root,-)
%{_sysconfdir}/nagios/restart
%dir %{_sysconfdir}/nagios/templates
%{_sysconfdir}/nagios/templates/ngreconfig.py
%config %{_sysconfdir}/nagios/templates/dns
%config %{_sysconfdir}/nagios/templates/global
%config %{_sysconfdir}/nagios/templates/ncpa
%config %{_sysconfdir}/nagios/templates/extinfo
%config(noreplace) %{_sysconfdir}/nagios/extinfo
%config(noreplace) %{_sysconfdir}/nagios/main
%dir %{_sysconfdir}/nagios/salpack
%config(noreplace) %{_sysconfdir}/nagios/salpack/*.cfg
%{nagios_plugins}/check_apcupsd
%{nagios_plugins}/check_httpd
%{nagios_plugins}/check_apache
%{nagios_plugins}/check_lighttpd
%{nagios_plugins}/check_php_fpm
%{nagios_plugins}/check_storwize
%{nagios_plugins}/check_papouch
%{nagios_plugins}/check_json
%{nagios_plugins}/check_ncpa
%{nagios_plugins}/check_snmp_resources
%dir %attr(-, nagios, nagios) %{_var}/log/nagios/httpd-status

%files nrpe
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/nrpe.d/*.cfg
%{_bindir}/sslwatch
%{nagios_plugins}/check_recv_mail
%{nagios_plugins}/check_procs_perf
%{nagios_plugins}/check_eth
%{nagios_plugins}/check_mem
%{nagios_plugins}/check_sa
%exclude %{nagios_plugins}/check_eth.py*
%{nagios_plugins}/check_quota
%{nagios_plugins}/check_sync
%{nagios_plugins}/check_cciss
%{nagios_plugins}/check_hpacucli
%{nagios_plugins}/check_aacraid
%{nagios_plugins}/check_storcli
%{nagios_plugins}/check_diskio
%{nagios_plugins}/check_backuppc
%{nagios_plugins}/check_usbtemp
%{nagios_plugins}/check_gpu
%{nagios_plugins}/check_value
%{nagios_plugins}/check_procfs
%{nagios_plugins}/check_portchannel
%{nagios_plugins}/check_valkey
%{nagios_plugins}/check_elasticsearch
%{nagios_plugins}/check_ssl_certs
%{nagios_plugins}/check_timesync
%{_bindir}/salpack-configure-nrpe

%files kvm
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/sysconfig/virt-backup
%if 0%{?rhel} == 8
%exclude %{_sysconfdir}/sysconfig/libvirt-guests
%else
%config(noreplace) %{_sysconfdir}/sysconfig/libvirt-guests
%endif
%{_datadir}/bash-completion/completions/vs
%config(noreplace) %{_sysconfdir}/sysctl.d/salpack-swappiness.conf
%{_bindir}/virt-backup
%{_bindir}/salpack-autorestart-guests
%{_bindir}/salpack-virsh
%{_bindir}/salpack-vlan-network
%{_bindir}/virsh-*
%{_bindir}/vs

%files vm
%defattr(-,root,root,-)
%{_bindir}/salpack-configure-console

%if 0%{?rhel} > 7 || 0%{?fedora} > 27
%files python3-turbogears
%endif

%files ipxe
%defattr(-,root,root,-)
/boot/*
/var/lib/tftpboot/*

%changelog
* __BUILD_DATE__ Jan ONDREJ (SAL) <ondrejj(at)salstar.sk> - __VERSION__-1
- this changelog is not updated
