%global major_minor 5.4
Name: gnuplot
Summary: a portable command-line driven graphing utility
Version: %{major_minor}.5
Release: 1
License: gnuplot and MIT
URL: http://www.gnuplot.info/

Source0: https://downloads.sourceforge.net/%{name}/%{name}-%{version}.tar.gz

Patch1: gnuplot-4.2.0-fonts.patch
Patch3: gnuplot-4.6.1-plot-sigsegv.patch
Patch4: gnuplot-4.6.4-singlethread.patch
Patch5: gnuplot-5.0.0-lua_checkint.patch
Patch6: gnuplot-5.4.3-no-lena.patch
Patch7: gnuplot-5.2.2-doc.patch
Patch8: gnuplot-5.2.8-cmd-opts.patch
Patch9: gnuplot-config.patch

BuildRequires: cairo-devel latex2html emacs gd-devel giflib-devel libotf libpng-devel
BuildRequires: librsvg2 texinfo libX11-devel libXt-devel lua-devel  m17n-lib tex-tex4ht
BuildRequires: tex(ecrm1000.tfm) tex(subfigure.sty) tex(cm-super-t1.enc)
BuildRequires: zlib-devel libjpeg-turbo-devel pango-devel qt5-linguist tex(latex)
BuildRequires: qt5-qtsvg-devel  qt5-qtbase-devel /usr/bin/texi2dvi

Requires: tex-preview dejavu-sans-fonts
Requires: tex(latex) tex(cm-super-t1.enc) tex(ecrm1000.tfm) tex(utf8x.def)

Requires(post): %{_sbindir}/alternatives 
Requires(preun): %{_sbindir}/alternatives

Obsoletes: gnuplot-qt < 5.0.0-4

Obsoletes: %{name}-common < %{version}-%{release}
Provides: %{name}-common = %{version}-%{release}
Obsoletes: %{name}-minimal < %{version}-%{release}
Provides: %{name}-minimal = %{version}-%{release}
Obsoletes: %{name}-latex < %{version}-%{release}
Provides: %{name}-latex = %{version}-%{release}

Obsoletes: emacs-%{name} <= 5.0.0-3
Obsoletes: emacs-%{name}-el <= 5.0.0-3

%description
Gnuplot is a portable command-line driven graphing utility for Linux, OS/2, MS
Windows, OSX, VMS, and many other platforms. The source code is copyrighted but
freely distributed (i.e., you don't have to pay for it). It was originally
created to allow scientists and students to visualize mathematical functions and
data interactively, but has grown to support many non-interactive uses such as
web scripting. It is also used as a plotting engine by third-party applications
like Octave. Gnuplot has been supported and under active development since 1986.

%package_help

%prep
%autosetup -p1
rm -rf demo/lena*

sed -i -e 's:"/usr/lib/X11/app-defaults":"%{_datadir}/X11/app-defaults":' src/gplt_x11.c
chmod 644 src/getcolor.h
chmod 644 demo/html/webify.pl
chmod 644 demo/html/webify_svg.pl
chmod 644 demo/html/webify_canvas.pl

%build
rm -rf demo/plugin/*.so demo/plugin/*.o
%global configure_opts \\\
        --with-readline=builtin \\\
        --enable-history-file \\\
        --with-texdir=/usr/share/texlive/texmf-dist/tex/latex/gnuplot
mkdir minimal
pushd minimal
cp -a ../configure .
%configure %{configure_opts} \
           --disable-wxwidgets \
           --without-qt
%make_build
popd

mkdir qt
pushd qt
cp -a ../configure .
%configure %{configure_opts} \
           --disable-wxwidgets \
           --enable-qt
%make_build
popd

%configure  %{configure_opts} 

ln -s ../minimal/src/%{name} src/
make -C docs info
export GNUPLOT_PS_DIR=../../term/PostScript
make -C docs/psdoc ps_symbols.ps ps_fontfile_doc.pdf
rm -rf docs/htmldocs/images.idx

%install
make -C qt install DESTDIR=%{buildroot} INSTALL='install -p'
mv  %{buildroot}%{_bindir}/%{name} %{buildroot}%{_bindir}/%{name}-qt
install -p -m 755 minimal/src/%{name} %{buildroot}%{_bindir}/%{name}-minimal
make -C docs install-info DESTDIR=%{buildroot} INSTALL='install -p'
rm -f $RPM_BUILD_ROOT%{_infodir}/dir
install -d %{buildroot}%{_datadir}/X11/app-defaults
mv  %{buildroot}%{_datadir}/%{name}/%{major_minor}/app-defaults/Gnuplot %{buildroot}%{_datadir}/X11/app-defaults/Gnuplot
rm -rf $RPM_BUILD_ROOT%{_libdir}/
ln -s gnuplot.1 $RPM_BUILD_ROOT/%{_mandir}/man1/gnuplot-qt.1
touch %{buildroot}%{_bindir}/%{name}

%posttrans
%{_sbindir}/alternatives --install %{_bindir}/%{name} %{name} %{_bindir}/%{name}-qt 61
%{_sbindir}/alternatives --install %{_bindir}/%{name} %{name} %{_bindir}/%{name}-minimal 40

%post
[ -e %{_bindir}/texhash ] && %{_bindir}/texhash 2> /dev/null;

%preun
if [ $1 = 0 ]; then
    %{_sbindir}/alternatives --remove %{name} %{_bindir}/%{name}-qt || :
    %{_sbindir}/alternatives --remove %{name} %{_bindir}/%{name}-minimal || :
fi

%files
%ghost %attr(0755,-,-) %{_bindir}/%{name}
%{_bindir}/%{name}-qt
%{_libexecdir}/%{name}/%{major_minor}/%{name}_qt
%{_datadir}/%{name}/%{major_minor}/qt/
%dir %{_datadir}/%{name}
%dir %{_datadir}/%{name}/%{major_minor}
%dir %{_datadir}/%{name}/%{major_minor}/PostScript
%{_datadir}/%{name}/%{major_minor}/PostScript/*.ps
%{_datadir}/%{name}/%{major_minor}/PostScript/aglfn.txt
%dir %{_datadir}/%{name}/%{major_minor}/js
%{_datadir}/%{name}/%{major_minor}/js/*
%dir %{_datadir}/%{name}/%{major_minor}/lua/
%{_datadir}/%{name}/%{major_minor}/lua/%{name}-tikz.lua
%{_datadir}/%{name}/%{major_minor}/colors_*
%{_datadir}/%{name}/%{major_minor}/%{name}.gih
%{_datadir}/%{name}/%{major_minor}/%{name}rc
%dir %{_libexecdir}/%{name}
%dir %{_libexecdir}/%{name}/%{major_minor}
%{_libexecdir}/%{name}/%{major_minor}/%{name}_x11
%{_datadir}/X11/app-defaults/Gnuplot
%{_infodir}/%{name}.info*
%{_bindir}/%{name}-minimal
%{_datadir}/texlive/texmf-dist/tex/latex/%{name}/

%files help
%doc BUGS Copyright FAQ.pdf NEWS README RELEASE_NOTES
%{_mandir}/man1/gnuplot.1*
%{_mandir}/man1/gnuplot-qt.1*
%doc docs/psdoc/ps_guide.ps
%doc docs/psdoc/ps_symbols.ps
%doc docs/psdoc/ps_file.doc demo
%doc docs/psdoc/ps_fontfile_doc.pdf

%changelog
* Fri Apr 14 2023 liyanan <thistleslyn@163.com> - 5.4.5-1
- Update to 5.4.5

* Thu Feb 09 2023 Ge Wang <wangge20@h-partners.com> - 5.0.6-13
- Fix Missing texdir build failure due to texlive-base updated

* Mon Feb 17 2020 Tianfei <tianfei16@huawei.com> - 5.0.6-12
- Package init
