%{!?_texmf: %global _texmf %(eval "echo `kpsewhich -expand-var '$TEXMFMAIN'`")}

%global         optflags %{optflags} -DGLM_ENABLE_EXPERIMENTAL

Name:           asymptote
Version:        2.70
Release:        2
Summary:        Descriptive vector graphics language
License:        LGPLv3+
URL:            http://asymptote.sourceforge.net/
Source0:        http://download.sourceforge.net/sourceforge/asymptote/asymptote-%{version}.src.tgz
Source1:        asy.gif
Source2:        xasy.desktop
Source3:        asymptote.sty.204
Patch0:         asymptote-2.67-settings.patch
# This doesn't need to go upstream. We put the info file in the topdir, not a subdir, so we need this fix.
Patch3:         asymptote-2.08-info-path-fix.patch
# Use libtirpc if found
Patch4:         asymptote-2.43-libtirpc.patch
# memrchr value does not match hardcoded one in /usr/include/string.h
# only conflicts on s390x
Patch5:         asymptote-2.52-const-memrchr.patch
Patch6:         asymptote-2.63-freeglut.patch
# removes .setpdfwrite from ghostscript call in dvipdf (#1968328)
Patch7:         asymptote-2.70-remove-old-setpdfwrite.patch

BuildRequires:  gcc-c++, ncurses-devel, readline-devel, fftw-devel >= 3.0, gc-devel >= 6.8
BuildRequires:  gsl-devel, tex(latex), tex(epsf.tex), tex(cm-super-t1.enc), tex(parskip.sty)
BuildRequires:  ghostscript, texinfo-tex, ImageMagick, desktop-file-utils, freeglut-devel
BuildRequires:  zlib-devel, libtool, mesa-libOSMesa-devel, emacs, libtirpc-devel
BuildRequires:  ghostscript-tools-dvipdf, glm-devel, make
Requires:       emacs-filesystem >= %{_emacs_version}
# Cleanup
Provides:       emacs-%{name} = %{version}-%{release}
Obsoletes:      emacs-%{name} <= 2.35
Provides:       emacs-%{name}-el = %{version}-%{release}
Obsoletes:      emacs-%{name}-el <= 2.35

Requires:       tex(latex), python3-qt5, python3-speg, python3-cson, python3-tkinter, python3-imaging-tk, python3-numpy
Recommends:     evince, xdg-utils

# for /usr/bin/texhash
Requires(post):     texlive-kpathsea-bin, tex-kpathsea
Requires(postun):   texlive-kpathsea-bin, tex-kpathsea

%define texpkgdir   %{_texmf}/tex/latex/%{name}

%description
Asymptote is a powerful descriptive vector graphics language for technical
drawings, inspired by MetaPost but with an improved C++-like syntax.
Asymptote provides for figures the same high-quality level of typesetting
that LaTeX does for scientific text.

%prep
%setup -q
%patch0 -p1 -b .settings
%patch3 -p1 -b .path-fix
%patch4 -p1 -b .libtirpc
%patch5 -p1 -b .const-memrchr
%patch6 -p1 -b .glut
%patch7 -p1
%{__sed} -i 's/\r//' doc/CAD1.asy

# convert to UTF-8
iconv -f iso-8859-1 -t utf-8 -o examples/interpolate1.asy{.utf8,}
mv examples/interpolate1.asy{.utf8,}
autoreconf -i

%build
%configure --enable-gc=system \
    --with-docdir=%{?_pkgdocdir}%{!?_pkgdocdir:%{_docdir}/%{name}-%{version}/} \
    --with-latex=%{_texmf}/tex/latex \
    --with-context=%{_texmf}/tex/context/
make %{?_smp_mflags}
cd doc/
make all

%install
%make_install 

install -p -m 644 BUGS ChangeLog LICENSE README ReleaseNotes TODO \
    %{buildroot}%{?_pkgdocdir}%{!?_pkgdocdir:%{_docdir}/%{name}-%{version}}

# Emacs files
mkdir -p %{buildroot}%{_emacs_sitestartdir}
mkdir -p %{buildroot}%{_emacs_sitelispdir}/%{name}
cp -a %{buildroot}%{_datadir}/%{name}/*.el %{buildroot}%{_emacs_sitelispdir}/%{name}
mv %{buildroot}%{_emacs_sitelispdir}/%{name}/asy-init.el %{buildroot}%{_emacs_sitestartdir}
for i in %{buildroot}%{_emacs_sitelispdir}/%{name}/*.el; do
   %{_emacs_bytecompile} $i
done

# Vim syntax file(s)
install -dm 755 %{buildroot}%{_datadir}/vim/vimfiles/syntax
pushd %{buildroot}%{_datadir}/vim/vimfiles/syntax
ln -s ../../../%{name}/asy.vim .
popd
install -dm 755 %{buildroot}%{_datadir}/vim/vimfiles/ftdetect
pushd %{buildroot}%{_datadir}/vim/vimfiles/ftdetect
ln -s ../../../%{name}/asy_filetype.vim .
popd

# Move info file
mv %{buildroot}%{_infodir}/asymptote/asymptote.info %{buildroot}%{_infodir}/asymptote.info

# copy icon to pixmaps dir
mkdir -p %{buildroot}%{_datadir}/pixmaps/
cp %{SOURCE1} %{buildroot}%{_datadir}/pixmaps/

mkdir -p %{buildroot}%{_datadir}/applications
desktop-file-install --dir %{buildroot}%{_datadir}/applications %{SOURCE2}

# Clean up symlink
rm -rf %{buildroot}%{_bindir}/xasy
cd %{buildroot}%{_bindir}
ln -s ../share/%{name}/GUI/xasy.py xasy

%post
texhash >/dev/null 2>&1 || :

%postun
texhash >/dev/null 2>&1 || :

%files
%doc %{?_pkgdocdir}%{!?_pkgdocdir:%{_docdir}/%{name}-%{version}/}
%{_bindir}/*
%{_datadir}/%{name}/
%{texpkgdir}/
%{_texmf}/tex/context/
%{_mandir}/man1/*.1*
%{_infodir}/*.info*
%{_datadir}/vim/vimfiles/syntax/asy.vim
%{_datadir}/vim/vimfiles/ftdetect/asy_filetype.vim
%{_datadir}/emacs*
%{_datadir}/pixmaps/asy.gif
%{_datadir}/applications/*.desktop
%{_emacs_sitestartdir}/*.el
%{_emacs_sitelispdir}/%{name}/*.elc
%dir %{_emacs_sitelispdir}/%{name}
%{_emacs_sitelispdir}/%{name}/*.el

%changelog
* Mon Aug 07 2023 chenchen <chen_aka_jan@163.com> - 2.70-2
- Add a space between the -ltirpc -lgsl option

* Fri Aug 26 2022 herengui <herengui@uniontech.com> - 2.70-1
- Initial package
