%bcond_with python2
%bcond_with php

%if "%{php_version}" < "5.6"
%global ini_name     %{name}.ini
%else
%global ini_name     40-%{name}.ini
%endif


%global pluginsver 6

%global php_extdir %(php-config --extension-dir 2>/dev/null || echo %{_libdir}/php4)



Name:               graphviz
Version:            2.44.0
Release:            1
Summary:            Graph Visualization Tools
License:            EPL
URL:                http://www.graphviz.org/
Source0:            https://gitlab.com/graphviz/graphviz/-/archive/%{version}/graphviz-%{version}.tar.gz

Patch1:             graphviz-2.40.1-dotty-menu-fix.patch

BuildRequires:      ksh bison m4 flex ruby automake perl-Carp autoconf libtool qpdf ocaml urw-base35-fonts, perl-ExtUtils-Embed, perl-generators, librsvg2-devel swig >= 1.3.33
BuildRequires:      zlib-devel libpng-devel libjpeg-devel expat-devel tk-devel fontconfig-devel libtool-ltdl-devel ruby-devel guile-devel freetype-devel >= 2 tcl-devel >= 8.3
BuildRequires:      python3-devel libXaw-devel libSM-devel libXext-devel java-devel pango-devel gmp-devel lua-devel gtk2-devel cairo-devel >= 1.1.10
BuildRequires:      ghostscript libgs-devel gd-devel perl-devel

%if %{with python2}
BuildRequires:      python2-devel
%endif

%if %{with php}
BuildRequires:      php-devel
%endif

Requires:           urw-base35-fonts xorg-x11-fonts-ISO8859-1-100dpi guile glibc
Requires(post):     glibc
Requires(postun):   glibc

Provides:           %{name}-gd %{name}-graphs %{name}-guile
Obsoletes:          %{name}-gd %{name}-graphs %{name}-guile


%description
Graphviz is open source graph visualization software. Graph visualization is a way of representing structural
 information as diagrams of abstract graphs and networks. It has important applications in networking, bioinformatics,
 software engineering, database and web design, machine learning, and in visual interfaces for other technical domains.

%package devel
Summary: Development headers and libraries for interfacing to the graphviz
Requires: %{name} = %{version}-%{release} pkgconfig

%description devel
Header files for using the graphviz libraries.  The
libdhcpctl and libomapi static libraries are also included in this package.

%package docs
Summary: Documentation files for graphviz

%description docs
The docs package contains documentation files.


%package java
Summary:             Java extension for graphviz
Requires:            %{name} = %{version}-%{release}

%description java
Java extension for graphviz.

%package lua
Summary:             Lua extension for graphviz
Requires:            %{name} = %{version}-%{release} lua

%description lua
Lua extension for graphviz.

%package ocaml
Summary:             Ocaml extension for graphviz
Requires:            %{name} = %{version}-%{release} ocaml

%description ocaml
Ocaml extension for graphviz.


%package perl
Summary:             Perl extension for graphviz
Requires:            %{name} = %{version}-%{release} perl

%description perl
Perl extension for graphviz.

%package ruby
Summary:             Ruby extension for graphviz
Requires:            %{name} = %{version}-%{release} ruby

%description ruby
Ruby extension for graphviz.

%package tcl
Summary:             Tcl extension & tools for graphviz
Requires:            %{name} = %{version}-%{release} tcl >= 8.3 tk

%description tcl
Various tcl packages (extensions) for the graphviz tools.


%if %{with php}
%package php
Summary:             PHP extension for graphviz
Requires:            %{name} = %{version}-%{release} php-common php

%description php
PHP extension for graphviz.
%endif


%if %{with python2}
%package python2
Summary:             Python extension for graphviz
Requires:            %{name} = %{version}-%{release}
Provides:            python2dist(gv) = %{version} python%{python2_version}dist(gv) = %{version}
Provides:            %{name}-python = %{version}-%{release} %{name}-python% = %{version}-%{release}
Obsoletes:           %{name}-python < 2.40.1-25 python2-%{name} < 2.40.1-25

%description python2
Python extension for graphviz.
%endif

%package python3
Summary:             Python 3 extension for graphviz
Requires:            %{name} = %{version}-%{release}
Provides:            python3dist(gv) = %{version} python%{python3_version}dist(gv) = %{version}

%description python3
Python 3 extension for graphviz.



%prep
%autosetup -n graphviz-%{version} -p1

find -type f -regex '.*\.\(c\|h\)$' -exec chmod a-x {} ';'

%build
./autogen.sh
sed -i '/JavaVM.framework/!s/JAVA_INCLUDES=/JAVA_INCLUDES=\"_MY_JAVA_INCLUDES_\"/g' configure
sed -i 's|_MY_JAVA_INCLUDES_|-I%{java_home}/include/ -I%{java_home}/include/linux/|g' configure
sed -i -e 's|expand(|expand(RbConfig::|' -e 's|sitearchdir|vendorarchdir|' config/config_ruby.rb

export CPPFLAGS=-I`ruby -e "puts File.join(RbConfig::CONFIG['includedir'], RbConfig::CONFIG['sitearch'])" || echo /dev/null`

%configure --with-x --disable-static --disable-dependency-tracking \
        --without-mylibgd --with-ipsepcola --with-pangocairo \
        --with-gdk-pixbuf --with-visio --disable-silent-rules \
        --without-lasi --without-gts --disable-sharp --without-ming \
        --disable-r  --without-devil  --without-qt

%if %{with python2}
cp -a tclpkg/gv tclpkg/gv.python2
%endif

%make_build CFLAGS="$RPM_OPT_FLAGS -fno-strict-aliasing -fno-strict-overflow %{?FFSTORE}" \
  CXXFLAGS="$RPM_OPT_FLAGS -fno-strict-aliasing -fno-strict-overflow %{?FFSTORE}" \
  PYTHON_INCLUDES=-I/usr/include/python%{python3_version}m PYTHON_LIBS="-lpython%{python3_version}m" \
  PYTHON_INSTALL_DIR=%{python3_sitearch} PYTHON=%{__python3}

%if %{with python2}
cd tclpkg/gv.python2
%make_build CFLAGS="$RPM_OPT_FLAGS -fno-strict-aliasing -fno-strict-overflow %{?FFSTORE}" \
  CXXFLAGS="$RPM_OPT_FLAGS -fno-strict-aliasing -fno-strict-overflow %{?FFSTORE}" \
  PYTHON_INCLUDES=-I/usr/include/python%{python2_version} PYTHON_LIBS="-lpython%{python2_version}" \
  PYTHON_INSTALL_DIR=%{python2_sitearch} libgv_python.la
%endif



%install
%make_install docdir=%{buildroot}%{_docdir}/%{name} \
        pkgconfigdir=%{_libdir}/pkgconfig PYTHON_LIBS="-lpython%{python3_version}m" \
        PYTHON_INSTALL_DIR=%{python3_sitearch} install

%delete_la

%if %{with php}
%{__mkdir_p} %{buildroot}%{_sysconfdir}/php.d
%{__cat} << __EOF__ > %{buildroot}%{_sysconfdir}/php.d/%{ini_name}
; Enable %{name} extension module
extension=gv.so
__EOF__
%endif


find $RPM_BUILD_ROOT%{_datadir}/%{name}/demo -type f -exec chmod a-x {} ';'

chmod -x $RPM_BUILD_ROOT%{_datadir}/%{name}/lefty/*

%if %{with python2}
pushd tclpkg/gv.python2
install -pD .libs/libgv_python.so $RPM_BUILD_ROOT%{python2_sitearch}/_gv.so
install -p gv.py $RPM_BUILD_ROOT%{python2_sitearch}/gv.py
popd
%endif

# python 3
pushd tclpkg/gv
install -pD .libs/libgv_python.so $RPM_BUILD_ROOT%{python3_sitearch}/_gv.so
install -p gv.py $RPM_BUILD_ROOT%{python3_sitearch}/gv.py
popd

rm -rf $RPM_BUILD_ROOT%{_libdir}/graphviz/python

touch $RPM_BUILD_ROOT%{_libdir}/graphviz/config%{pluginsver}

%check
%if %{with php}
LD_LIBRARY_PATH=$RPM_BUILD_ROOT%{_libdir} \
php --no-php-ini --define extension_dir=$RPM_BUILD_ROOT%{_libdir}/graphviz/php/ \
    --define extension=libgv_php.so --modules | grep gv
%endif

%pre

%preun


%post
/sbin/ldconfig
%{_bindir}/dot -c

%postun -p /sbin/ldconfig


%files
%defattr(-,root,root)
%doc README

%{_bindir}/*
%dir %{_libdir}/graphviz
%{_libdir}/*.so.*
%{_libdir}/graphviz/*.so.*
%{_libdir}/graphviz/guile/

%dir %{_datadir}/graphviz
%{_datadir}/graphviz/
%ghost %{_libdir}/graphviz/config%{pluginsver}

%exclude %{_bindir}/dot_builtins

%if %{with php}
%files php
%config(noreplace) %{_sysconfdir}/php.d/%{ini_name}
%{_libdir}/graphviz/php/
%{php_extdir}/gv.so
%{_datadir}/php*/*
%endif


%files  devel
%{_includedir}/graphviz
%{_libdir}/*.so
%{_libdir}/graphviz/*.so
%{_libdir}/pkgconfig/*.pc


%files docs
%defattr(644,root,root)
%doc %{_datadir}/%{name}/doc
%doc %{_datadir}/%{name}/demo
%doc %{_datadir}/%{name}/doc/pdf
%{_mandir}/man1/*
%{_mandir}/man3/*
%{_mandir}/man7/*

%files java
%{_libdir}/graphviz/java/

%files lua
%{_libdir}/graphviz/lua/
%{_libdir}/lua*/*

%files ocaml
%{_libdir}/graphviz/ocaml/

%files perl
%{_libdir}/graphviz/perl/
%{_libdir}/perl*/*

%if %{with python2}
%files python2
%{python2_sitearch}/*
%{_libdir}/graphviz/python2/*
%endif

%files python3
%{python3_sitearch}/*
%{_libdir}/graphviz/python3/*

%files ruby
%{_libdir}/graphviz/ruby/
%{_libdir}/*ruby*/*

%files tcl
%{_libdir}/graphviz/tcl/
%{_libdir}/tcl*/*



%changelog
* Thu Aug 20 2020 jinzhimin <jinzhimin2@huawei.com> - 2.44.0-1
- Type:update
- ID:NA
- SUG:NA
- DESC:update to 2.44.0

* Tue Aug 18 2020 chenyaqiang <chenyaqiang@huawei.com> - 2.40.1-39
- Type:rebuild
- ID:NA
- SUG:NA
- DESC:rebuild for package build

* Tue Dec 31 2019 openEuler Buildteam <buildteam@openeuler.org> - 2.40.1-38
- Type:bugfix
- ID:NA
- SUG:NA
- DESC:optimization the spec

* Thu Sep 19 2019 hufeng <solar.hu@huawei.com> - 2.40.1-37
-Create spec
