%global _hardened_build 1

# Filter provides
# %%global __provides_exclude_from ^%%{_libdir}/%%{name}/.*$

%define pregenerated_dhparam_files 1

Name:           zproxy
Version:        0.3.7
Release:        2%{?dist}
Summary:        A high-performance reverse proxy

%global commit 2e0ec7d15417f0058bdeda41ca5259b7a62b14c1
%global gittag v0.3.7
%global shortcommit %(c=%{commit}; echo ${c:0:7})

License:        AGPLv3
URL:            https://github.com/zevenet/zproxy
Source0:        https://github.com/zevenet/zproxy/archive/%{commit}/%{name}-%{shortcommit}.tar.gz
Source1:        zproxy.service
Source2:        zproxy.sysconfig
Source3:        zproxy.cfg.sample
Source4:        README.Fedora
Patch0:         zproxy-fix-missing-lz.patch
Patch1:         zproxy-fix-dhparam-path.patch

# These files are used to speed up the compilation during my tests
Source101:      dh512.h
Source102:      dh2048.h
Source103:      dh2048.pem

# We are adding a group for zproxy
Requires(pre):  shadow-utils 

#Requires:       
BuildRequires:  gcc-c++ cmake openssl-devel zlib-devel pcre-devel libmodsecurity-devel
# For the documentation?
#BuildRequires:  doxygen
# To generate the dh2048.pem:
BuildRequires:  openssl
# For the service unit macro:
BuildRequires:  systemd
# For the tests:
# Note: we are currently skipping tests as they need network.
#BuildRequires:  git

BuildRequires:  systemd-rpm-macros

# We are splitting the library into it's own package, without 
#Requires:       %%{name}-libs%%{?_isa} = %%{version}-%%{release}

%description
Zevenet zproxy is a high-performance multi-threaded and event-driven L7 reverse
proxy and load balancer inspired by Pound reverse proxy simplicity.

zproxy main features:

* HTTP, HTTPS handling
* Pound load balancer configuration file compatibility.
* Managed by REST API requests in JSON format.
* Load balancing algorithms: Round Robin, Least Connections, Response Time,
  Pending Connections
* Connection pinning.
* Backend output traffic marking.
* Simple HTTP Caching - WIP
* Pound control interface like binary (zproxyctl)

%package libs
Summary: The libl7pcore shared library from zproxy

%description libs
The libl7pcore library from zproxy. I'm not sure if it could be useful outside
of zproxy itself, but... here it is!

%prep
%autosetup -n %{name}-%{commit} -p 1
%{__cp} %{SOURCE4} .

# Use pregenerated dhparam files for speeding up the compile during my tests
# Perhaps this should not be used when producing the final RPM
%if 0%{?pregenerated_dhparam_files} > 0
  %{__cp} %{SOURCE101} src/config/
  %{__cp} %{SOURCE102} src/config/
  %{__cp} %{SOURCE103} src/config/
  export CMAKE_CCACHE="-D CMAKE_CXX_COMPILER_LAUNCHER=ccache -D CMAKE_C_COMPILER_LAUNCHER=ccache"
%endif

%build

mkdir build && cd build

#%%cmake -D BUILD_TESTS=off -D CMAKE_CXX_COMPILER_LAUNCHER=ccache -D CMAKE_C_COMPILER_LAUNCHER=ccache ..
%cmake -D BUILD_TESTS=off  ..

# Why is CMake creating another subdir? For multi-platform compilations?
cd redhat-linux-build

/usr/bin/make -O -j2 V=1 VERBOSE=1

%make_build


%install

cd build/redhat-linux-build
%make_install

# The library

install -d -m 755 $RPM_BUILD_ROOT%{_libdir}/%{name}
install -p -m 755 src/libl7pcore.so $RPM_BUILD_ROOT%{_libdir}/%{name}/libl7pcore.so

# Add the systemd service file
mkdir -p %{buildroot}/%{_unitdir}
cp -p %{SOURCE1}    %{buildroot}/%{_unitdir}/

# Add the environment file
install -Dp -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/sysconfig/%{name}

# Add a sample configuration
install -Dp -m 0644 %{SOURCE3} %{buildroot}%{_sysconfdir}/%{name}/zproxy.cfg.sample

%ldconfig_scriptlets

%files
%license LICENSE
%doc README.md README.Fedora VERSION benchmark/zproxy.cfg

%dir %attr(0750, root, zproxy) /etc/zproxy

%config(noreplace) /etc/zproxy/dh2048.pem
%config(noreplace) %{_sysconfdir}/sysconfig/%{name}

# The sample config could change if the future versions change the config syntax
%config /etc/zproxy/zproxy.cfg.sample

   /usr/bin/zproxy
   /usr/bin/zproxyctl
   /usr/lib/systemd/system/zproxy.service
   /usr/share/man/man8/zproxy.8.gz
   /usr/share/man/man8/zproxyctl.8.gz

   #/usr/lib/debug/usr/bin/zproxy-1.0.2-1.fc30.x86_64.debug
   #/usr/lib/debug/usr/bin/zproxyctl-1.0.2-1.fc30.x86_64.debug

%files libs
   %{_libdir}/%{name}/libl7pcore.so

%pre
getent group zproxy >/dev/null || groupadd -r zproxy 

%post
%systemd_post %{name}.service

%preun
%systemd_preun %{name}.service

%postun
%systemd_postun_with_restart %{name}.service

%changelog

* Sun Jan 09 2022 Bussi Andrea <btabta at gmail.com> - 0.3.7-2
- WIP: Do not expose the private library as a 'provides'.
- Handle systemd reloads pre/post installation and restart of service after updates.
- Optionally speed up compilation by using precompiled dhparam files. 
- Add the environment file for passing options to zproxy.
- Add a sample configuration file.
- Add a README.Fedora.

* Sat Jan 08 2022 Bussi Andrea <btabta at gmail.com> - 0.3.7-1
- The upstream project version went back to lower numbers; I need to look if I have
  to change the epoch for this package (tecnically, it is not released yet).
- Updated to the latest tag in the git repo.

* Thu Nov  7 2019 Bussi Andrea <btabta at gmail.com> - 1.0.2-1
- Initial version
-