%{!?dotests: %define dotests 1}
%global perl_vendorarch_32 %(eval "`%{_bindir}/perl_32 -V:installvendorarch`"; echo $installvendorarch)
%global perl_vendorarch_64 %(eval "`%{_bindir}/perl_64 -V:installvendorarch`"; echo $installvendorarch)
%define perl_version %(eval "`perl -V:version`"; echo $version)

%define _libdir64 %{_libdir}64

Name:           mod_perl
Version:        2.0.11
Release:        2
Summary:        An embedded Perl interpreter for the Apache HTTP Server 2.4.X

Group:          System Environment/Daemons
License:        ASL 2.0
URL:            http://perl.apache.org/
Source0:        http://perl.apache.org/dist/%{name}-%{version}.tar.gz
Source1:        http://perl.apache.org/dist/%{name}-%{version}.tar.gz.asc
Source2:        %{name}.conf
Source3:	%{name}.conf_64

Patch1:		%{name}-2.0.11-testcase.patch 

BuildRequires:  apr-devel >= 1.5.2-1, apr-util-devel >= 1.5.4-1
BuildRequires:  gdbm-devel >= 1.18.1-1
BuildRequires:  httpd-devel >= 2.4.2, httpd-devel < 2.6
BuildRequires:  patch
BuildRequires:  perl >= 5.30.2
Requires:       apr >= 1.5.2-1, apr-util >= 1.5.4-1
Requires:       httpd >= 2.4.43
Requires:       perl >= 5.30.2
Requires:	expat >= 2.2.9
Requires:       openldap >= 2.4.48

%description
Mod_perl incorporates a Perl interpreter into the Apache web server,
so that the Apache web server can directly execute Perl code.
Mod_perl links the Perl runtime library into the Apache web server and
provides an object-oriented Perl interface for Apache's C language
API. The end result is a quicker CGI script turnaround process, since
no external Perl interpreter has to be started.

Install mod_perl if you're installing the Apache web server and you'd
like for it to directly incorporate a Perl interpreter.

The mod_perl package also contains the files needed for building XS
modules that use mod_perl.


%prep
%setup -q
export PATH=/opt/freeware/bin:$PATH
rm -rf   /tmp/%{name}-%{version}-32bit
cp -pr . /tmp/%{name}-%{version}-32bit
rm -fr *
mv       /tmp/%{name}-%{version}-32bit 32bit
cp -pr 32bit 64bit
%patch1 -p0

%build
export MP_USE_MY_EXTUTILS_EMBED=1

# build on 64bit mode
cd 64bit
export OBJECT_MODE=64
LDDLFLAGS="-Wl,-b64 -Wl,-bhalt:4 -Wl,-G   -Wl,-bnoentry -lpthreads -lc -lm -L/opt/freeware/lib64 -L/usr/lib64 -L/opt/freeware/lib -L/usr/lib -Wl,-berok -Wl,-bexpall"
MODPERL_LDDLFLAGS="-Wl,-b64 -Wl,-bhalt:4 -Wl,-G  -Wl,-bnoentry -lpthreads -lc -lm -L/opt/freeware/lib64 -L/usr/lib64 -L/opt/freeware/lib -L/usr/lib -Wl,-berok -Wl,-bexpall"
%{_bindir}/perl_64bit Makefile.PL \
    PREFIX=${RPM_BUILD_ROOT}%{_prefix} \
    INSTALLDIRS=vendor \
    MP_APXS=%{_bindir}/apxs_64 \
    MP_APR_CONFIG=%{_bindir}/apr-1-config_64 \
    MP_APU_CONFIG=%{_bindir}/apu-1-config_64
gmake LDDLFLAGS="$LDDLFLAGS" MODPERL_LDDLFLAGS="$MODPERL_LDDLFLAGS"

if [ "%{dotests}" == 1 ]
then
    (gmake -k test PERL=%{_bindir}/perl_64bit || true)
fi

# build on 32bit mode
cd ../32bit
export OBJECT_MODE=32
LDDLFLAGS="-Wl,-bhalt:4 -Wl,-G -Wl,-bnoentry -lpthreads -lc -lm -L/opt/freeware/lib -L/usr/lib -Wl,-berok -Wl,-bexpall"
MODPERL_LDDLFLAGS="-Wl,-bhalt:4 -Wl,-G -Wl,-bnoentry -lpthreads -lc -lm -L/opt/freeware/lib -L/usr/lib -Wl,-berok -Wl,-bexpall"
%{_bindir}/perl_32 Makefile.PL \
    PREFIX=${RPM_BUILD_ROOT}%{_prefix} \
    INSTALLDIRS=vendor \
    MP_APXS=%{_bindir}/apxs \
    MP_APR_CONFIG=%{_bindir}/apr-1-config \
    MP_APU_CONFIG=%{_bindir}/apu-1-config
gmake LDDLFLAGS="$LDDLFLAGS" MODPERL_LDDLFLAGS="$MODPERL_LDDLFLAGS"

if [ "%{dotests}" == 1 ]
then
    (gmake -k test PERL=%{_bindir}/perl_32 || true)
fi

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

export MP_USE_MY_EXTUTILS_EMBED=1

# install on 64bit mode
cd 64bit
export OBJECT_MODE=64
make install \
    MODPERL_AP_LIBEXECDIR=${RPM_BUILD_ROOT}%{_libdir64}/httpd/modules \
    MODPERL_AP_INCLUDEDIR=${RPM_BUILD_ROOT}%{_includedir}/httpd \
    DESTINSTALLVENDORMAN3DIR=${RPM_BUILD_ROOT}%{_mandir}/man3

# Remove the temporary files
find ${RPM_BUILD_ROOT} -type f -name .packlist -exec rm -f {} ';'
find ${RPM_BUILD_ROOT} -type f -name perllocal.pod -exec rm -f {} ';'
find ${RPM_BUILD_ROOT} -type d -depth -exec rmdir {} 2>/dev/null ';'

# Fix permissions to avoid strip failures on non-root builds.
chmod -R u+w ${RPM_BUILD_ROOT}/*

(
    cd  ${RPM_BUILD_ROOT}/%{_prefix}/bin
    for fic in $(ls -1| grep -v -e _32 -e _64)
    do
	mv $fic "$fic"_64
    done
)

# install on 32bit mode
cd ../32bit
export OBJECT_MODE=32
make install \
    MODPERL_AP_LIBEXECDIR=${RPM_BUILD_ROOT}%{_libdir}/httpd/modules \
    MODPERL_AP_INCLUDEDIR=${RPM_BUILD_ROOT}%{_includedir}/httpd \
    DESTINSTALLVENDORMAN3DIR=${RPM_BUILD_ROOT}%{_mandir}/man3

# Remove the temporary files
find ${RPM_BUILD_ROOT} -type f -name .packlist -exec rm -f {} ';'
find ${RPM_BUILD_ROOT} -type f -name perllocal.pod -exec rm -f {} ';'
find ${RPM_BUILD_ROOT} -type d -depth -exec rmdir {} 2>/dev/null ';'

# Fix permissions to avoid strip failures on non-root builds.
chmod -R u+w ${RPM_BUILD_ROOT}/*

# Install the config file
mkdir -p ${RPM_BUILD_ROOT}%{_sysconfdir}/httpd/conf/extra
cp %{SOURCE2} ${RPM_BUILD_ROOT}%{_sysconfdir}/httpd/conf/extra/httpd-%{name}.conf
cp %{SOURCE3} ${RPM_BUILD_ROOT}%{_sysconfdir}/httpd/conf/extra/httpd-%{name}.conf_64
chmod 0644 ${RPM_BUILD_ROOT}%{_sysconfdir}/httpd/conf/extra/*.conf

/usr/bin/strip -X32_64 ${RPM_BUILD_ROOT}%{_bindir}/* || :


%preun
if [ "$1" = 0 ]; then
    cat %{_sysconfdir}/httpd/conf/httpd.conf | \
      grep -v "# mod_perl settings" | \
      grep -v "Include conf/extra/httpd-mod_perl.conf" \
      > %{_sysconfdir}/httpd/conf/tmp_httpd.conf
    mv -f %{_sysconfdir}/httpd/conf/tmp_httpd.conf %{_sysconfdir}/httpd/conf/httpd.conf
    cat %{_sysconfdir}/httpd/conf/httpd.conf_64 | \
      grep -v "# mod_perl settings" | \
      grep -v "Include conf/extra/httpd-mod_perl.conf_64" \
      > %{_sysconfdir}/httpd/conf/tmp_httpd.conf_64
    mv -f %{_sysconfdir}/httpd/conf/tmp_httpd.conf_64 %{_sysconfdir}/httpd/conf/httpd.conf_64
fi
    echo "Please restart your web server using: '/opt/freeware/sbin/apachectl restart'"


%post
if [ "$1" = 1 ]; then
cat %{_sysconfdir}/httpd/conf/httpd.conf | \
  grep -v "# mod_perl settings" | \
  grep -v "Include conf/extra/httpd-mod_perl.conf" \
  > %{_sysconfdir}/httpd/conf/tmp_httpd.conf
mv -f %{_sysconfdir}/httpd/conf/tmp_httpd.conf %{_sysconfdir}/httpd/conf/httpd.conf
echo "# mod_perl settings" >> %{_sysconfdir}/httpd/conf/httpd.conf
echo "Include conf/extra/httpd-mod_perl.conf" >> %{_sysconfdir}/httpd/conf/httpd.conf
cat %{_sysconfdir}/httpd/conf/httpd.conf_64 | \
  grep -v "# mod_perl settings" | \
  grep -v "Include conf/extra/httpd-mod_perl.conf_64" \
  > %{_sysconfdir}/httpd/conf/tmp_httpd.conf_64
mv -f %{_sysconfdir}/httpd/conf/tmp_httpd.conf_64 %{_sysconfdir}/httpd/conf/httpd.conf_64
echo "# mod_perl settings" >> %{_sysconfdir}/httpd/conf/httpd.conf_64
echo "Include conf/extra/httpd-mod_perl.conf_64" >> %{_sysconfdir}/httpd/conf/httpd.conf_64
fi
echo "Please restart your web server using: '/opt/freeware/sbin/apachectl restart'"


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


%files
%defattr(-,root,system,-)
%doc 32bit/Changes 32bit/LICENSE 32bit/README* 32bit/STATUS 32bit/SVN-MOVE 32bit/docs/
%config(noreplace) %{_sysconfdir}/httpd/conf/extra/*.conf*
%{_bindir}/*
%{_includedir}/httpd/*
%{_libdir}/httpd/modules/*
%{_libdir64}/httpd/modules/*
%{perl_vendorarch_32}/*
%{perl_vendorarch_64}/*
%{_mandir}/man3/*


%changelog
* Thu Jul 2 2020 Reshma V Kumar <reskumar@in.ibm.com> - 2.0.11-2
- Build with perl 5.30.2
- Build with gcc

* Mon Mar 23 2020 Reshma V Kumar <reskumar@in.ibm.com> - 2.0.11-1
- Initial build for AIX Toolbox

* Tue Jun 19 2012 Michael Perzl <michael@perzl.org> - 2.0.7-1
- updated to version 2.0.7

* Tue Jun 19 2012 Michael Perzl <michael@perzl.org> - 2.0.6-1
- first version for AIX V5.1 and higher
