#default ON
%bcond_without mysql
%bcond_without common_rpms
%bcond_without jemalloc

#default OFF
%bcond_with lustre

%if %{with jemalloc}
	%global jemalloc_switch	--enable-jemalloc
%else
	%global jemalloc_switch	--disable-jemalloc
%endif

# target install dir for web gui
%define installdir_www  /var/www

###### end of macro definitions #####

Name: @PACKAGE@
Version: @VERSION@

Vendor: CEA, HPC department <http://www-hpc.cea.fr>
Prefix: %{_prefix}
Release: @RELEASE@%{?config_dependant}%{?dist}

Summary: Robinhood - Policy engine and reporting tool for large filesystems
License: CeCILL-C
Group: Applications/System
Url: http://robinhood.sourceforge.net
Source0: @PACKAGE@-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildRequires: glib2-devel >= 2.16
BuildRequires: libattr-devel
BuildRequires: systemd
%systemd_requires # post/preun/postun
%if %{with mysql}
BuildRequires: /usr/include/mysql/mysql.h
%endif
%if %{with jemalloc}
BuildRequires: jemalloc
BuildRequires: jemalloc-devel
%endif

%description
Robinhood is a tool for monitoring and applying policies to file system entries.
It is designed to process all its tasks in parallel, so it is particularly adapted
for managing large file systems with millions of entries and petabytes of data.

%{?configure_flags:Generated using options: }%{?configure_flags}

# Package robinhood-posix includes robinhood for other POSIX filesystems
%package posix

Summary: Robinhood Policy engine for POSIX filesystems
Group: Applications/System
Conflicts: robinhood-lustre
Provides: robinhood = %{version}-%{release}
Obsoletes: robinhood-tmpfs < 3.0, robinhood-tmpfs-posix < 3.0
Requires: mailx
%if %{with jemalloc}
Requires: jemalloc
%endif

%description posix
Policy engine for POSIX filesystems.

%{?configure_flags:Generated using options: }%{?configure_flags}

%endif

%if %{with common_rpms}
%package adm
Summary: admin/config helper for Robinhood PolicyEngine
Group: Applications/System
Release: @RELEASE@

%description adm
This RPM provides an admin/config helper for Robinhood PolicyEngine (command rbh-config).


%package webgui
Summary: Web interface to vizualize filesystems stats
Group: Applications/System
Release: @RELEASE@
Requires: php, php-pdo, php-mysql

%description webgui
Web interface to vizualize filesystems stats.
This uses robinhood database to display misc. user and group stats.


%package tests
Summary: Test suite for Robinhood
Group: Applications/System
Requires: robinhood robinhood-adm bc strace
# mariadb or mysql
Requires: /usr/bin/mysql
Release: @RELEASE@

%description tests
Lustre and Posix tests for Robinhood.

%endif

%prep
%setup -q -n @PACKAGE@-%{version}

%build
./configure %{lswitch} %{jemalloc_switch} %{?configure_flags} \
	--mandir=%{_mandir} --libdir=%{_libdir}
make %{?_smp_mflags}

%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/%{_sysconfdir}/robinhood.d/templates
mkdir -p $RPM_BUILD_ROOT/%{_sysconfdir}/robinhood.d/includes
mkdir -p $RPM_BUILD_ROOT/%{_sysconfdir}/ld.so.conf.d

install -m 644 doc/templates/includes/*.inc $RPM_BUILD_ROOT/%{_sysconfdir}/robinhood.d/includes/
install -m 644 doc/templates/*.conf $RPM_BUILD_ROOT/%{_sysconfdir}/robinhood.d/templates/

mkdir $RPM_BUILD_ROOT/%{_sysconfdir}/sysconfig
install -m 644 scripts/sysconfig_robinhood $RPM_BUILD_ROOT/%{_sysconfdir}/sysconfig/robinhood

install -m 644 scripts/ld.so.robinhood.conf $RPM_BUILD_ROOT/%{_sysconfdir}/ld.so.conf.d/robinhood.conf

%if %{with common_rpms}
mkdir -p $RPM_BUILD_ROOT/%{installdir_www}/robinhood
cp -r web_gui/gui_v3/* $RPM_BUILD_ROOT/%{installdir_www}/robinhood/.
cp    web_gui/gui_v3/api/.htaccess $RPM_BUILD_ROOT/%{installdir_www}/robinhood/api/.
mkdir -p $RPM_BUILD_ROOT/%{_sysconfdir}/httpd/conf.d/
install -m 644 web_gui/robinhood.conf $RPM_BUILD_ROOT/%{_sysconfdir}/httpd/conf.d/.
%endif

rm -f $RPM_BUILD_ROOT/%{_libdir}/robinhood/librbh_mod_*.a
rm -f $RPM_BUILD_ROOT/%{_libdir}/robinhood/librbh_mod_*.la

# Add an unmutable copy of the templates for the tests
%if %{with common_rpms}
mkdir -p $RPM_BUILD_ROOT/%{_datadir}/robinhood/doc
cp -a doc/templates $RPM_BUILD_ROOT/%{_datadir}/robinhood/doc
%endif

mkdir -p  $RPM_BUILD_ROOT/%{_unitdir}
install -m 444 scripts/robinhood.service $RPM_BUILD_ROOT/%{_unitdir}/robinhood.service
install -m 444 scripts/robinhood@.service $RPM_BUILD_ROOT/%{_unitdir}/robinhood@.service

%post posix
/sbin/ldconfig
%systemd_post robinhood.service
%systemd_post robinhood@.service

%preun posix
%systemd_preun robinhood.service
%systemd_preun robinhood@.service

%postun posix
/sbin/ldconfig
%systemd_postun

%clean
rm -rf $RPM_BUILD_ROOT

%if %{with common_rpms}

%files adm
%{_sbindir}/rbh-config

%files webgui

# set apache permissions
%defattr(640, root, apache, 750)
%{installdir_www}/robinhood
%config(noreplace) %{_sysconfdir}/httpd/conf.d/robinhood.conf


%files tests
%defattr(-,root,root,-)
%dir %{_datadir}/robinhood/
%{_datadir}/robinhood/tests/
%{_datadir}/robinhood/doc/

%endif

# robinhood RPM name for posix is robinhood-posix
%files posix

%defattr(-,root,root,-)
%doc README.md
%doc COPYING
%doc ChangeLog

%{_sbindir}/robinhood
%{_sbindir}/rbh-report
%{_sbindir}/rbh-diff
%{_sbindir}/rbh-undelete
%{_sbindir}/rbh-rebind
%{_sbindir}/rbh_cksum.sh
%{_bindir}/rbh-du
%{_bindir}/rbh-find

%{_libdir}/robinhood/librbh_mod_*.so*

# All man pages but the lhsmtool_cmd one
%{_mandir}/man1/r*

%config(noreplace) %{_sysconfdir}/sysconfig/robinhood

%dir %{_sysconfdir}/robinhood.d
%dir %{_sysconfdir}/robinhood.d/includes
%dir %{_sysconfdir}/robinhood.d/templates

%{_sysconfdir}/ld.so.conf.d/robinhood.conf

%exclude %{_sysconfdir}/robinhood.d/includes/shook.inc
%exclude %{_sysconfdir}/robinhood.d/templates/example_shook.conf
%config %{_sysconfdir}/robinhood.d/includes/*.inc
%config %{_sysconfdir}/robinhood.d/templates/*.conf

%{_unitdir}/robinhood.service
%{_unitdir}/robinhood@.service

%changelog

* Wed Mar 20 2019 Thomas Leibovici <thomas.leibovici@cea.fr> 3.1.5-1
- Robinhood 3.1.5

* Fri Sep 21 2018 Thomas Leibovici <thomas.leibovici@cea.fr> 3.1.4-1
- Robinhood 3.1.4

* Tue Sep 26 2017 Thomas Leibovici <thomas.leibovici@cea.fr> 3.1-1
- Robinhood 3.1

* Tue Sep 12 2017 Thomas Leibovici <thomas.leibovici@cea.fr> 3.1-0.beta1
- Robinhood 3.1 beta

* Fri Sep 16 2016 Thomas Leibovici <thomas.leibovici@cea.fr> 3.0-1
- Final Robinhood 3.0 release

* Thu Jul 07 2016 Thomas Leibovici <thomas.leibovici@cea.fr> 3.0-0.rc1
- Robinhood v3 rc1

* Fri Mar 25 2016 Thomas Leibovici <thomas.leibovici@cea.fr> 3.0-0.alpha2
- Robinhood v3 alpha2

* Wed Dec 16 2015 Thomas Leibovici <thomas.leibovici@cea.fr> 3.0-0.alpha1
- Robinhood v3 alpha1
