# Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License, version 2.0, as published by the
# Free Software Foundation.
#
# This program is also distributed with certain software (including but not
# limited to OpenSSL) that is licensed under separate terms, as designated in a
# particular file or component or in included license documentation. The
# authors of MySQL hereby grant you an additional permission to link the
# program and your derivative works with the separately licensed software that
# they have included with MySQL.
#
# Without limiting anything contained in the foregoing, this file, which is
# part of MySQL Connector/J, is also subject to the Universal FOSS Exception,
# version 1.0, a copy of which can be found at
# http://oss.oracle.com/licenses/universal-foss-exception.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
# for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

# You can pass these options to "rpmbuild"
#
#    --define="commercial <nonempty>"
#    --define="with_ant <path>"
#    --define="with_java <path>"
#    --define="with_jardeps <path>"
#    --define="with_docs <path>"
#
# The 'with_docs' option should have a path that points out
#
#  <docs-base>/en/html/connector-j.html
#  <docs-base>/en/html/mvl.css (not really used)
#  <docs-base>/en/pdf/connector-j.pdf
#  <docs-base>/en/txt/connector-j.txt

# Some linux distributions doesn't set the "dist" macro. There is a
# list how to identify dists here
# https://en.opensuse.org/openSUSE:Build_Service_cross_distribution_howto

# SuSE will not set "dist", others do
%if 0%{?suse_version} == 1315
%global dist            .sles12
%global sles12          1
%endif

%if 0%{?suse_version} == 1500
%global dist                 .sl15
%global sles15                1
%endif

%if 0%{?commercial:1}
%global lic_tag        Commercial
%else
%global lic_tag        GPLv2
%endif

Summary:        Standardized MySQL database driver for Java
Name:           @MYSQL_CJ_EXTENDED_PROD_NAME@
Version:        @MYSQL_CJ_VERSION_NUMERIC@
Release:        @MYSQL_CJ_RPM_RELEASE_FULL@%{?dist}
License:        %{lic_tag}
Group:          Development/Libraries
URL:            http://dev.mysql.com/downloads/connector/j/
Source0:        https://cdn.mysql.com/Downloads/Connector-J/@MYSQL_CJ_FULL_PROD_NAME@.tar.gz

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

%if 0%{?commercial:1}
Obsoletes:     mysql-connector-java < %{version}-%{release}
Provides:      mysql-connector-java = %{version}-%{release}
%endif

%if 0%{!?with_ant:1}
BuildRequires:  ant
%endif

%if 0%{!?with_java:1}
BuildRequires:  java-devel >= 1:1.8.0
%endif

%if 0%{?sles12:1} || 0%{?sles15:1}
Requires:       java-headless >= 1.8.0
%else
Requires:       java-headless >= 1:1.8.0
%endif

%description
MySQL provides connectivity for client applications developed in the
Java programming language with @MYSQL_CJ_DISPLAY_PROD_NAME@, a driver that
implements the [Java Database Connectivity (JDBC) API]
(http://www.oracle.com/technetwork/java/javase/jdbc/).

@MYSQL_CJ_DISPLAY_PROD_NAME@ @MYSQL_CJ_VERSION_SERIES@ is a JDBC Type 4 driver that is compatible with
the [JDBC 4.2](http://docs.oracle.com/javase/8/docs/technotes/guides/jdbc/)
specification. The Type 4 designation means that the driver is a pure
Java implementation of the MySQL protocol and does not rely on the
MySQL client libraries.

For detailed information please visit the official
[@MYSQL_CJ_DISPLAY_PROD_NAME@ documentation]
(http://dev.mysql.com/doc/connector-j/en/).

%prep
%setup -q -n @MYSQL_CJ_FULL_PROD_NAME@

%build
COMMON_OPTIONS="\
  -Dcom.mysql.cj.build.dir.driver=build/driver
  -Dcom.mysql.cj.extra.libs=%{with_jardeps} \
  -Dcom.mysql.cj.build.driver.version.status=@MYSQL_CJ_VERSION_STATUS@ \
  -Dcom.mysql.cj.build.driver.version.extra=@MYSQL_CJ_VERSION_EXTRA@ \
  -Dcom.mysql.cj.build.driver.version.snapshot=@MYSQL_CJ_VERSION_SNAPSHOT@ \
  -Dcom.mysql.cj.build.driver.extraName=@MYSQL_CJ_EXTRA_NAME@ \
  -Dcom.mysql.cj.dist.licenseUrl=@MYSQL_CJ_LICENSEURL@ \
  -Djava.awt.headless=true"

%if 0%{?with_ant:1}
export ANT_HOME=%{with_ant}
export ANT_CMD="${ANT_HOME}/bin/ant"
%else
export ANT_CMD="ant"
%endif

export ANT_OPTS=-Xmx512M

%if 0%{?with_java:1}
export JAVA_HOME=%{with_java}
export PATH=${JAVA_HOME}/bin:$PATH
COMMON_OPTIONS="${COMMON_OPTIONS} -Dcom.mysql.cj.build.jdk=${JAVA_HOME}"
%endif

%if 0%{?commercial:1}
COMMON_OPTIONS="${COMMON_OPTIONS} -Dcom.mysql.cj.build.commercial=true"
%endif

# Get the file revision-info.properties again because it was excluded
# from SOURCES
cp %{_tmppath}/@MYSQL_CJ_FULL_PROD_NAME@/revision-info.properties .

${ANT_CMD} \
   ${COMMON_OPTIONS} \
   full-package-no-sources

# We use the 'full-package-no-sources' as there are changes done only
# when creating a package, 'dist' is not enough. To make it a bit easier
# in the install step, we rename the directory with the content
mv dist/toArchive/@PACKAGE_NAME@ package-content

# The 'package' target doesn't copy the HTML or PDF doc like 'dist',
# we copy it here
%if 0%{?with_docs:1}
mkdir -p package-content/docs
cp %{with_docs}/en/html/connector-j.html package-content/docs/
cp %{with_docs}/en/html/mvl.css          package-content/docs/
cp %{with_docs}/en/pdf/connector-j.pdf   package-content/docs/
cp %{with_docs}/en/txt/connector-j.txt   package-content/docs/
%endif

%install
install -d -m 0755 %{buildroot}%{_javadir}
install -p -m 0644 package-content/@MYSQL_CJ_FULL_PROD_NAME@.jar %{buildroot}%{_javadir}/@MYSQL_CJ_FULL_PROD_NAME@.jar

%clean
rm -rf %{buildroot}

%files
%doc package-content/CHANGES
# EL6 doesn't like 'license' macro here, so we use 'doc'
%doc package-content/LICENSE
%doc package-content/README
%doc package-content/INFO_BIN
%doc package-content/INFO_SRC

%if 0%{?with_docs:1}
# README.txt is a rename of "connector-j.txt"
%doc package-content/docs/connector-j.txt
%doc package-content/docs/connector-j.pdf
%doc package-content/docs/connector-j.html
%doc package-content/docs/mvl.css
%endif

%{_javadir}/@MYSQL_CJ_FULL_PROD_NAME@.jar

%changelog
* Mon Nov 27 2017 MySQL Release Engineering <mysql-build@oss.oracle.com> - 8.0.9-1
- Set more in "build.xml"
* Tue Mar 14 2017 MySQL Release Engineering <mysql-build@oss.oracle.com> - 6.0.7-1
- initial package
