%bcond_with dotests

%global __tar /opt/freeware/bin/tar

%global py_major 3.9

%global py_minor 16

%global python_major python%{py_major}

%global meta_package python3-%{name}

%global python3_sitearch %(%{python_major} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(0))")

%global python3_sitearch64  %(%{python_major} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")

%define _libdir64 %{_prefix}/lib64

%{!?_httpd_modconfdir: %{expand: %%global _httpd_modconfdir %%{_sysconfdir}/httpd/conf.d}}
%{!?_httpd_moddir:    %{expand: %%global _httpd_moddir    %%{_libdir}/httpd/modules}}
%{!?_httpd_moddir64:    %{expand: %%global _httpd_moddir64    %%{_libdir64}/httpd/modules}}

Name:           mod_wsgi
Version:        5.0.0
Release:        1
Summary:        A WSGI interface for Python web applications in Apache
License:        ASL 2.0
URL:            https://modwsgi.readthedocs.io/
Source0:        https://github.com/GrahamDumpleton/mod_wsgi/archive/%{version}.tar.gz#/mod_wsgi-%{version}.tar.gz
Source1:        wsgi-python3.conf
Source2:	wsgi-python3.conf_64
Patch0:		mod_wsgi-4.7.1-aix.patch

BuildRequires:  httpd-devel
BuildRequires:  gcc

%description
The mod_wsgi adapter is an Apache module that provides a WSGI compliant
interface for hosting Python based web applications within Apache. The
adapter is written completely in C code against the Apache C runtime and
for hosting WSGI applications within Apache has a lower overhead than using
existing WSGI adapters for mod_python or CGI.

%package -n %{python_major}-%{name}
Summary:        %summary
Requires:       httpd >= 2.4.43
BuildRequires: %{python_major}-devel >= %{py_major}.%{py_minor}
Requires: apr >= 1.7.0
Requires: apr-util >= 1.6.1
Requires: %{python_major} >= %{py_major}.%{py_minor}

Provides: mod_wsgi = %{version}-%{release}

%description -n %{python_major}-%{name}
The mod_wsgi adapter is an Apache module that provides a WSGI compliant
interface for hosting Python based web applications within Apache. The
adapter is written completely in C code against the Apache C runtime and
for hosting WSGI applications within Apache has a lower overhead than using
existing WSGI adapters for mod_python or CGI.

%package -n %{meta_package}
Summary:       Meta-package for %{name}

Requires:      python3 >= %{py_major}.%{py_minor}
Requires:      gettext >= 0.21
Requires:      libgcc >= 10.3.0
Requires:      %{python_major}-%{name} = %{version}-%{release}

%description -n %{meta_package}
This is a Meta-package for %{name} package.

%prep
%setup -q -n %{name}-%{version}

%patch0 -p0

rm -rf   /tmp/%{name}-%{version}-32bit
cp -pr . /tmp/%{name}-%{version}-32bit
rm -fr *
mv       /tmp/%{name}-%{version}-32bit 32bit
cp -pr 32bit 64bit


%build

export PATH="/opt/freeware/bin:$PATH"
export CC="gcc"
export PKG_CONFIG_PATH=""

# build on 64bit mode
cd 64bit
export OBJECT_MODE=64
export CFLAGS="-maix64 -O2"
export LDFLAGS="-maix64 -L/opt/freeware/lib64 -L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib64:/opt/freeware/lib:/usr/lib:/lib"
export APXS=/opt/freeware/bin/apxs_64

./configure --prefix=%{_prefix} --with-apxs=/opt/freeware/bin/apxs_64 --with-python=%{python_major}
gmake
%{python_major} setup.py build

# build on 32bit mode
cd ../32bit
export OBJECT_MODE=32

export CFLAGS="-maix32 -O2 -D_LARGE_FILES"
export LDFLAGS="-L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib:/usr/lib:/lib -Wl,-bmaxdata:0x80000000"
export APXS=/opt/freeware/bin/apxs

./configure --prefix=%{_prefix} --with-apxs=/opt/freeware/bin/apxs --with-python=%{_libexecdir}/%{python_major}_32
gmake
%{_libexecdir}/%{python_major}_32 setup.py build

%install
[ "${RPM_BUILD_ROOT}" != "/" ] && rm -rf ${RPM_BUILD_ROOT}

# install on 64bit mode
cd 64bit
export OBJECT_MODE=64
make install DESTDIR=$RPM_BUILD_ROOT LIBEXECDIR=%{_httpd_moddir64}
mv  $RPM_BUILD_ROOT%{_httpd_moddir64}/mod_wsgi.so $RPM_BUILD_ROOT%{_httpd_moddir64}/mod_wsgi_python3.so

install -d -m 755 $RPM_BUILD_ROOT%{_httpd_modconfdir}
# httpd >= 2.4.x
install -p -m 644 %{SOURCE2} $RPM_BUILD_ROOT%{_httpd_modconfdir}/10-wsgi-python3.conf_64

%{python_major} setup.py install --skip-build --root ${RPM_BUILD_ROOT}
cd $RPM_BUILD_ROOT%{_bindir}
mv mod_wsgi-express mod_wsgi-express-3.9
ln -sf mod_wsgi-express-3.9 mod_wsgi-express-3
cd -

# install on 32bit mode
cd ../32bit
export OBJECT_MODE=32
make install DESTDIR=$RPM_BUILD_ROOT LIBEXECDIR=%{_httpd_moddir}
mv  $RPM_BUILD_ROOT%{_httpd_moddir}/mod_wsgi.so $RPM_BUILD_ROOT%{_httpd_moddir}/mod_wsgi_python3.so

install -d -m 755 $RPM_BUILD_ROOT%{_httpd_modconfdir}
# httpd >= 2.4.x
install -p -m 644 %{SOURCE1} $RPM_BUILD_ROOT%{_httpd_modconfdir}/10-wsgi-python3.conf

%{_libexecdir}/%{python_major}_32 setup.py install --skip-build --root ${RPM_BUILD_ROOT}


%post -n %{meta_package} 
if [ "$1" = "1" ]; then
echo "Include conf.d/10-wsgi-python3.conf" >> %{_prefix}/etc/httpd/conf/httpd.conf
echo "Include conf.d/10-wsgi-python3.conf_64" >> %{_prefix}/etc/httpd/conf/httpd.conf_64
fi

%preun -n %{meta_package}
if [ "$1" = "0" ]; then
/usr/bin/cat %{_sysconfdir}/httpd/conf/httpd.conf | \
/usr/bin/sed -e "s|Include conf.d/10-wsgi-python3.conf||" \
> %{_sysconfdir}/httpd/conf/tmp_httpd.conf
/usr/bin/mv -f %{_sysconfdir}/httpd/conf/tmp_httpd.conf %{_sysconfdir}/httpd/conf/httpd.conf

/usr/bin/cat %{_sysconfdir}/httpd/conf/httpd.conf_64| \
/usr/bin/sed -e "s|Include conf.d/10-wsgi-python3.conf_64||" \
> %{_sysconfdir}/httpd/conf/tmp_httpd.conf_64
/usr/bin/mv -f %{_sysconfdir}/httpd/conf/tmp_httpd.conf_64 %{_sysconfdir}/httpd/conf/httpd.conf_64
fi


%files -n %{python_major}-%{name}
%doc 32bit/CREDITS.rst 32bit/README.rst
%{python3_sitearch}/*
%{python3_sitearch64}/*
%{_bindir}/mod_wsgi-express-3.9

%files -n python3-%{name}
%defattr(-,root,system,-)
%config(noreplace) %{_httpd_modconfdir}/*wsgi-python3.conf*
%{_httpd_moddir}/mod_wsgi_python3.so
%{_httpd_moddir64}/mod_wsgi_python3.so
%{_bindir}/mod_wsgi-express-3

%changelog
* Thu Dec 07 2023 Aditya Kamath <Aditya.Kamath1@ibm.com> - 5.0.0-1
- Mod_wsgi CVE fix  CVE-2022-2255 

* Sat Apr 01 2023 Reshma V Kumar <reskumar@in.ibm.com> - 4.7.1-2
- Mod_wsgi for python3.9

* Thu Oct 08 2020 Reshma V Kumar <reskumar@in.ibm.com> - 4.7.1-1
- Initial port for AIX Toolbox
