# By default, dotests are run.
# No tests: rpmbuild -ba --without dotests *.spec
%bcond_without dotests

# default dependencies
%global hawkey_version 0.39.1
%global hawkey_version_dy 0.39.1-32_50
%global libcomps_version 0.1.15
%global libcomps_version_dy 0.1.15-101
%global libmodulemd_version 1.5.2
%global libmodulemd_version_dy 1.5.2-100
%global gpgme_version 1.13.1
%global gpgme_version_dy 1.13.1-100
%global rpm_version 4.15.1

# conflicts
%global conflicts_dnf_plugins_core_version 4.0.12
%global conflicts_dnf_plugins_extras_version 4.0.4
%global conflicts_dnfdaemon_version 0.3.19

%global python3_version 3.9

%bcond_without with_python3

# YUM compat subpackage configuration
#
# level=full    -> deploy all compat symlinks (conflicts with yum < 4)
# level=minimal -> deploy a subset of compat symlinks only
#                  (no conflict with yum >= 3.4.3-505)*
# level=preview -> minimal level with altered paths (no conflict with yum < 4)
# *release 505 renamed /usr/bin/yum to /usr/bin/yum-deprecated
%global yum_compat_level minimal
%global yum_subpackage_name yum
%if 0%{?fedora}
    # Avoid file conflict with yum < 4 in all Fedoras
    # It can be resolved by pretrans scriptlet but they are not recommended in Fedora
    %global yum_compat_level minimal
    %if 0%{?fedora} < 31
        # Avoid name conflict with yum < 4
        %global yum_subpackage_name %{name}-yum
    %endif
%endif
%if 0%{?rhel} && 0%{?rhel} <= 7
    %global yum_compat_level preview
    %global yum_subpackage_name nextgen-yum4
%endif

# paths
%global confdir %{_sysconfdir}/%{name}
%global pluginconfpath %{confdir}/plugins

%define is_python %(test -e /opt/freeware/libexec/python%{python3_version}_32 && echo 1 || echo 0)
%if %{is_python}
  %define python3_sitelib %(/opt/freeware/libexec/python%{python3_version}_32 -c "from distutils.sysconfig import get_python_lib; print (get_python_lib())")
  %global py3pluginpath %{python3_sitelib}/%{name}-plugins
%endif

# Use the same directory of the main package for subpackage licence and docs
%global _docdir_fmt %{name}

%global pkg_summary     Package manager
%global pkg_description Utility that allows users to manage packages on their systems. \
It supports RPMs, modules and comps groups & environments.

%bcond_with with_python2
%bcond_without with_python3

Name:           dnf
Version:        4.2.17
#Release:        0.2beta
Release:        32_51
Summary:        %{pkg_summary}
# For a breakdown of the licensing, see PACKAGE-LICENSING
License:        GPLv2+ and GPLv2 and GPL
URL:            https://github.com/rpm-software-management/dnf
Source0:        %{url}/archive/%{version}/%{name}-%{version}.zip
Source1:	update_pkg_yumdb
# This change is not approved in F30 (https://fedoraproject.org/wiki/Changes/DNF_Better_Counting)
#Patch0001:      0001-Remove-docs-for-countme-and-user_agent-options.patch
Patch0:		dnf-4.2.17-aix.patch
Patch1:		dnf-4.2.17-etc-paths.patch	
Patch2:		dnf-4.2.17-dnfconf.patch
# python looks for libraries in it's  search path irrespective of search path given in
# the module being used (_rpm.so in this case).
# This is to force python to find correct libraries (32 or 64-bit) and in correct path.
# If this isn't forced then import might fail.
Patch3:         dnf-4.2.17-dnf_libpath_32.patch

#BuildArch:      noarch
BuildRequires:  cmake >= 3.22.0
BuildRequires:  gettext >= 0.20.2
# Documentation
#BuildRequires:  systemd
BuildRequires:  bash-completion >= 2.11
%if %{with with_python3}
#BuildRequires:  %{_bindir}/sphinx-build-3
Requires:       python3-%{name} = %{version}-%{release}
%else
BuildRequires:  %{_bindir}/sphinx-build
Requires:       python2-%{name} = %{version}-%{release}
%endif
%if %{with with_python3}
Recommends:     (python3-dbus if NetworkManager)
%endif
Obsoletes: yum >= 4.2.17-32_1

Provides:       dnf-command(alias)
Provides:       dnf-command(autoremove)
Provides:       dnf-command(check-update)
Provides:       dnf-command(clean)
Provides:       dnf-command(distro-sync)
Provides:       dnf-command(downgrade)
Provides:       dnf-command(group)
Provides:       dnf-command(history)
Provides:       dnf-command(info)
Provides:       dnf-command(install)
Provides:       dnf-command(list)
Provides:       dnf-command(makecache)
Provides:       dnf-command(mark)
Provides:       dnf-command(provides)
Provides:       dnf-command(reinstall)
Provides:       dnf-command(remove)
Provides:       dnf-command(repolist)
Provides:       dnf-command(repoquery)
Provides:       dnf-command(repository-packages)
Provides:       dnf-command(search)
Provides:       dnf-command(updateinfo)
Provides:       dnf-command(upgrade)
Provides:       dnf-command(upgrade-to)
Conflicts:      python3-dnf-plugins-core < %{conflicts_dnf_plugins_core_version}
Conflicts:      python3-dnf-plugins-extras < %{conflicts_dnf_plugins_extras_version}

%description
%{pkg_description}

%package data
Summary:        Common data and configuration files for DNF
#Requires:       libreport-filesystem
Obsoletes:      %{name}-conf <= %{version}-%{release}
Provides:       %{name}-conf = %{version}-%{release}

%description data
Common data and configuration files for DNF

#%package -n %{yum_subpackage_name}
#Requires:       %{name} = %{version}-%{release}
#Requires:       %{name}-data = %{version}-%{release}
#Requires:	python3 >= 3.7.12
#Summary:        %{pkg_summary}
#Provides:       %{name}-yum = %{version}-%{release}
#Obsoletes:      %{name}-yum < 5

#%description -n %{yum_subpackage_name}
#%{pkg_description}

%if %{with with_python3}
%package -n python%{python3_version}-%{name}
Summary:        Python 3 interface to DNF
%{?python_provide:%python_provide python3-%{name}}
BuildRequires:  python%{python3_version}-devel >= %{python3_version}.13
BuildRequires:  python3-hawkey >= %{hawkey_version}
BuildRequires:  python3-libdnf >= %{hawkey_version}
BuildRequires:  python3-libcomps >= %{libcomps_version}
BuildRequires:  python3-libdnf
BuildRequires:  libmodulemd >= %{libmodulemd_version}
Requires:       libmodulemd >= %{libmodulemd_version_dy}
#BuildRequires:  python3-nose
BuildRequires:  python3-gpg >= %{gpgme_version}
Requires:       python%{python3_version}-gpg >= %{gpgme_version_dy}
Requires:       %{name}-data = %{version}-%{release}
Requires:       python%{python3_version}-hawkey >= %{hawkey_version_dy}
Requires:       python%{python3_version}-libdnf >= %{hawkey_version_dy}
Requires:       python%{python3_version}-libcomps >= %{libcomps_version_dy}
Requires:       python%{python3_version}-libdnf >= %{hawkey_version_dy}
BuildRequires:  rpm-python3 >= %{rpm_version}
Requires:       rpm-python%{python3_version} >= %{rpm_version}-32_2
Requires:	python%{python3_version} >= %{python3_version}.15
Recommends:     python3-unbound
Recommends:     rpm-plugin-systemd-inhibit

%description -n python%{python3_version}-%{name}
Python 3 interface to DNF.
%endif

%if %{with with_python3}
%package -n python3-%{name}
Requires:	python%{python3_version}-%{name} = %{version}-%{release}
Requires:	python3 >= %{python3_version}.15
Summary:        Meta package forPython 3 interface to DNF

%description -n python3-%{name}
Meta package for Python 3 interface to DNF.
%endif

%package automatic
Summary:        %{pkg_summary} - automated upgrades
#BuildRequires:  systemd
Requires:       %{name} = %{version}-%{release}
#%{?systemd_requires}

%description automatic
Systemd units that can periodically download package upgrades and apply them.


%prep
%autosetup -p1
mkdir build-py2
mkdir build-py3


%build
echo "yum_compat_level : %{yum_compat_level}"
echo ""
echo "python3_version   : %{python3_version}"
echo "python3_sitelib  : %{python3_sitelib}"
echo "py3pluginpath    : %{py3pluginpath}"


%if %{with with_python3}
     cd build-py3
     cmake .. -DPYTHON_EXECUTABLE=/opt/freeware/libexec/python%{python3_version}_32 \
        -DPYTHON_INCLUDE_DIR=/opt/freeware/include/python%{python3_version} \
        -DPYTHON_LIBRARY=/opt/freeware/lib/libpython%{python3_version}.a \
        -DPYTHON_INCLUDE_PATH=/opt/freeware/include/python%{python3_version} \
        -DENABLE_DOCS=OFF -DCMAKE_INSTALL_PREFIX:PATH=/opt/freeware \
        -DPYTHON_DESIRED="3" -DPYTHON_VERSION_MAJOR="3"

     gmake VERBOSE=1
%endif


%install
%if %{with with_python3}
    cd build-py3
    gmake install DESTDIR=${RPM_BUILD_ROOT}
    cd ${RPM_BUILD_ROOT}/%{_prefix}
    #mkdir etc
    mkdir -p etc/yum.repos.d
    mkdir -p etc/yum/repos.d
    #popd
%endif

%find_lang %{name}
mkdir -p %{buildroot}%{confdir}/vars
mkdir -p %{buildroot}%{confdir}/aliases.d
mkdir -p %{buildroot}%{pluginconfpath}/
mkdir -p %{buildroot}%{_sysconfdir}/%{name}/modules.d
mkdir -p %{buildroot}%{_sysconfdir}/%{name}/modules.defaults.d
%if %{with with_python3}
mkdir -p %{buildroot}%{py3pluginpath}/__pycache__/
%endif
mkdir -p %{buildroot}%{_localstatedir}/log/
mkdir -p %{buildroot}/var/cache/dnf/
touch %{buildroot}%{_localstatedir}/log/%{name}.log
%if %{with with_python3}
#ln -sr %{buildroot}%{_bindir}/dnf-3 %{buildroot}%{_bindir}/dnf
mv %{buildroot}%{_bindir}/dnf-automatic-3 %{buildroot}%{_bindir}/dnf-automatic
%else
ln -sf %{buildroot}%{_bindir}/dnf-2 %{buildroot}%{_bindir}/dnf
mv %{buildroot}%{_bindir}/dnf-automatic-2 %{buildroot}%{_bindir}/dnf-automatic
%endif
rm -vf %{buildroot}%{_bindir}/dnf-automatic-*

# Strict conf distribution
rm -vf %{buildroot}%{confdir}/%{name}-strict.conf

# YUM compat layer
%if %{with with_python3}
#ln -sf  %{buildroot}%{confdir}/%{name}.conf %{buildroot}%{_sysconfdir}/yum.conf
# Don't yet provide the yum symlink pointing to dnf.
cd %{buildroot}%{_bindir}
ln -sf  dnf-3 yum
ln -sf  dnf-3 yum4
ln -sf  dnf-3 dnf
# Create /usr/bin/yum soft link to /opt/freeware/bin/yum
#cd %{buildroot}
#mkdir -p usr/bin
#cd usr/bin
#ln -s ../..%{_bindir}/yum yum

%else
%if "%{yum_compat_level}" == "preview"
ln -sr  %{buildroot}%{_bindir}/dnf-2 %{buildroot}%{_bindir}/yum4
ln -sr  %{buildroot}%{_mandir}/man8/dnf.8.gz %{buildroot}%{_mandir}/man8/yum4.8.gz
rm -f %{buildroot}%{_mandir}/man8/yum.8.gz
%else
ln -sr  %{buildroot}%{_bindir}/dnf-2 %{buildroot}%{_bindir}/yum
%endif
%endif
%if "%{yum_compat_level}" == "full"
mkdir -p %{buildroot}%{_sysconfdir}/yum
ln -sr  %{buildroot}%{pluginconfpath} %{buildroot}%{_sysconfdir}/yum/pluginconf.d
ln -sr  %{buildroot}%{confdir}/protected.d %{buildroot}%{_sysconfdir}/yum/protected.d
ln -sr  %{buildroot}%{confdir}/vars %{buildroot}%{_sysconfdir}/yum/vars
%endif

cd %{buildroot}
mkdir -p %{buildroot}%{_prefix}/sbin
cp %{SOURCE1} %{buildroot}%{_prefix}/sbin

%check
%if %{without dotests}
echo "*** Skipping tests"
exit 0
%endif # without dotests

%if %{with with_python3}
cd build-py3
( ctest -VV || true )
%endif

%pre
/usr/bin/rpm -qa | grep "^dnf-[0-9].*" > /dev/null 2>&1
if [[ $? -ne 0 ]]
then
    echo "The proper way to install dnf is by running dnf_aixtoolbox.sh script."
    echo "Please check the DNF forum https://community.ibm.com/community/user/power/blogs/sangamesh-mallayya1/2021/05/28/dnf-is-now-available-on-aix-toolbox?CommunityKey=10c1d831-47ee-4d92-a138-b03f7896f7c9"
    echo "For information on configuring DNF on AIX using dnf_aixtoolbox.sh script."
    exit 1
fi

#%pre -n %{yum_subpackage_name}
#if [[ "$1" = "2" ]]
#then
#    if [[ -f /opt/freeware/etc/yum/yum.conf ]]
#    then

#        /usr/bin/echo ""
#	/usr/bin/echo "This version of yum4 is a soft link to dnf and all the yum commands are dnf commands."
#        /usr/bin/echo "Repositories present in yum.conf is being used for dnf."
#        /usr/bin/echo ""
#
#	/usr/bin/cp /opt/freeware/etc/dnf/dnf.conf /opt/freeware/etc/dnf/dnf.conf.yum3
#	/usr/bin/cp /opt/freeware/etc/yum/yum.conf /opt/freeware/etc/dnf/dnf.conf
#
#	dnf_conf=`/usr/bin/ls /opt/freeware/etc/dnf/dnf.conf`
#	/usr/bin/sed 's|\/var\/cache\/yum|\/var\/cache\/dnf|1' $dnf_conf > ${dnf_conf}_tmp
#	/usr/bin/mv ${dnf_conf}_tmp $dnf_conf
#	/usr/bin/sed 's|\/var\/log\/yum.log|\/var\/log\/dnf.log|1' $dnf_conf > ${dnf_conf}_tmp
#	/usr/bin/mv ${dnf_conf}_tmp $dnf_conf
#	/usr/bin/sed  's|\obsoletes=1||1' $dnf_conf > ${dnf_conf}_tmp
#	/usr/bin/mv ${dnf_conf}_tmp $dnf_conf
#	/usr/bin/perl -i -pe "BEGIN{undef $/;} s/^exactarch=1$/exactarch=1\ngpgcheck=1/sgm" $dnf_conf
#	/usr/bin/perl -i -pe "BEGIN{undef $/;} s/^gpgcheck=1$/gpgcheck=1\ninstallonly_limit=3/sgm" $dnf_conf
#	/usr/bin/perl -i -pe "BEGIN{undef $/;} s/^installonly_limit=3$/installonly_limit=3\nclean_requirements_on_remove=True/sgm" $dnf_conf
#	/usr/bin/perl -i -pe "BEGIN{undef $/;} s/^clean_requirements_on_remove=True$/clean_requirements_on_remove=True\nbest=True/sgm" $dnf_conf
#	/usr/bin/perl -i -pe "BEGIN{undef $/;} s/^best=False$/best=False\nskip_if_unavailable=True/sgm" $dnf_conf
#    fi
#
#    # Check if installed yum is 3.4.3 which is required to run update_pkg_yumdb 
#    /usr/bin/rpm -qa | grep yum-3.4.3 > /dev/null 2>&1
#    if [[ $? -eq 0 ]]
#    then
#        /usr/bin/echo "" 
#        /usr/bin/echo "Trying to create pkg_yumdb database required for migrate plugin"
#        /opt/freeware/bin/python2 /opt/freeware/sbin/update_pkg_yumdb

#        if [[ $? -ne 0 ]]
#        then
#            /usr/bin/echo "Failed to create pkg_yumdb database"
#        else
#            /usr/bin/echo "Successfully created pkg_yumdb database"
#        fi 
#    fi
#fi

%post data
if [[ "$1" = "1" ]]
then
    # Create releasever variable fils in /opt/freeware/etc/dnf/vars path
    # This is to avoid Unable to detect release version warning
    release_ver=`/usr/bin/lslpp -qLc bos.rte | /usr/bin/cut -d: -f3`
    touch /opt/freeware/etc/dnf/vars/releasever
    echo $release_ver > /opt/freeware/etc/dnf/vars/releasever

    osver=`/usr/bin/oslevel`
    dnf_conf=`ls /opt/freeware/etc/dnf/dnf.conf`
    if [[ $osver = "7.1.0.0" ]]
    then
        /usr/bin/echo "[AIX_Toolbox_71]" >> $dnf_conf
        /usr/bin/echo "name=AIX 7.1 specific repository" >> $dnf_conf
        /usr/bin/echo "baseurl=https://anonymous:anonymous@public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc-7.1/" >> $dnf_conf
        /usr/bin/echo "enabled=1" >> $dnf_conf
        /usr/bin/echo "gpgcheck=0" >> $dnf_conf
        /usr/bin/echo "" >> $dnf_conf
    elif [[ $osver = "7.2.0.0" ]]
    then
        /usr/bin/echo "[AIX_Toolbox_72]" >> $dnf_conf
        /usr/bin/echo "name=AIX 7.2 specific repository" >> $dnf_conf
        /usr/bin/echo "baseurl=https://anonymous:anonymous@public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc-7.2/" >> $dnf_conf
        /usr/bin/echo "enabled=1" >> $dnf_conf
        /usr/bin/echo "gpgcheck=0" >> $dnf_conf
        /usr/bin/echo "" >> $dnf_conf
    else
        echo "No additional repositories were added"
    fi
fi

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



%files
%defattr(-,root,system,-)
%{_bindir}/%{name}
%{_bindir}/yum
#%{_bindir}/%{name}-*
#%exclude %{_bindir}/yum
#%dir %{_datadir}/bash-completion
#%dir %{_datadir}/bash-completion/completions
%{_datadir}/bash-completion/completions/%{name}
#%{_mandir}/man8/%{name}.8*
#%{_mandir}/man8/yum2dnf.8*
#%{_mandir}/man7/dnf.modularity.7*
#%{_unitdir}/%{name}-makecache.service
#%{_unitdir}/%{name}-makecache.timer
/var/cache/%{name}/

%files data
%defattr(-,root,system,-)
%license COPYING PACKAGE-LICENSING
%doc AUTHORS README.rst
%dir %{confdir}
%dir %{confdir}/modules.d
%dir %{confdir}/modules.defaults.d
%dir %{pluginconfpath}
#%dir %{confdir}/protected.d
%dir %{confdir}/vars
%dir %{confdir}/aliases.d
%dir %{_prefix}/etc/yum.repos.d
%ghost %dir %{_prefix}/etc/yum/repos.d
%dir %{_prefix}/etc/dnf
#%exclude %{confdir}/aliases.d/zypper.conf
%config(noreplace) %{confdir}/%{name}.conf
%config(noreplace) %{confdir}/protected.d/%{name}.conf
%config(noreplace) %{confdir}/logrotate.d/%{name}
%ghost %attr(644,-,-) /var/log/hawkey.log
%ghost %attr(644,-,-) /var/log/%{name}.log
%ghost %attr(644,-,-) /var/log/%{name}.librepo.log
%ghost %attr(644,-,-) /var/log/%{name}.rpm.log
%ghost %attr(644,-,-) /var/log/%{name}.plugin.log
%ghost %attr(755,-,-) %dir /var/lib/%{name}
%ghost %attr(644,-,-) /var/lib/%{name}/groups.json
%ghost %attr(755,-,-) %dir /var/lib/%{name}/yumdb
%ghost %attr(755,-,-) %dir /var/lib/%{name}/history
#%{_mandir}/man5/%{name}.conf.5*
%{_libdir}/tmpfiles.d/%{name}.conf
%{_sysconfdir}/libreport/events.d/collect_dnf.conf
%attr (755, root, system) %{_prefix}/sbin/update_pkg_yumdb

#%files -n %{yum_subpackage_name}
#%defattr(-,root,system,-)
#%if "%{yum_compat_level}" == "full"
#%{_bindir}/yum
#%{confdir}/yum.conf
#%{confdir}/yum/pluginconf.d
#%{confdir}/yum/protected.d
#%{confdir}/yum/vars
#%{_mandir}/man8/yum.8*
#%{_mandir}/man5/yum.conf.5.*
##%{_mandir}/man8/yum-shell.8*
#%{_mandir}/man1/yum-aliases.1*
#%config(noreplace) %{confdir}/protected.d/yum.conf
#%else
##%exclude %{_sysconfdir}/yum.conf
##%exclude %{_sysconfdir}/yum/pluginconf.d
##%exclude %{_sysconfdir}/yum/protected.d
##%exclude %{_sysconfdir}/yum/vars
##%exclude %{confdir}/protected.d/yum.conf
##%exclude %{_mandir}/man5/yum.conf.5.*
##%exclude %{_mandir}/man8/yum-shell.8*
##%exclude %{_mandir}/man1/yum-aliases.1*
#%endif

#%if "%{yum_compat_level}" == "minimal"
#%{_bindir}/yum
##/usr/bin/yum
##%{_mandir}/man8/yum.8*
#%endif

#%if "%{yum_compat_level}" == "preview"
#%{_bindir}/yum4
#%{_mandir}/man8/yum4.8*
#%exclude %{_mandir}/man8/yum.8*
#%endif

%if %{with with_python3}
%files -n python%{python3_version}-%{name}
%defattr(-,root,system,-)
%exclude %{python3_sitelib}/%{name}/automatic
%{python3_sitelib}/%{name}/
%dir %{py3pluginpath}
%dir %{py3pluginpath}/__pycache__
%endif

%if %{with with_python3}
%files -n python3-%{name}
%defattr(-,root,system,-)
%{_bindir}/%{name}-3
%endif

%files automatic
%defattr(-,root,system,-)
%{_bindir}/%{name}-automatic
#%config(noreplace) %{confdir}/automatic.conf
#%{_mandir}/man8/%{name}-automatic.8*
#%{_unitdir}/%{name}-automatic.service
#%{_unitdir}/%{name}-automatic.timer
#%{_unitdir}/%{name}-automatic-notifyonly.service
#%{_unitdir}/%{name}-automatic-notifyonly.timer
#%{_unitdir}/%{name}-automatic-download.service
#%{_unitdir}/%{name}-automatic-download.timer
#%{_unitdir}/%{name}-automatic-install.service
#%{_unitdir}/%{name}-automatic-install.timer
%if %{with with_python3}
%{python3_sitelib}/%{name}/automatic/
%endif

%changelog
* Tue Jan 24 2023 Sangamesh Mallayya <smallayy@in.ibm.com> - 4.2.17-32_51
- Rebuild with python3.9.

* Thu Apr 21 2022 Sangamesh Mallayya <smallayy@in.ibm.com> - 4.2.17-32_50
- Use Release as 100 so that 32 & 64-bit static dependencies can be
- update to this dynamic version.
- Build it as ppc instead of noarch so that it has to be uploaded in
- AIX version specific paths.

* Sat Jul 10 2021 Sangamesh Mallayya <smallayy@in.ibm.com> - 4.2.17-32_1
- Rebuild with release name change to 32_1 to have better clarity between
-  32 & 64-bit dnf rpm packages.
- Don't provide /usr/bin/yum with yum4.
 
- Also add pre install check if older dnf without 32_1 is installed.
- Needs to remove this check if some fix is been made.
  
* Mon Feb 10 2020 Sangamesh Mallayya <smallayy@in.ibm.com> - 4.2.17-1
- Initial port for AIX with python3. Adopted from the rhel.
