%define srcname docutils
%define elisppkgname rst-el
%define prereq_ge()  %(LC_ALL="C" rpm -q --queryformat 'Requires(post,preun):%%{NAME} >= %%{VERSION}' %1| grep -v "is not")

Summary: an open-source text processing system written in Python
Summary(ja): Pythonで書かれたテキスト処理システム
Name: python-%{srcname}
Version: 0.16
Release: 1%{?_dist_release}
Group: programming
Vendor: Project Vine
Distribution: Vine Linux
Packager: shaolin, munepi, owa

License: Public Domain and MIT and Python and GPLv3+
URL: https://docutils.sourceforge.net/
Source0: https://prdownloads.sourceforge.net/%{srcname}/%{srcname}-%{version}.tar.gz
## for rst-mode
Source1: %{elisppkgname}-install.sh
Source2: %{elisppkgname}-remove.sh
Source3: vine-default-%{elisppkgname}.el
Source4: %{elisppkgname}-init.el

BuildArch: noarch
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root

BuildRequires: python-rpm-macros
BuildRequires: python-devel
BuildRequires: python2-rpm-macros
BuildRequires: python-setuptools
Requires: python

%{?python_provide:%python_provide python2-%{srcname}}
Provides: docutils = %{version}-%{release}
Obsoletes: docutils <= 0.5

%description
Docutils is an open-source text processing system for processing 
plaintext documentation into useful formats, such as HTML or LaTeX.
It includes reStructuredText, the easy to read, easy to use,
what-you-see-is-what-you-get plaintext markup language.

%description -l ja
Docutilsはオープンソースのテキスト処理システムで、プレーンテキストの
文書をHTMLやLaTeXなどの使いやすいフォーマットに変換するものです。
読みやすく使いやすくWYSIWYGなプレーンテキストのマークアップ言語である
reStructuresTextを含んでいます。


%package -n python3-%{srcname}
Summary: an open-source text processing system written in Python
Summary(ja): Pythonで書かれたテキスト処理システム
Group: programming

BuildRequires: python3-devel
BuildRequires: python3-rpm-macros
BuildRequires: python3-setuptools
Requires: python3
%{?python_provide:%python_provide python3-%{srcname}}

%description -n python3-%{srcname}
Docutils is an open-source text processing system for processing 
plaintext documentation into useful formats, such as HTML or LaTeX.
It includes reStructuredText, the easy to read, easy to use,
what-you-see-is-what-you-get plaintext markup language.

%description -n python3-%{srcname} -l ja
Docutilsはオープンソースのテキスト処理システムで、プレーンテキストの
文書をHTMLやLaTeXなどの使いやすいフォーマットに変換するものです。
読みやすく使いやすくWYSIWYGなプレーンテキストのマークアップ言語である
reStructuresTextを含んでいます。


%package -n %{elisppkgname}
Summary: Emacs support for reStructuredText
Summary(ja): reStructuredText の Emacs サポート
Group: publishing
Requires:     emacsen
Requires(post,preun):       emacsen
%prereq_ge    emacsen-common

%description -n %{elisppkgname}
Emacs support for reStructuredText.

%description -l ja -n %{elisppkgname}
reStructuredText の Emacs サポート


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

# Remove shebang from library files
sed -i -e '/#! *\/usr\/bin\/.*/{1D}' $(grep -Erl '^#!.+python' %{srcname})

iconv -f ISO88592 -t UTF8 tools/editors/emacs/IDEAS.rst > tmp
mv tmp tools/editors/emacs/IDEAS.rst

# We want the licenses but don't need this build file
rm -f licenses/docutils.conf

# https://bugzilla.redhat.com/show_bug.cgi?id=1687377
rm test/test_writers/test_odt.py


%build
%py3_build
%py2_build

%install
%__rm -rf $RPM_BUILD_ROOT

%py2_install
%py3_install

# Flash file is used for testing docutils but shouldn't be in the installed package.
mv docs/user/rst/images/biohazard.swf ./biohazard.swf 

# docutils setup.py runs 2to3 on a copy of the tests and puts it in sitelib.
rm -rf %{buildroot}%{python3_sitelib}/test

for file in %{buildroot}/%{_bindir}/*.py; do
  mv $file `dirname $file`/`basename $file .py`
done

## for Emacs package
mkdir -p %{buildroot}%{_datadir}/emacs/site-lisp/%{elisppkgname}/packages
mkdir -p %{buildroot}%{emacsen_pkgdir}/install
mkdir -p %{buildroot}%{emacsen_pkgdir}/remove

#
# install el files
#
mv tools/editors/emacs/rst.el %{buildroot}%{_datadir}/emacs/site-lisp/%{elisppkgname}
cp -p %{SOURCE3} %{SOURCE4} %{buildroot}%{_datadir}/emacs/site-lisp/%{elisppkgname}

#
# install script (bytecompile el and install elc , remove)
#
%_installemacsenscript %{elisppkgname} %{SOURCE1}
%_removeemacsenscript  %{elisppkgname} %{SOURCE2}


%check
mv  biohazard.swf docs/user/rst/images/biohazard.swf
python test/alltests.py
python3 test/alltests.py
rm docs/user/rst/images/biohazard.swf


%clean
rm -rf $RPM_BUILD_ROOT


%post -n %{elisppkgname}
#
# bytecompile and install
#
if [ "$1" = 2 ]; then
%_emacsenPackageRemove %{elisppkgname}
fi
%_addemacsenlist %{elisppkgname}
%_emacsenPackageInstall %{elisppkgname}

%preun -n %{elisppkgname}
if [ "$1" = 0 ]; then
%_emacsenPackageRemove %{elisppkgname}
%_removeemacsenlist %{elisppkgname}
fi



%files
%defattr(-,root,root)
%license COPYING.txt licenses/*
%doc BUGS.txt FAQ.txt HISTORY.txt README.txt RELEASE-NOTES.txt 
%doc THANKS.txt docs tools/editors
%{python_sitelib}/%{srcname}/
%{python_sitelib}/docutils-*.egg-info

%files -n python3-%{srcname}
%defattr(-,root,root)
%license COPYING.txt licenses/*
%doc BUGS.txt FAQ.txt HISTORY.txt README.txt RELEASE-NOTES.txt 
%doc THANKS.txt docs tools/editors
%{_bindir}/*
%{python3_sitelib}/%{srcname}/
%{python3_sitelib}/docutils-*.egg-info

%files -n %{elisppkgname}
%defattr(-,root,root)
%license COPYING.txt licenses/*
%doc BUGS.txt FAQ.txt HISTORY.txt README.txt RELEASE-NOTES.txt 
%doc THANKS.txt docs tools/editors
%{_datadir}/emacs/site-lisp/%{elisppkgname}
%{emacsen_pkgdir}/install/%{elisppkgname}
%{emacsen_pkgdir}/remove/%{elisppkgname}


%changelog
* Sun Aug 16 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.16-1
- new upstream release.

* Tue Nov 27 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.14-1
- new upstream release.
- added python3 support.

* Sun Jan 12 2014 Yoji TOYODA <bsyamato@sea.plala.or.jp> 0.11-2
- rebuild with VineSeed environment

* Fri Sep 06 2013 Toshiharu Kudoh <toshi.kd2@gmail.com> 0.11-1
- new upstream release

* Sun Jun 16 2013 Toshiharu Kudoh <toshi.kd2@gmail.com> 0.10-1
- new upstream release

* Sun Feb 19 2012 Yoji TOYODA <bsyamato@sea.plala.or.jp> 0.6-4
- rebuild with python-2.7.2

* Sat Jan 29 2011 IWAI, Masaharu <iwai@alib.jp> 0.6-3
- new package: rst-el (Emacs Lisp)
 - add Source1-4

* Thu Sep 02 2010 Shu KONNO <owa@bg.wakwak.com> 0.6-2
- changed package-name to python-docutils from docutils
- added Provides: docutils = %%{version}-%%{release}
- added Obsoletes: docutils <= 0.5
- added BR: python-setuptools
- added RQ: python-imaging
  (merged from '* Mon Aug 23 2010 Munehiro Yamamoto <munepi@vinelinux.org> python-docutils-0.6-1')

* Wed Feb 03 2010 Shu KONNO <owa@bg.wakwak.com> 0.5-3
- rebuilt with python-2.6.4

* Thu Aug  6 2009 MATSUBAYASHI Kohji <shaolin@vinelinux.org> - 0.5-2
- rename /usr/bin/*.py to /usr/bin/*
  (keep symlinking to *.py for compatibility - should be removed in the future)

* Wed Jul 01 2009 Shu KONNO <owa@bg.wakwak.com> 0.5-1
- updated docutils to 0.5
- added BuildArch: noarch

* Sun Jul 20 2008 Shu KONNO <owa@bg.wakwak.com> 0.4-1vl5
- rebuilt with python-2.5.2
- applied new versioning policy and spec in utf-8

* Thu Jan 31 2008 Shu KONNO <owa@bg.wakwak.com> 0.4-0vl4
- defined python_sitelib by using distutils.sysconfig.get_python_lib()
- updated module directory with platform-independent path
- added script to install extras/roman.py (missing if it's already on the system)

* Sun Sep 9 2007 KANEKO Seiji <giraffe@m2.pbc.ne.jp> 0.4-0vl2
- initial build for Vine Linux