%global with_python3 0

%if 0%{?fedora} >= 30 || 0%{?rhel} >= 8
# Python v2 Support dropped
%global with_python3 1
%endif

Name:           nzbget
Version:        21.1
Release:        3%{?dist}
Summary:        An Efficient Usenet Downloader
Group:          System Environment/Daemons
License:        GPLv2+
URL:            https://nzbget.net
Source0:        https://github.com/nzbget/nzbget/releases/download/v%{version}/nzbget-%{version}-src.tar.gz
Source1:        nzbget.systemd
Source2:        nzbget.xml
Source3:        nzbget-secure.xml

# Useful NZBGetScripts
# pulled from https://github.com/clinton-hall/GetScripts/
Source4:       ResetDateTime.py
Source5:       DeleteSamples.py
Source6:       Flatten.py
Source7:       PasswordList.py
# This scan-script reads category information from nzb-file 
# (if it's present in meta-tag) and sets the category for nzb-file
# when adding to queue.
# Author: Hugbug (src: https://forum.nzbget.net/viewtopic.php?f=8&t=2999)
Source8:       MetaCategory.py

Source9:       Completion.py

# By default the paths are hard-coded to reference tools that are natively
# not available in Fedora/RedHat as is.  For example:
#   7z -> /usr/bin/7za
#   unrar -> /usr/bin/unrar
#
# This patch also updates the default configuration file to just work out of the
# box by already referencing the paths the nzbget source files, templates and
# working directories can be found.
Patch0:         nzbget-default.paths.patch

# Compiler Warning
Patch1:       nzbget-size_t-warning-fix.patch
# OpenSSL v3 Support
Patch2:       nzget-openssl-build-fix.patch

BuildRequires: gcc-c++
BuildRequires: libxml2-devel
BuildRequires: openssl-devel
BuildRequires: libpar2-devel
BuildRequires: ncurses-devel
BuildRequires: systemd

%if 0%{?fedora} >= 30
BuildRequires: systemd-rpm-macros
%endif

#to fix the line endings in nzbget
BuildRequires:  dos2unix

%if 0%{?rhel} && 0%{?rhel} <= 7
# C++11 Support
BuildRequires: autoconf
BuildRequires: libtool
BuildRequires: automake
%endif

# For user control
Requires(pre): shadow-utils

# Dependencies after install
Requires: unrar
Requires: p7zip

%description
NZBGet is a cross-platform binary newsgrabber for nzb files, written in C++.
It supports client/server mode, automatic par-check/-repair and web-interface.
NZBGet requires low system resources and runs great on routers,
NAS-devices and media players.

%prep
%setup -q -n %{name}-%{version}
# Better Red Hat Environment Support
%patch0 -p1

# Typecasting correct size response
%patch1 -p1

%if 0%{?fedora} >= 33 || 0%{?rhel} >= 8
# OpenSSL v3 Support
%patch2 -p1
%endif

# The following gracefully handles default paths with respect to the OS
# the RPM was built for
sed -i -e 's|{_datadir}|%{_datadir}|g' nzbget.conf
sed -i -e 's|{_datadir}|%{_datadir}|g' daemon/main/Options.cpp
sed -i -e 's|{_datadir}|%{_datadir}|g' %{SOURCE1}
sed -i -e 's|{_sysconfdir}|%{_sysconfdir}|g' nzbget.conf
sed -i -e 's|{_sysconfdir}|%{_sysconfdir}|g' daemon/main/Options.cpp
sed -i -e 's|{_sysconfdir}|%{_sysconfdir}|g' %{SOURCE1}
sed -i -e 's|{_sharedstatedir}|%{_sharedstatedir}|g' nzbget.conf
sed -i -e 's|{_sharedstatedir}|%{_sharedstatedir}|g' daemon/main/Options.cpp
sed -i -e 's|{_sharedstatedir}|%{_sharedstatedir}|g' %{SOURCE1}
sed -i -e 's|{_localstatedir}|%{_localstatedir}|g' nzbget.conf
sed -i -e 's|{_localstatedir}|%{_localstatedir}|g' daemon/main/Options.cpp
sed -i -e 's|{_localstatedir}|%{_localstatedir}|g' %{SOURCE1}
sed -i -e 's|{_bindir}|%{_bindir}|g' nzbget.conf
sed -i -e 's|{_bindir}|%{_bindir}|g' daemon/main/Options.cpp
sed -i -e 's|{_bindir}|%{_bindir}|g' %{SOURCE1}

# fix-up python references; this effectively changes
# #!/usr/bin/env python
#  to:
# #!/usr/bin/python3 (if python 3)
# #!/usr/bin/python2 (if python 2)
%if 0%{?with_python3}
find scripts -type f -name '*.py' -exec \
   sed -i -e 's|#!.*env python[23]\?$|#!/usr/bin/python3|g' {} \;
for file in %{SOURCE4} %{SOURCE5} %{SOURCE6} %{SOURCE7} %{SOURCE8} %{SOURCE9}; do
   sed -i -e 's|#!.*env python[23]\?$|#!/usr/bin/python3|g' $file
done
%else
find scripts -type f -name '*.py' -exec \
   sed -i -e 's|#!.*env python[23]\?$|#!/usr/bin/python2|g' {} \;
for file in %{SOURCE4} %{SOURCE5} %{SOURCE6} %{SOURCE7} %{SOURCE8} %{SOURCE9}; do
   sed -i -e 's|#!.*env python[23]\?$|#!/usr/bin/python2|g' $file
done
%endif

%build
# symbolic link required
ln -sfn scripts ppscripts

%if 0%{?rhel} && 0%{?rhel} <= 7
# C++11 Support
export CXXFLAGS="-std=c++11 -O2"
%configure --with-tlslib=OpenSSL \
   --docdir=%{_docdir}/%{name}-%{version} \
   --disable-cpp-check
%else
%configure --with-tlslib=OpenSSL \
   --docdir=%{_docdir}/%{name}-%{version}
%endif
%make_build

%install
%make_install

# Create a global configuration file from sample already
install -p -D -m 644 %{SOURCE1} \
                %{buildroot}/%{_unitdir}/nzbget.service
install -p -D -m 664 %{buildroot}%{_datadir}/%{name}/nzbget.conf \
                %{buildroot}/%{_sysconfdir}/nzbget.conf

# remove old RC Script
rm -f %{buildroot}/%{_sbindir}/%{name}d
mv -f ppscripts %{buildroot}%{_datadir}/%{name}

mkdir -p %{buildroot}/%{_sharedstatedir}/%{name}
mkdir -p %{buildroot}/%{_sharedstatedir}/%{name}/nzb
mkdir -p %{buildroot}/%{_sharedstatedir}/%{name}/queue
mkdir -p %{buildroot}/%{_sharedstatedir}/%{name}/tmp
mkdir -p %{buildroot}/%{_sharedstatedir}/%{name}/inter
mkdir -p %{buildroot}/%{_sharedstatedir}/%{name}/downloads

# Logging
mkdir -p %{buildroot}/var/log/%{name}

# Scripts
mkdir -p %{buildroot}/%{_datadir}/%{name}/scripts
install -p -m 755 scripts/EMail.py \
   %{buildroot}/%{_datadir}/%{name}/scripts/EMail.py
install -p -m 755 scripts/Logger.py \
   %{buildroot}/%{_datadir}/%{name}/scripts/Logger.py


# Extra Useful Scripts
install -p -D -m 755 %{SOURCE4} \
   %{buildroot}/%{_datadir}/%{name}/scripts/ResetDateTime.py
install -p -D -m 755 %{SOURCE5} \
   %{buildroot}/%{_datadir}/%{name}/scripts/DeleteSamples.py
install -p -D -m 755 %{SOURCE6} \
   %{buildroot}/%{_datadir}/%{name}/scripts/Flatten.py
install -p -D -m 755 %{SOURCE7} \
   %{buildroot}/%{_datadir}/%{name}/scripts/PasswordList.py
install -p -D -m 755 %{SOURCE8} \
   %{buildroot}/%{_datadir}/%{name}/scripts/MetaCategory.py
install -p -D -m 755 %{SOURCE9} \
   %{buildroot}/%{_datadir}/%{name}/scripts/Completion.py

# Firewalld Support
%{__install} -D -m 644 %{SOURCE2} \
                %{buildroot}/%{_sysconfdir}/firewalld/services/nzbget.xml
%{__install} -D -m 644 %{SOURCE3} \
                %{buildroot}/%{_sysconfdir}/firewalld/services/nzbget-secure.xml

%pre
# Create ourselves a user to daemonize our application against
getent group nzbget >/dev/null || groupadd -r nzbget
getent passwd nzbget >/dev/null || \
    useradd -r -g nzbget -d %{_datadir}/%{name} -s /sbin/nologin \
    -c "NZBget account" nzbget || :

%post
%systemd_post nzbget.service

%preun
%systemd_preun nzbget.service

%postun
%systemd_postun_with_restart nzbget.service

%files
%doc ChangeLog README
%license COPYING

# Passwords are stored in this file; intentionally made as having no rwx
# permissions for 'other' users
%attr(0660,nzbget,root) %config(noreplace) %{_sysconfdir}/nzbget.conf
%attr(0755,nzbget,nzbget) %dir %{_sharedstatedir}/%{name}
%attr(0775,nzbget,nzbget) %dir %{_sharedstatedir}/%{name}/nzb
%attr(0755,nzbget,nzbget) %dir %{_sharedstatedir}/%{name}/inter
%attr(0755,nzbget,nzbget) %dir %{_sharedstatedir}/%{name}/queue
%attr(0755,nzbget,nzbget) %dir %{_sharedstatedir}/%{name}/tmp
%attr(0775,nzbget,nzbget) %dir %{_sharedstatedir}/%{name}/downloads
# 770 because invalid usernames and passwords are printed in clear text in
# this log file
%attr(0770,nzbget,root) %dir /var/log/%{name}

%{_bindir}/%{name}*
%{_unitdir}/nzbget.service
%{_datadir}/%{name}

%{_sysconfdir}/firewalld/services/nzbget.xml
%{_sysconfdir}/firewalld/services/nzbget-secure.xml

%if 0%{?fedora} >= 21 || 0%{?rhel} >= 8
%{_defaultdocdir}/*
%endif

%changelog
* Sun Sep 04 2022 Chris Caron <lead2gold@gmail.com> - 21.1-3
- added 6 useful scripts as part of packaging

* Sun Sep 04 2022 Chris Caron <lead2gold@gmail.com> - 21.1-2
- Updated NZBGet webui Configuration Path

* Tue Aug 30 2022 Chris Caron <lead2gold@gmail.com> - 21.1-1
- Updated to NZBGet v21.1

* Sun Sep 06 2020 Chris Caron <lead2gold@gmail.com> - 21.0-2
- Updated to include firewalld configuration

* Fri May 03 2019 Chris Caron <lead2gold@gmail.com> - 21.0-1
- Initial install of NZBget v21.0