%define _centos_ver %{?centos_ver:%{centos_ver}}%{!?centos_ver:5}

%define mysql_version_default 5.6.37
%define mysql_release_default rel82.2
%define mysql_dist_default    %{?dist}
%define mysql_download_base_url_default http://repo.percona.com/centos/%{_centos_ver}/SRPMS
%define mysql_spec_file_default percona-server.spec

%define _mysql_version %{?mysql_version:%{mysql_version}}%{!?mysql_version:%{mysql_version_default}}
%define _mysql_release %{?mysql_release:%{mysql_release}}%{!?mysql_release:%{mysql_release_default}}
%define _mysql_dist %{?mysql_dist:%{mysql_dist}}%{!?mysql_dist:%{mysql_dist_default}}
%define _mysql_download_base_url %{?mysql_download_base_url:%{mysql_download_base_url}}%{!?mysql_download_base_url:%{mysql_download_base_url_default}}
%define _mysql_spec_file %{?mysql_spec_file:%{mysql_spec_file}}%{!?mysql_spec_file:%{mysql_spec_file_default}}

%define groonga_required_version @REQUIRED_GROONGA_VERSION@

Name:		percona-server-56-mroonga
Version:	@VERSION@
Release:	1%{?dist}
Summary:	A fast fulltext searchable storage engine for MySQL

Group:		Applications/Databases
License:	LGPLv2.1
URL:		http://mroonga.org/
Source0:	http://packages.groonga.org/source/mroonga/mroonga-%{version}.tar.gz

BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-%(%{__id_u} -n)
BuildRequires:	groonga-devel >= %{groonga_required_version}
BuildRequires:	groonga-normalizer-mysql-devel
BuildRequires:	wget
BuildRequires:	which
BuildRequires:	gcc
BuildRequires:	gcc-c++
BuildRequires:	Percona-Server-devel-56 = %{_mysql_version}-%{_mysql_release}%{_mysql_dist}
BuildRequires:	selinux-policy-devel
Requires:	Percona-Server-server-56 = %{_mysql_version}-%{_mysql_release}%{_mysql_dist}
Requires:	Percona-Server-client-56 = %{_mysql_version}-%{_mysql_release}%{_mysql_dist}
Requires:	groonga-libs >= %{groonga_required_version}
Requires:	groonga-normalizer-mysql

%description
Mroonga is a fast fulltext searchable storage plugin for MySQL.
It is based on Groonga that is a fast fulltext search engine and
column store. Groonga is good at real-time update.

%package doc
Summary:	Documentation for Mroonga
Group:		Documentation
License:	LGPLv2.1

%description doc
Documentation for Mroonga


%prep
%setup -q -n mroonga-%{version}

mysql_full_version=%{_mysql_version}-%{_mysql_release}.generic
srpm=Percona-Server-56-${mysql_full_version}.src.rpm
if [ ! -f ../../SRPMS/$srpm ]; then
    wget --continue -O ../../SRPMS/$srpm %{_mysql_download_base_url}/$srpm
    rpm -Uvh ../../SRPMS/$srpm
fi

%build
mysql_source=../percona-server-%{_mysql_version}-$(echo %{_mysql_release} | sed -e 's/rel//')
if [ ! -d ${mysql_source} ]; then
    specs_dir=
    rpmbuild -bp \
	--define 'runselftest 0' \
	--define 'optflags -O0' \
	../../SPECS/%{_mysql_spec_file}
fi
%configure \
    --disable-static \
    --with-mysql-source=${mysql_source} \
    --enable-fast-mutexes \
    %{?mroonga_configure_options}
make %{?_smp_mflags}

%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT
rm $RPM_BUILD_ROOT%{_libdir}/mysql/plugin/*.la
mv $RPM_BUILD_ROOT%{_datadir}/doc/mroonga/ mysql-mroonga-doc/

%clean
rm -rf $RPM_BUILD_ROOT

%post
if ! /sbin/service mysql status > /dev/null; then
  /sbin/service mysql start
  stop_after_installation=1
else
  stop_after_installation=0
fi

mysql_command=`which mysql`
password_option=""

if $mysql_command -u root -e "quit" > /dev/null 2>&1; then
  password_option=""
else
  password_option="-p"
fi
current_version=0
version=`echo %{groonga_required_version} | sed -e 's/\.//g'`
required_version=`expr $version`
version=`$mysql_command -e "SHOW VARIABLES LIKE 'mroonga_libgroonga_version'" | \
    grep mroonga | cut -f 2 | sed -e 's/\.//g'`
if [ -n "$version" ]; then
    current_version=`expr $version`
fi
install_sql=%{_datadir}/mroonga/install.sql
uninstall_sql=%{_datadir}/mroonga/uninstall.sql

if [ "$1" = 2 ] ; then
    if [ $current_version -lt $required_version ]; then
	command="$mysql_command -u root $password_option"
	echo "run the following command after restarting MySQL server:";
	echo "  $command < ${uninstall_sql}"
	echo "  $command < ${install_sql}"
	exit 0
    else
	command="$mysql_command -u root $password_option < ${uninstall_sql}"
	echo $command
	eval $command || \
	    (echo "run the following command to unregister Mroonga:"; \
	     echo "  $command")
    fi
fi
command="$mysql_command -u root $password_option < ${install_sql}"
echo $command
eval $command || \
	(echo "run the following command to register Mroonga:"; \
	 echo "  $command")

if [ "$stop_after_installation" = "1" ]; then
  /sbin/service mysql stop
fi

%preun
if ! /sbin/service mysql status > /dev/null; then
  /sbin/service mysql start
  stop_after_uninstallation=1
else
  stop_after_uninstallation=0
fi

uninstall_sql=%{_datadir}/mroonga/uninstall.sql
mysql_command=`which mysql`
if $mysql_command -u root -e "quit" > /dev/null 2>&1; then
    password_option=""
else
    password_option="-p"
fi
if [ "$1" = 0 ]; then
    command="$mysql_command -u root $password_option < ${uninstall_sql}"
    echo $command
    eval $command || \
	(echo "run the following command to unregister Mroonga:"; \
	 echo "  $command")
fi

if [ "$stop_after_uninstallation" = "1" ]; then
  /sbin/service mysql stop
fi

%files
%defattr(-,root,root,-)
%{_libdir}/mysql/plugin/
%{_datadir}/mroonga/*
%{_datadir}/man/man1/*
%{_datadir}/man/*/man1/*

%files doc
%defattr(-,root,root,-)
%doc README COPYING
%doc mysql-mroonga-doc/*

%changelog
* Thu Oct 12 2017 Kentaro Hayashi <hayashi@clear-code.com> - 7.07-1
- new upstream release.

* Tue Aug 29 2017 Kentaro Hayashi <hayashi@clear-code.com> - 7.06-1
- new upstream release.

* Mon Aug 14 2017 Kentaro Hayashi <hayashi@clear-code.com> - 7.05-2
- build against Percona Server 5.6.36rel82.1 Reported by @tigersun2000_twitter. Thanks!!!

* Sat Jul 29 2017 Kentaro Hayashi <hayashi@clear-code.com> - 7.05-1
- new upstream release.

* Thu Jun 29 2017 Kentaro Hayashi <hayashi@clear-code.com> - 7.04-1
- new upstream release.

* Mon May 29 2017 Kentaro Hayashi <hayashi@clear-code.com> - 7.03-1
- new upstream release.

* Thu May 17 2017 Kentaro Hayashi <hayashi@clear-code.com> - 7.02-2
- build against Percona Server 5.6.36. Reported by @pinpikokun. Thanks!!!

* Sat Apr 29 2017 Kentaro Hayashi <hayashi@clear-code.com> - 7.02-1
- new upstream release.

* Wed Mar 29 2017 Kentaro Hayashi <hayashi@clear-code.com> - 7.01-1
- new upstream release.

* Thu Feb 09 2017 Kentaro Hayashi <hayashi@clear-code.com> - 7.00-1
- new upstream release.

* Fri Jan 13 2017 Kouhei Sutou <kou@clear-code.com> - 6.13-1
- new upstream release.

* Thu Dec 29 2016 Kentaro Hayashi <hayashi@clear-code.com> - 6.12-1
- new upstream release.

* Tue Nov 29 2016 Kentaro Hayashi <hayashi@clear-code.com> - 6.11-1
- new upstream release.

* Sat Oct 29 2016 Kentaro Hayashi <hayashi@clear-code.com> - 6.10-1
- new upstream release.

* Mon Oct 24 2016 Kouhei Sutou <kou@clear-code.com> - 6.09-2
- build against Percona Server 5.6.33. Reported by Hiroshi Kagami. Thanks!!!

* Thu Sep 29 2016 Kentaro Hayashi <hayashi@clear-code.com> - 6.09-1
- new upstream release.

* Mon Aug 29 2016 Kentaro Hayashi <hayashi@clear-code.com> - 6.08-1
- new upstream release.

* Fri Jul 29 2016 Kentaro Hayashi <hayashi@clear-code.com> - 6.07-1
- new upstream release.

* Thu Jun 30 2016 Masafumi Yokoyama <yokoyama@clear-code.com> - 6.06-1
- new upstream release.

* Wed Jun 29 2016 Kentaro Hayashi <hayashi@clear-code.com> - 6.05-1
- new upstream release.

* Sun May 29 2016 Kentaro Hayashi <hayashi@clear-code.com> - 6.03-1
- new upstream release.

* Fri Apr 29 2016 HAYASHI Kentaro <hayashi@clear-code.com> - 6.02-1
- new upstream release.

* Tue Mar 29 2016 Masafumi Yokoyama <yokoyama@clear-code.com> - 6.01-1
- new upstream release.

* Mon Feb 29 2016 Kouhei Sutou <kou@clear-code.com> - 6.00-1
- new upstream release.

* Fri Jan 29 2016 Kouhei Sutou <kou@clear-code.com> - 5.12-1
- new upstream release.

* Tue Dec 29 2015 Kouhei Sutou <kou@clear-code.com> - 5.11-1
- new upstream release.

* Sun Nov 29 2015 Kouhei Sutou <kou@clear-code.com> - 5.10-1
- new upstream release.

* Thu Oct 29 2015 Kouhei Sutou <kou@cozmixng.org> - 5.09-1
- new upstream release.

* Tue Sep 29 2015 Kouhei Sutou <kou@clear-code.com> - 5.08-1
- new upstream release.

* Mon Aug 31 2015 Kouhei Sutou <kou@clear-code.com> - 5.06-1
- new upstream release.

* Tue Mar 17 2015 Kouhei Sutou <kou@clear-code.com> - 5.00-1
- initial release.
