%{!?dotests: %define dotests 1}
%define name python3-sphinxcontrib-applehelp
%define srcname sphinxcontrib-applehelp
%define version 1.0.1
%define release 1

%define is_python %(test -e /usr/bin/python3_32 && echo 1 || echo 0)
%if %{is_python}
%define python_sitelib %(python3_32 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")
%endif

%define _libdir64 %{_prefix}/lib64

%define is_python_64 %(test -e /usr/bin/python3 && echo 1 || echo 0)
%if %{is_python_64}
%define python_sitelib64 %(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")
%endif

Summary: Sphinx extension for Apple help books
Name: %{name}
Version: %{version}
Release: %{release}
Source0: %{srcname}-%{version}.tar.gz
Url:     http://sphinx-doc.org/
License: BSD
Group: Development/Libraries
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot
Prefix: %{_prefix}
BuildArch: noarch

BuildRequires: gettext >= 0.19.8.1-4
BuildRequires: python3 >= 3.7.4-1
BuildRequires: python3-devel >= 3.7.4-1
BuildRequires: python3-sphinx >= 2.3.0-1
BuildRequires: python3-pytest >= 5.3.1-1

Requires: python3 >= 3.7.4-1

%description
sphinxcontrib-applehelp is a sphinx extension which outputs Apple help books.

%prep
%setup -q -n %{srcname}-%{version}
find -name '*.mo' -delete


%build
export OBJECT_MODE=64
for po in $(find -name '*.po'); do
  msgfmt --output-file=${po%.po}.mo ${po}
done

python3 setup.py build


%install
[ -d "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT

python3 setup.py install --skip-build --root ${RPM_BUILD_ROOT}

# Move language files to /opt/freeware/share
cd %{buildroot}%{python_sitelib}
for lang in `find sphinxcontrib/applehelp/locales -maxdepth 1 -mindepth 1 -type d -not -path '*/\.*' -printf "%f "`;
do
  test $lang == __pycache__ && continue
  install -d %{buildroot}%{_datadir}/locale/$lang/LC_MESSAGES
  mv sphinxcontrib/applehelp/locales/$lang/LC_MESSAGES/*.mo %{buildroot}%{_datadir}/locale/$lang/LC_MESSAGES/
done
rm -rf sphinxcontrib/applehelp/locales
ln -s %{_datadir}/locale sphinxcontrib/applehelp/locales

cd -
%find_lang sphinxcontrib.applehelp


%if %{dotests}
%check
python3 -m pytest || true 
%endif


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

%files -f sphinxcontrib.applehelp.lang
%defattr(-,root,system,-)
%doc README.rst LICENSE
%{python_sitelib}/*

%changelog
* Mon Dec 30 2019 Reshma V Kumar <reskumar@in.ibm.com> -1.0.1-1
- Initial build for AIX Toolbox
