
# This is the SPEC file to build RPM packages for MySQL Workbench
#
# To use this file, you should define several macros when calling
# rpmbuild, in order to specify the edition, version and release
# numbers of the current build.
#
# Here's a usage example:
# rpmbuild -ba mysql-workbench.spec --target=`arch` \
#                                   --define='edition community' \
#                                   --define='version 8.0.42' \
#                                   --define='release 1'
####################################################################

# Enable use of "--with" and "--without" options to "rpmbuld"
#

# Temporarily disable the check-rpath run. It is enabled by default on
# Fedora 35. It will complain about "ogr2ogr" and "ogrinfo" that
# contains invalid RPATHS pointing to internal install directories
%global __brp_check_rpaths %{nil}

# Allow to continue when a binary with no build-id was found
%undefine _missing_build_ids_terminate_build

# Disable debug source packages
%undefine _debugsource_packages

%if !%{defined version}
%define version         @VERSION@
%endif

# Disable global build id because of cpp connector bundle
%global _build_id_links none

%if 0%{?fedora} || 0%{?rhel} == 9
%else
%include %{_rpmconfigdir}/macros.python
%endif

%if "%{edition}" == "commercial"
%define commercial       1
%define lictag           com
%define license_type     Commercial
%define license_file     License-commercial.txt
%define readme_file      README-commercial.md
%else
%define community        1
%define lictag           gpl
%define edition          community
%define license_type     GPLv2
%define license_file     License.txt
%define readme_file      README.md
%endif


# FIXME change so that if not defined, this is a build building
# against RPM installed libraries
# %%if %%{defined bundles_path}

Summary: A MySQL visual database modeling, administration, development and migration tool
Name   : mysql-workbench-%{edition}
Version: %{version}
Release: %{release}%{?dist}
Group  : Applications/Databases
Vendor : Oracle Corporation
License: %{license_type}
URL    : http://wb.mysql.com
Source : %{name}-%{version}-src.tar.gz

BuildRoot    : %{_tmppath}/%{name}-%{version}-root

BuildRequires: pcre-devel >= 3.9
BuildRequires: cmake >= 3.6
BuildRequires: libzip-devel libxml2-devel
%if 0%{?fedora}
BuildRequires: python3-devel >= 3.7
%else
%if 0%{?rhel} == 9
BuildRequires: python3-devel >= 3.9
%endif
%endif
BuildRequires: gtk3-devel
BuildRequires: gtkmm30-devel
BuildRequires: mesa-libGL-devel
BuildRequires: make
BuildRequires: tar
BuildRequires: gcc-c++
BuildRequires: swig >= 3.0
BuildRequires: proj-devel
BuildRequires: libsecret-devel

%if !%{defined mysql_home}
BuildRequires: mysql-devel >= 5.7
%endif

%if !%{defined bundles_path}
BuildRequires: boost-devel
BuildRequires: sqlite-devel
BuildRequires: vsqlite++-devel
BuildRequires: unixODBC-devel
BuildRequires: gdal-devel
%endif

%if !%{defined python_lib_path} || !%{defined python_inc_dir}
%if 0%{?rhel} == 9
%define python_lib_path /usr/lib64/libpython3.9.so
%define python_inc_dir /usr/include/python3.9
%endif
%endif

%if 0%{?fedora} >= 18
BuildRequires: libuuid-devel
%endif

%if 0%{?fedora} >= 26
BuildRequires: jasper-devel
%endif

Provides: mysql-workbench = %{version}-%{release}
Provides: mysql-workbench%{?_isa} = %{version}-%{release}
Requires: libzip
Requires: gnome-keyring
Requires: glib2 >= 2.28
Requires: gtk3
Requires: gtkmm30
Requires: libsecret

# our old package names
Obsoletes: mysql-workbench < 7.0
Conflicts: mysql-workbench-oss
Conflicts: mysql-workbench-com-se
Conflicts: mysql-workbench-gpl

%if 0%{?commercial}
Obsoletes: mysql-workbench-com-se < 7.0
Conflicts: mysql-workbench-community
%else
Obsoletes: mysql-workbench-oss < 7.0
Obsoletes: mysql-workbench-gpl < 7.0
Conflicts: mysql-workbench-commercial
%endif


# Filtering: https://fedoraproject.org/wiki/Packaging:AutoProvidesAndRequiresFiltering (from EL7)
# FIXME if using system installs of one of these, this list is not correct
%global __requires_exclude ^lib(antlr4|antlr4-runtime|cdbc|gdal|grt|iodbc|iodbcadm|iodbcinst|linux_utilities|mdcanvas|mdcanvasgtk|mforms|mysqlcppconn|parsers|sqlide|sqlite3|sqlparser|vsqlitepp|wbbase|wbprivate|wbpublic|wbscintilla|mtemplate|ssh|gcrypt|gpg-error|wbssh|mysqlclient)\\.so.*$
%global __provides_exclude_from ^(%{_libdir}/mysql.workbench/.*\\.so.*)$


%description
MySQL Workbench is a unified visual tool for database architects, developers,
and DBAs. MySQL Workbench provides data modeling, SQL development, and
comprehensive administration tools for server configuration, user
administration, backup, and much more. MySQL Workbench is available on
Windows, Linux and Mac OS X.


%prep

if [ -z "%{?bundles_path}" ] ; then
  echo "ERROR: You need to run like: rpmbuild --define 'bundles_path <path-to-bundles-repo>' ..."
  exit 1
fi

# Add the -D flag if you don't want to delete the source root on each build
%setup -q -n %{name}-%{version}-src

%build
%undefine _hardened_build #need to undefine because of some memory problems
# CMAKE_INSTALL_PREFIX etc is handled by the cmake macro
%cmake \
       -DCMAKE_BUILD_TYPE=Release \
       -DREAL_EXECUTABLE_DIR=%{_libexecdir}/mysql-workbench \
%if %{defined mysql_home}
       -DMySQL_CONFIG_PATH=%{mysql_home}/bin/mysql_config \
       -DUSE_BUNDLED_MYSQLDUMP=1 \
%endif
       -DMySQLCppConn_LIBRARIES="-L%{bundles_path}/lib -lmysqlcppconn" \
       -DMySQLCppConn_INCLUDE_DIR=%{bundles_path}/include/mysql-cppconn/jdbc \
       -DIODBC_CONFIG_PATH=%{bundles_path}/bin/iodbc-config \
       -DIODBC_INCLUDE_PATH=%{bundles_path}/include \
       -DRAPIDJSON_INCLUDE_DIR=%{bundles_path}/include \
       -DIODBC_LIBRARIES_PATH=%{bundles_path}/lib \
       -DGDAL_INCLUDE_DIR=%{bundles_path}/include \
       -DGDAL_LIBRARY=%{bundles_path}/lib/libgdal.so \
       -DVSQLITE_INCLUDE_DIR=%{bundles_path}/include \
       -DVSQLITE_LIBRARIES="-L%{bundles_path}/lib -lvsqlitepp" \
       -DANTLR4_INCLUDE_DIR=%{bundles_path}/include \
       -DANTLR4_LIBRARIES="-L%{bundles_path}/lib -lantlr4-runtime" \
       -DWITH_ANTLR_JAR=%{bundles_path}/bin/antlr-4.13.2-complete.jar \
       -Dlibssh_DIR=%{bundles_path}/lib/cmake/libssh \
%if %{defined python_lib_path}
       -DPYTHON_LIBRARY=%{python_lib_path} \
       -DPYTHON_INCLUDE_DIR=%{python_inc_dir} \
%endif
       -DBOOST_ROOT=%{bundles_path}/include \
       -DBoost_NO_BOOST_CMAKE=ON 
       
%cmake_build


%install
%cmake_install

rm -fr %{buildroot}/usr/share/doc/mysql-workbench*

# Bundle client programs and lib
%if %{defined mysql_home}
  cp -a %{mysql_home}/bin/mysql %{buildroot}%{_libexecdir}/mysql-workbench/
  cp -a %{mysql_home}/bin/mysqldump %{buildroot}%{_libexecdir}/mysql-workbench/
  cp -a %{bundles_path}/bin/ogr2ogr %{buildroot}%{_libexecdir}/mysql-workbench/
  cp -a %{bundles_path}/bin/ogrinfo %{buildroot}%{_libexecdir}/mysql-workbench/
%endif

#
# Don't bundle libzip.so. Those can be found in EPEL.
#
#%%if 0%%{?rhel}
#  cp -a /usr/lib64/libzip.so* %%{buildroot}%%{_libdir}/mysql-workbench/
#%%endif

find %{buildroot}%{_libdir}/mysql-workbench -name \*.a  -exec rm {} \; -print
find %{buildroot}%{_libdir}/mysql-workbench -name \*.la -exec rm {} \; -print

%if %{defined bundles_path}
  # Bundle pre-built libs
  cp -a %{bundles_path}/Python/pyodbc/pyodbc*.so         %{buildroot}%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/libsqlite*.so*              %{buildroot}%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/libgdal.so*                 %{buildroot}%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/libmysqlcppconn.so*         %{buildroot}%{_libdir}/mysql-workbench/
# Note that MySQL Shell only provide static libraries at the moment,
# i.e. is linked into the WB binaries
# cp -a %%{mysqlshell_dir}/lib/*.so*                  %%{buildroot}%%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/libvsqlitepp.so*            %{buildroot}%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/libantlr4-runtime.so*       %{buildroot}%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/libiodbc{,inst,adm}.so*     %{buildroot}%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/bin/iodbcadm-gtk                %{buildroot}%{_libexecdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/libssh.so*                  %{buildroot}%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/mysql/libmysqlclient.so.*   %{buildroot}%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/mysql/authentication_kerberos_client.so   %{buildroot}%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/mysql/authentication_ldap_sasl_client.so   %{buildroot}%{_libdir}/mysql-workbench/
  cp -a %{bundles_path}/lib/mysql/mysql_native_password.so   %{buildroot}%{_libdir}/mysql-workbench/
%endif

# If python path is specified that means we are using pb2 built
# python and hence the python (.so) file needs to be bundled with
# WB package.
%if %{defined python_lib_path}
  cp -a %{python_lib_path}*                             %{buildroot}%{_libdir}/mysql-workbench/
%endif

%post
touch --no-create %{_datadir}/icons/hicolor || :
if [ -x %{_bindir}/gtk-update-icon-cache ]; then
   %{_bindir}/gtk-update-icon-cache --quiet %{_datadir}/icons/hicolor || :
fi

if [ -x %{_bindir}/update-desktop-database ]; then
    %{_bindir}/update-desktop-database
fi

if [ -x %{_bindir}/update-mime-database ]; then
    %{_bindir}/update-mime-database %{_datadir}/mime 2>&1 > /dev/null || :
fi


%postun
touch --no-create %{_datadir}/icons/hicolor || :
if [ -x %{_bindir}/gtk-update-icon-cache ]; then
   %{_bindir}/gtk-update-icon-cache --quiet %{_datadir}/icons/hicolor || :
fi

if [ -x %{_bindir}/update-desktop-database ]; then
    %{_bindir}/update-desktop-database
fi

if [ -x %{_bindir}/update-mime-database ]; then
    %{_bindir}/update-mime-database %{_datadir}/mime 2>&1 > /dev/null || :
fi


%clean
rm -rf %{buildroot}
rm -rf %{_builddir}/%{name}-%{version}-src


%files
%defattr(0644, root, root, 0755)
%doc %{license_file} %{readme_file}

%attr(0755,root,root) %{_bindir}/mysql-workbench
%attr(0755,root,root) %{_bindir}/wbcopytables

%dir %{_libexecdir}/mysql-workbench
%attr(0755,root,root) %{_libexecdir}/mysql-workbench/mysql-workbench-bin
%attr(0755,root,root) %{_libexecdir}/mysql-workbench/wbcopytables-bin
%attr(0755,root,root) %{_libexecdir}/mysql-workbench/iodbcadm-gtk
%attr(0755,root,root) %{_libexecdir}/mysql-workbench/mysql
%attr(0755,root,root) %{_libexecdir}/mysql-workbench/mysqldump
%attr(0755,root,root) %{_libexecdir}/mysql-workbench/ogr2ogr
%attr(0755,root,root) %{_libexecdir}/mysql-workbench/ogrinfo

%{_libdir}/mysql-workbench

%{_datadir}/mysql-workbench
%attr(0755,root,root) %{_datadir}/mysql-workbench/extras/*.sh

%{_datadir}/icons/hicolor/*/mimetypes/*
%{_datadir}/icons/hicolor/*/apps/*
%{_datadir}/mime-info/*
%{_datadir}/mime/packages/*
%{_datadir}/applications/*.desktop

%changelog
