%bcond_with systemd

%global _initddir %{_sysconfdir}/init.d/

Name:		groonga
Summary:	An Embeddable Fulltext Search Engine
Version:	11.0.6
Release:	1%{?_dist_release}%{?with_systemd:.systemd}
Group:		office
Vendor:		Project Vine
Distribution:	Vine Linux
Packager:	tomop, iwaim

License:	LGPLv2
URL:		http://groonga.org/
Source0:	http://packages.groonga.org/source/groonga/groonga-%{version}.tar.gz

BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-%(%{__id_u} -n)
BuildRequires:	mecab-devel
BuildRequires:	zlib-devel
BuildRequires:	lz4-devel
BuildRequires:	libzstd-devel
BuildRequires:	pcre-devel
Requires:	%{name}-libs = %{version}-%{release}
Requires:	%{name}-plugin-suggest = %{version}-%{release}
%if %{with systemd}
%else
%endif

%description
Groonga is an embeddable full-text search engine library.  It can
integrate with DBMS and scripting languages to enhance their search
functionality.  It also provides a standalone data store server based
on relational data model.


%package	libs
Summary:	Runtime libraries for groonga
Group:		system
License:	LGPLv2 and (MIT or GPLv2)

%description	libs
This package contains the libraries for groonga


%package	server-common
Summary:	Common packages for the groonga server and the groonga HTTP server
Group:		servers
License:	LGPLv2
Requires:	%{name} = %{version}-%{release}
Requires(pre):	shadow-utils

%description	server-common
This package provides common settings for server use


%package	server-gqtp
Summary:	Groonga GQTP server
Group:		servers
License:	LGPLv2
Requires:	%{name}-server-common = %{version}-%{release}
Requires(pre):	shadow-utils
Requires(post):	/sbin/chkconfig
Requires(preun):	/sbin/chkconfig
Requires(preun):	/sbin/service
Requires(postun):	/sbin/service
Obsoletes:	%{name} < 1.2.2-0
Obsoletes:	%{name}-server < 2.0.7-0

%description	server-gqtp
This package contains the groonga GQTP server


%package	server-http
Summary:	Groonga HTTP server (transitional)
Group:		servers
License:	LGPLv2
Requires:	%{name}-server-common = %{version}-%{release}
Requires:	curl
Requires(pre):	shadow-utils
Requires(post):	/sbin/chkconfig
Requires(preun):	/sbin/chkconfig
Requires(preun):	/sbin/service
Requires(postun):	/sbin/service
Obsoletes:	%{name} < 1.2.2-0
Obsoletes:	%{name}-server < 2.0.7-0

%description	server-http
This is a transitional package to groonga-httpd.


%package	httpd
Summary:	Groonga HTTP server
Group:		servers
License:	LGPLv2 and BSD
Requires:	%{name}-server-common = %{version}-%{release}
Provides:	%{name}-server-http = %{version}-%{release}
Obsoletes:	%{name}-server-http <= 4.0.7-2

%description	httpd
This package contains the groonga HTTP server. It is experimental
but has many features. Because it is based on nginx HTTP server.
It will obsolete groonga-server-http when it is stable.


%package	doc
Summary:	Documentation for groonga
Group:		documentation
License:	LGPLv2 and BSD

%description	doc
Documentation for Groonga


%package	devel
Summary:	Libraries and header files for Groonga
Group:		programming
Requires:	%{name}-libs = %{version}-%{release}

%description	devel
Libraries and header files for Groonga


%package	tokenizer-mecab
Summary:	MeCab tokenizer for Groonga
Group:		office
Requires:	%{name}-libs = %{version}-%{release}

%description	tokenizer-mecab
MeCab tokenizer for Groonga


%package	plugin-suggest
Summary:	Suggest plugin for Groonga
Group:		office
Requires:	%{name}-libs = %{version}-%{release}

%description	plugin-suggest
Sugget plugin for Groonga


%package	munin-plugins
Summary:	Munin plugins for Groonga
Group:		system
Requires:	%{name}-libs = %{version}-%{release}
Requires:	munin-node
Requires(post):	munin-node
Requires(post):	/sbin/service
Requires(postun):	/sbin/service

%description	munin-plugins
Munin plugins for Groonga


%debug_package


%prep
%setup -q


%build
%configure \
  --disable-static \
%if %{with systemd}
  --with-package-platform=centos8 \
%else
  --with-package-platform=centos6 \
%endif
  --with-zlib \
  --with-lz4 \
  --with-zstd \
  --with-mecab \
  --with-rapidjson=bundled \
  --with-munin-plugins \
  --enable-mruby

sed -i 's|^hardcode_libdir_flag_spec=.*|hardcode_libdir_flag_spec=""|g' libtool
sed -i 's|^runpath_var=LD_RUN_PATH|runpath_var=DIE_RPATH_DIE|g' libtool
make %{?_smp_mflags}


%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT INSTALL="install -p"
rm $RPM_BUILD_ROOT%{_libdir}/groonga/plugins/*/*.la
rm $RPM_BUILD_ROOT%{_libdir}/*.la

rm -rf groonga-doc
mv $RPM_BUILD_ROOT%{_datadir}/doc/groonga groonga-doc

mkdir -p $RPM_BUILD_ROOT%{_localstatedir}/run/groonga
mkdir -p $RPM_BUILD_ROOT%{_localstatedir}/lib/groonga/db
mkdir -p $RPM_BUILD_ROOT%{_localstatedir}/log/groonga

mv $RPM_BUILD_ROOT%{_datadir}/groonga/munin/ $RPM_BUILD_ROOT%{_datadir}/
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/munin/plugin-conf.d/
cat <<EOC > $RPM_BUILD_ROOT%{_sysconfdir}/munin/plugin-conf.d/groonga
[groonga_*]
  user groonga
  group groonga
  env.PATH %{_bindir}
  env.database_path %{_localstatedir}/lib/groonga/db/db
  env.host 127.0.0.1

  env.http_host 127.0.0.1
  env.http_port 10041
  env.http_database_path %{_localstatedir}/lib/groonga/db/db
  env.http_pid_path %{_localstatedir}/run/groonga/groonga-http.pid
  env.http_query_log_path %{_localstatedir}/log/groonga/query-http.log

  env.httpd_host 127.0.0.1
  env.httpd_port 10041
  env.httpd_database_path %{_localstatedir}/lib/groonga/db/db
  env.httpd_pid_path %{_localstatedir}/run/groonga/groonga-httpd.pid
  env.httpd_query_log_path %{_localstatedir}/log/groonga/httpd/groonga-query.log

  env.gqtp_host 127.0.0.1
  env.gqtp_port 10043
  env.gqtp_database_path %{_localstatedir}/lib/groonga/db/db
  env.gqtp_pid_path %{_localstatedir}/run/groonga/groonga-gqtp.pid
  env.gqtp_query_log_path %{_localstatedir}/log/groonga/query-gqtp.log
EOC

%if %{with systemd}
mkdir -p %{buildroot}%{_unitdir}
mkdir -p %{buildroot}%{_tmpfilesdir}
mv -f %{buildroot}%{_sysconfdir}/tmpfiles.d/* %{buildroot}%{_tmpfilesdir}/
mv -f %{buildroot}/usr%{_unitdir}/* %{buildroot}%{_unitdir}/
%else
# this file is needed by systemd only
rm -rf %{buildroot}%{_sysconfdir}/tmpfiles.d
%endif


%clean
rm -rf $RPM_BUILD_ROOT


%pre server-common
getent group groonga >/dev/null || groupadd -r groonga
getent passwd groonga >/dev/null || \
       useradd -r -g groonga -d %{_localstatedir}/lib/groonga -s /sbin/nologin \
	-c 'groonga' groonga
if [ $1 = 1 ]; then
	mkdir -p %{_localstatedir}/lib/groonga/db
	groonga -n %{_localstatedir}/lib/groonga/db/db shutdown > /dev/null
	chown -R groonga:groonga %{_localstatedir}/lib/groonga
	mkdir -p %{_localstatedir}/run/groonga
	chown -R groonga:groonga %{_localstatedir}/run/groonga
fi
exit 0

%post server-gqtp
%if %{with systemd}
%systemd_post groonga-server-gqtp.service
%else
/sbin/chkconfig --add groonga-server-gqtp
%endif

%post server-http
%if %{with systemd}
%systemd_post groonga-server-http.service
%else
/sbin/chkconfig --add groonga-server-http
%endif

%post httpd
if [ $1 = 1 ] ; then
	mkdir -p %{_localstatedir}/log/groonga/httpd
	chown -R groonga:groonga %{_localstatedir}/log/groonga/httpd
%if %{with systemd}
	%systemd_post groonga-httpd.service
elif [ $1 = 2 ] ; then
	/usr/sbin/groonga-httpd-restart > /dev/null 2>&1 || :
%else
elif [ $1 = 2 ] ; then
	/sbin/service groonga-httpd restart >/dev/null 2>&1 || :
%endif
fi

%post munin-plugins
%{_sbindir}/munin-node-configure --shell --remove-also | grep -e 'groonga_' | sh
[ -f %{_localstatedir}/lock/subsys/munin-node ] && \
%if %{with systemd}
	/bin/systemctl restart munin-node.service
%else
	/sbin/service munin-node restart > /dev/null 2>&1
%endif
:

%preun server-http
%if %{with systemd}
%systemd_preun groonga-server-http.service
%else
if [ $1 = 0 -o -x /bin/systemctl ] ; then
	/sbin/service groonga-server-http stop >/dev/null 2>&1 || :
	/sbin/chkconfig --del groonga-server-http
fi
%endif

%postun server-http
%if %{with systemd}
%systemd_postun groonga-server-http.service
%else
if [ $1 -ge 1 ] ; then
	/sbin/service groonga-server-http condrestart >/dev/null 2>&1 || :
fi
%endif

%preun server-gqtp
%if %{with systemd}
%systemd_preun groonga-server-gqtp.service
%else
if [ $1 = 0 -o -x /bin/systemctl ] ; then
	/sbin/service groonga-server-http stop >/dev/null 2>&1 || :
	/sbin/chkconfig --del groonga-server-gqtp
fi
%endif

%postun server-gqtp
%if %{with systemd}
%systemd_postun groonga-server-gqtp.service
%else
if [ $1 -ge 1 ] ; then
	/sbin/service groonga-server-gqtp condrestart >/dev/null 2>&1 || :
fi
%endif

%preun httpd
%if %{with systemd}
%systemd_preun groonga-httpd.service
%else
if [ $1 = 0 -o -x /bin/systemctl ] ; then
	%{_initddir}/groonga-httpd stop >/dev/null 2>&1 || :
	/sbin/chkconfig --del groonga-server-gqtp
fi
%endif

%postun httpd
%if %{with systemd}
%systemd_postun groonga-httpd.service
%else
if [ $1 -ge 1 ] ; then
	%{_initddir}/groonga-httpd condrestart >/dev/null 2>&1 || :
fi
%endif

%postun munin-plugins
%if %{with systemd} 
%else
if [ $1 -eq 0 ]; then
	[ -f %{_localstatedir}/lock/subsys/munin-node ] && \
%if %{with systemd}
		/bin/systemctl restart munin-node.service
%else
		/sbin/service munin-node restart > /dev/null 2>&1
%endif
	:
fi
%endif

%triggerpostun -n groonga-server-common -- groonga-server-common < 2.0.8-2
chown -R groonga:groonga %{_localstatedir}/lib/groonga
chown -R groonga:groonga %{_localstatedir}/run/groonga


%files
%defattr(-,root,root,-)
%{_bindir}/groonga
%{_bindir}/groonga-benchmark
%{_bindir}/grndb

%files libs
%defattr(-,root,root,-)
%license COPYING
%doc README.md
%{_libdir}/*.so.*
%{_libdir}/groonga
%{_datadir}/groonga
%config(noreplace) %{_sysconfdir}/groonga/synonyms.tsv

%files server-common
%if %{with systemd} 
%{_tmpfilesdir}/*
%endif

%files server-gqtp
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/groonga/
%config(noreplace) %{_sysconfdir}/sysconfig/groonga-server-gqtp
%config(noreplace) %{_sysconfdir}/logrotate.d/groonga-server-gqtp
%ghost %dir %{_localstatedir}/run/%{name}
%attr(0750,groonga,groonga) %dir %{_localstatedir}/lib/%{name}
%attr(0750,groonga,groonga) %dir %{_localstatedir}/lib/%{name}/db
%if %{with systemd} 
%{_unitdir}/groonga-server-gqtp.service
%else
%{_initddir}/groonga-server-gqtp
%endif

%files server-http
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/groonga/
%config(noreplace) %{_sysconfdir}/sysconfig/groonga-server-http
%config(noreplace) %{_sysconfdir}/logrotate.d/groonga-server-http
%ghost %dir %{_localstatedir}/run/%{name}
%attr(0750,groonga,groonga) %dir %{_localstatedir}/lib/%{name}
%attr(0750,groonga,groonga) %dir %{_localstatedir}/lib/%{name}/db
%if %{with systemd}
%{_unitdir}/groonga-server-http.service
%else
%{_initddir}/groonga-server-http
%endif

%files httpd
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/groonga/httpd/*
%config(noreplace) %{_sysconfdir}/sysconfig/groonga-httpd
%config(noreplace) %{_sysconfdir}/logrotate.d/groonga-httpd
%{_sbindir}/groonga-httpd
%{_sbindir}/groonga-httpd-restart
%ghost %dir %{_localstatedir}/run/%{name}
%attr(0750,groonga,groonga) %dir %{_localstatedir}/lib/%{name}
%attr(0750,groonga,groonga) %dir %{_localstatedir}/lib/%{name}/db
%if %{with systemd}
%{_unitdir}/groonga-httpd.service
%else
%{_initddir}/groonga-httpd
%endif

%files doc
%defattr(-,root,root,-)
%license COPYING
%doc README.md
%doc groonga-doc/*

%files devel
%defattr(-,root,root,-)
%{_includedir}/groonga/
%{_libdir}/*.so
%{_libdir}/pkgconfig/groonga*.pc

%files plugin-suggest
%defattr(-,root,root,-)
%{_bindir}/groonga-suggest-*
%dir %{_libdir}/groonga/plugins
%{_libdir}/groonga/plugins/suggest/suggest.so

%files tokenizer-mecab
%defattr(-,root,root,-)
%{_libdir}/groonga/plugins/tokenizers/mecab.so

%files munin-plugins
%defattr(-,root,root,-)
%{_datadir}/munin/plugins/*
%config(noreplace) %{_sysconfdir}/munin/plugin-conf.d/*

%changelog
* Tue Aug 31 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 11.0.6-1
- new upstream release.

* Sat Aug 07 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 11.0.5-1
- new upstream release.

* Tue Jun 29 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 11.0.4-1
- new upstream release.
- added systemd support (disabled as default).

* Fri Mar 19 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 11.0.0-1
- new upstream release.
- dropped ldconfig scriptlets.

* Wed Sep 04 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 9.0.7-1
- new upstream release.

* Wed May 08 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 9.0.2-1
- new upstream release.
- added BR:libxcrypt-devel.

* Sat Dec 30 2017 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 7.1.0-1
- new upstream release.

* Mon May  1 2017 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 7.0.2-1
- new upstream release.

* Tue Dec 13 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 6.1.1-1
- new upstream release.

* Wed Jun 29 2016 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 6.0.5-1
- new upstream release.

* Mon Nov 30 2015 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.1.0-1
- new upstream release.

* Mon Sep 29 2014 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.0.6-1
- new upstream release.

* Sat May 31 2014 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.0.2-1
- new upstream release.

* Mon Mar 31 2014 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.0.1-1
- new upstream release.

* Tue Feb 11 2014 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 4.0.0-1
- new upstream release.

* Sun Dec 29 2013 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.1.1-1
- new upstream release.

* Wed Dec 04 2013 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 3.1.0-1
- new upstream release.

* Mon Feb 11 2013 IWAI, Masaharu <iwai@alib.jp> 3.0.0-1
- update to 3.0.0
- drop groonga-server-http script patch (Patch0): upstream merged
- add grn_index_cursor_next() patch (Patch1) from upstream git
- add Packager: iwaim

* Thu Feb  7 2013 IWAI, Masaharu <iwai@alib.jp> 2.0.8-2
- fix pre script for groonga-server-common sub package
- add triggerpostun script: groonga-server-common < 2.0.8-2
- add groonga-server-http script patch (Patch0)

* Fri Nov 16 2012 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.0.8-1
- new upstream release.

* Sat Sep 15 2012 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 2.0.6-1
- initial build for Vine Linux.

* Wed Aug 29 2012 HAYASHI Kentaro <hayashi@clear-code.com> - 2.0.6-0
- new upstream release.

* Sun Jul 29 2012 Kouhei Sutou <kou@clear-code.com> - 2.0.5-0
- new upstream release.
- split groonga-httpd related files into groonga-httpd package.

* Fri Jun 29 2012 Kouhei Sutou <kou@clear-code.com> - 2.0.4-0
- new upstream release.
- groonga package does not require groonga-tokenizer-mecab package.

* Tue May 29 2012 Kouhei Sutou <kou@clear-code.com> - 2.0.3-0
- new upstream release.

* Sun Apr 29 2012 Kouhei Sutou <kou@clear-code.com> - 2.0.2-0
- new upstream release.

* Fri Mar 30 2012 Kouhei Sutou <kou@clear-code.com> - 2.0.1-2
- Use shutdown command for stop.

* Fri Mar 30 2012 Kouhei Sutou <kou@clear-code.com> - 2.0.1-1
- Fix bind address argument parameter.
  Patch by Masaharu IWAI. Thanks!!!

* Thu Mar 29 2012 Kouhei Sutou <kou@clear-code.com> - 2.0.1-0
- new upstream release.
- grntest -> groonga-benchmark.
- remove groong-tools package.

* Wed Feb 29 2012 Kouhei Sutou <kou@clear-code.com> - 2.0.0-0
- new upstream release.
- remove other permission from DB directory.
- install init.d related files directly.
- use HTTP as the default protocol.

* Sun Jan 29 2012 Kouhei Sutou <kou@clear-code.com> - 1.3.0-0
- new upstream release.
- groonga-server package does not require groonga-munin-plugins package.
  suggested by Masaharu IWAI. Thanks!!!
- groonga package does not require groonga-doc package.
  suggested by Masaharu IWAI. Thanks!!!

* Thu Dec 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.2.9-0
- new upstream release.

* Tue Nov 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.2.8-0
- new upstream release.
- enable zlib support.
- enable lzo support.
- add --with-package-platform=redhat configure option to install init script.
- add --with-munin-plugins cofnigure option to install Munin plugins.

* Sat Oct 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.2.7-0
- new upstream release.

* Thu Sep 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.2.6-0
- new upstream release.

* Mon Aug 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.2.5-0
- new upstream release.

* Fri Jul 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.2.4-0
- new upstream release.

* Wed Jun 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.2.3-0
- new upstream release.
- add a new groong-tools package.

* Sun May 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.2.2-0
- new upstream release.
- split server files into groonga-server package.

* Fri Apr 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.2.1-0
- new upstream release.

* Tue Mar 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.2.0-0
- new upstream release.

* Wed Feb 09 2011 Kouhei Sutou <kou@clear-code.com> - 1.1.0-0
- new upstream release.

* Wed Feb 02 2011 Kouhei Sutou <kou@clear-code.com> - 1.0.8-0
- new upstream release.

* Sat Jan 29 2011 Kouhei Sutou <kou@clear-code.com> - 1.0.7-0
- new upstream release.

* Fri Dec 31 2010 Kouhei Sutou <kou@clear-code.com> - 1.0.6-0
- new upstream release

* Wed Dec 29 2010 Kouhei Sutou <kou@clear-code.com> - 1.0.5-0
- new upstream release.

* Mon Nov 29 2010 Kouhei Sutou <kou@clear-code.com> - 1.0.4-1
- new upstream release

* Wed Nov 24 2010 Daiki Ueno <dueno@redhat.com> - 1.0.3-2
- %%ghost /var/run/*.

* Fri Oct 29 2010 Kouhei Sutou <kou@clear-code.com> - 1.0.3-1
- new upstream release.

* Sat Oct 09 2010 Kouhei Sutou <kou@clear-code.com> - 1.0.2-2
- merge Fedora changes.

* Thu Sep 09 2010 Kouhei Sutou <kou@clear-code.com> - 1.0.2-1
- new upstream release.

* Mon Sep 06 2010 Kouhei Sutou <kou@clear-code.com> - 1.0.1-1
- new upstream release.

* Thu Sep 02 2010 Kouhei Sutou <kou@clear-code.com> - 1.0.0-1
- split packages.

* Tue Aug 24 2010 Daiki Ueno <dueno@redhat.com> - 0.7.6-1
- initial packaging for Fedora