#
%define nginx_user nginx
%define nginx_group nginx

%if "%%MODULE_CONFIGURE_ARGS%%" == ""
%define META_MODULE 1
%else
%define META_MODULE 0
%endif

%if 0%{?rhel} || 0%{?amzn}
%define _group System Environment/Daemons
%if 0%{?amzn} >= 2
BuildRequires: openssl11-devel
%else
BuildRequires: openssl-devel
%endif
%endif

%if 0%{?suse_version} >= 1315
%define _group Productivity/Networking/Web/Servers
BuildRequires: libopenssl-devel
%define _debugsource_template %{nil}
%endif

%if (0%{?rhel} == 7) && (0%{?amzn} == 0)
%define dist .el7
%endif

%if 0%{?rhel} == 8
%define _debugsource_template %{nil}
%endif

%if 0%{?fedora}
%define _debugsource_template %{nil}
%global _hardened_build 1
%endif

%%MODULE_DEFINITIONS%%

%define base_version %%BASE_VERSION%%
%if "x%%BASE_SUFFIX%%" != "x"
%define base_suffix %%BASE_SUFFIX%%
%endif
%define base_release %%BASE_RELEASE%%%{?dist}.ngx
%define plus_version %%PLUS_VERSION%%

%define bdir %{_builddir}/%{name}-%{base_version}

Summary: %%SUMMARY%%
Name: %%NAME%%
Version: %%VERSION_PREFIX%%%%VERSION%%
Release: %%RELEASE%%%{?dist}.ngx
Vendor: %%PACKAGE_VENDOR%%
URL: %%MODULE_PACKAGE_URL%%
Group: %{_group}

%if %{META_MODULE} == 1
BuildArch: noarch
%endif

Source0: nginx%{?base_suffix}-%{base_version}.tar.gz
Source1: %%COPYRIGHT_SOURCE%%
%%MODULE_SOURCES%%

%%MODULE_PATCHES%%

License: %%MODULE_PACKAGE_LICENSE%%

BuildRoot: %{_tmppath}/%{name}-%{base_version}-%{base_release}-root
BuildRequires: zlib-devel
BuildRequires: pcre2-devel
Requires: nginx-plus-r%{plus_version}
Provides: %{name}-r%{plus_version}

%description
NGINX Plus: %%SHORT_SUMMARY%%.

%if 0%{?suse_version}
%debug_package
%endif

%define WITH_CC_OPT $(echo %{optflags} $(pcre2-config --cflags))
%define WITH_LD_OPT -Wl,-z,relro -Wl,-z,now

%define BASE_CONFIGURE_ARGS $(echo "%%BASE_CONFIGURE_ARGS%%")
%define MODULE_CONFIGURE_ARGS $(echo "%%MODULE_CONFIGURE_ARGS%%")

%prep
%setup -qcTn %{name}-%{base_version}
%if %{META_MODULE} == 0
	tar --strip-components=1 -zxf %{SOURCE0}
	if ! test -e configure ; then ln -s auto/configure ./ ; fi
%endif
%%MODULE_PREP_SOURCES%%
%%MODULE_PREP_PATCHES%%

%build
%%MODULE_PREBUILD%%

%if %{META_MODULE} == 0
	cd %{bdir}
	%%MODULE_ENV%%
	./configure %{BASE_CONFIGURE_ARGS} %{MODULE_CONFIGURE_ARGS} \
		--with-cc-opt="%{WITH_CC_OPT} %%MODULE_CC_OPT_DEBUG%%" \
		--with-ld-opt="%{WITH_LD_OPT} %%MODULE_LD_OPT_DEBUG%%" \
		--with-debug
	make %{?_smp_mflags} modules
	for so in `find %{bdir}/objs/ -type f -name "*.so"`; do
	debugso=`echo $so | sed -e 's|\.so$|-debug.so|'`
	mv $so $debugso
	done
	%%MODULE_ENV%%
	./configure %{BASE_CONFIGURE_ARGS} %{MODULE_CONFIGURE_ARGS} \
		--with-cc-opt="%{WITH_CC_OPT} %%MODULE_CC_OPT%%" \
		--with-ld-opt="%{WITH_LD_OPT} %%MODULE_LD_OPT%%"
	make %{?_smp_mflags} modules
%endif

%check
%{__rm} -rf $RPM_BUILD_ROOT/usr/src
cd %{bdir}
grep -v 'usr/src' debugfiles.list > debugfiles.list.new && mv debugfiles.list.new debugfiles.list
cat /dev/null > debugsources.list
%if 0%{?suse_version} >= 1500
cat /dev/null > debugsourcefiles.list
%endif

%install
cd %{bdir}
%{__rm} -rf $RPM_BUILD_ROOT
%{__mkdir} -p $RPM_BUILD_ROOT%{_datadir}/doc/%%NAME%%
%{__install} -m 644 -p %{SOURCE1} \
    $RPM_BUILD_ROOT%{_datadir}/doc/%%NAME%%/COPYRIGHT

%%MODULE_PREINSTALL%%

%if %{META_MODULE} == 0
	%{__mkdir} -p $RPM_BUILD_ROOT%{_libdir}/nginx/modules
	for so in `find %{bdir}/objs/ -maxdepth 1 -type f -name "*.so"`; do
	%{__install} -m755 $so \
	   $RPM_BUILD_ROOT%{_libdir}/nginx/modules/
	done
%endif

%clean
%{__rm} -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root)
%if %{META_MODULE} == 0
%{_libdir}/nginx/modules/*
%endif
%dir %{_datadir}/doc/%%NAME%%
%{_datadir}/doc/%%NAME%%/*
%%MODULE_FILES%%

%pre

%post
if [ $1 -eq 1 ]; then
%%MODULE_POST%%
fi

%preun

%postun

%changelog
