%global ovirt_install_systemd 1
%global ovirt_install_systemv 0
%global commons_collections apache-commons-collections
%global dom4j dom4j

%if 0
%global dom4j eap7-dom4j
%global ovirt_install_systemd 0
%global ovirt_install_systemv 1
%global commons_collections commons-collections
%global dom4j dom4j
%endif

%global product_name Data warehouse package for oVirt Virtualization Suite
%global product_description oVirt virtualization manager data warehouse
%global engine_gid 108
%global engine_group ovirt
%global engine_uid 108
%global engine_user ovirt
%global _ovirt_create_user() \
getent group %3 >/dev/null || groupadd -r -g %4 %3; \
getent passwd %1 >/dev/null || useradd -r -u %2 -g %3 -c %5 -s /sbin/nologin -d %6 %1

%global ovirt_create_user_engine \
%_ovirt_create_user %{engine_user} %{engine_uid} %{engine_group} %{engine_gid} "%%{ovirt_user_description}" %{engine_state}

%global make_common_opts \\\
	-j1 \\\
	BUILD_VALIDATION=0 \\\
	PACKAGE_NAME=%{name} \\\
	RPM_VERSION=%{version} \\\
	RPM_RELEASE=%{release} \\\
	LOCALSTATE_DIR=%{_localstatedir} \\\
	PREFIX=%{_prefix} \\\
	SYSCONF_DIR=%{_sysconfdir} \\\
	BIN_DIR=%{_bindir} \\\
	DATAROOT_DIR=%{_datadir} \\\
	MAN_DIR=%{_mandir} \\\
	DOC_DIR=%{_docdir} \\\
	PYTHON=%{__python3} \\\
	PYTHON_DIR=%{python3_sitelib} \\\
	JAVA_DIR=%{_javadir} \\\
	PKG_USER=%{engine_user} \\\
	PKG_GROUP=%{engine_group} \\\
	%{?EXTRA_BUILD_FLAGS:EXTRA_BUILD_FLAGS="%{EXTRA_BUILD_FLAGS}"}

Name:		ovirt-engine-dwh
Version:	4.4.4.1
Release:	2
License:	LGPLv2 and ASL 2.0
Summary:	%{product_name}
Group:		Virtualization/Management
URL:		http://www.ovirt.org
BuildArch:	noarch
Source:		http://resources.ovirt.org/pub/src/ovirt-engine-dwh/ovirt-engine-dwh-4.4.4.1.tar.gz

BuildRequires:  java-11-openjdk-devel >= 11.0.4
BuildRequires:  java-1.8.0-openjdk-devel
BuildRequires:	%{commons_collections}
BuildRequires:	%{dom4j}
BuildRequires:	ant
BuildRequires:	jpackage-utils
BuildRequires:	javapackages-tools
BuildRequires:	make

BuildRequires:	python3
BuildRequires:	python3-devel

%if %{ovirt_install_systemd}
BuildRequires:	systemd
%endif

Requires:	%{commons_collections}
Requires:	%{dom4j}
Requires:	%{name}-setup >= %{version}-%{release}
Requires:	%{name}-grafana-integration-setup >= %{version}-%{release}
Requires:	java-11-openjdk-headless >= 11.0.4
Requires:	jpackage-utils
Requires:	javapackages-tools
Requires:	logrotate
Requires:	postgresql-jdbc

Requires:       postgresql-server
Requires:       postgresql-contrib

%description
The %{product_description} package provides
the ETL process and DB scripts to create a historic database API.
Enables SQL BI reports creation for management and monitoring.

%if %{ovirt_install_systemd}
Requires(post):		systemd
Requires(preun):	systemd
Requires(postun):	systemd
%endif

%package setup
Summary:	%{product_name} setup
Group:		Virtualization/Management
Requires:	ovirt-engine-setup-plugin-ovirt-engine-common >= 4.4.0
Requires:	%{name}-grafana-integration-setup = %{version}-%{release}
BuildRequires:	python3
BuildRequires:	python3-devel
Requires: python%{python3_pkgversion}-ovirt-setup-lib
Requires:	%{name} >= 3.6.0

Requires:       postgresql-server
Requires:       postgresql-contrib

%description setup
%{product_description} setup package.

%package grafana-integration-setup
Summary:	%{product_name} Grafana integration setup
Group:		Virtualization/Management
Requires:	ovirt-engine-setup-plugin-ovirt-engine-common >= 4.4.0
Requires:	%{name}-setup = %{version}-%{release}
Requires:	grafana >= 6.2
Requires:	grafana-postgres >= 6.2
Requires:	httpd
Requires:	mod_ssl
BuildRequires:	python3
BuildRequires:	python3-devel
Requires:	python3-ovirt-setup-lib
Requires:	python3-jinja2

Requires:       postgresql-server
Requires:       postgresql-contrib

%description grafana-integration-setup
%{product_description} Grafana integration setup package.

%prep
pushd /usr/lib/jvm/
  dir=`ls | grep java-11-openjdk-`
  jredir=`ls | grep jre-11-openjdk-`
  export JAVA_HOME="/usr/lib/jvm/${dir}"
  export JRE_HOME="/usr/lib/jvm/${jredir}"
  export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
  export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
popd
%setup -c -q
sed -i '/export JAVA_HOME/d' automation/build-artifacts.sh
sed -i '/export JAVA_HOME/d' automation/build-artifacts-manual.sh

%build
make %{make_common_opts}

%install
rm -fr "%{buildroot}"

make %{make_common_opts} install DESTDIR=%{buildroot}

%py_byte_compile %{__python3} %{buildroot}/%{_datadir}/*

#
# /var creation
#
install -dm 755 "%{buildroot}/%{_localstatedir}/lib/ovirt-engine-dwh"
install -dm 700 "%{buildroot}/%{_localstatedir}/log/ovirt-engine-dwh"

#
# Move jars to destribution location
#
install -dm 755 "%{buildroot}%{_javadir}/ovirt-engine-dwh"
while read lib_path; do
	java_path="%{_javadir}/ovirt-engine-dwh/$(basename "${lib_path}")"
	mv "%{buildroot}/${lib_path}" "%{buildroot}/${java_path}"
	ln -s "${java_path}" "%{buildroot}${lib_path}"
done << __EOF__
%{_datadir}/ovirt-engine-dwh/lib/advancedPersistentLookupLib.jar
%{_datadir}/ovirt-engine-dwh/lib/etltermination.jar
%{_datadir}/ovirt-engine-dwh/lib/historyETL.jar
%{_datadir}/ovirt-engine-dwh/lib/routines.jar
__EOF__

# Needed for compatibility if package is different than the directory structure
%if "%{name}" != "ovirt-engine-dwh"
ln -s "ovirt-engine-dwh" "%{buildroot}%{_javadir}/ovirt-engine-dwh/../%{name}"
%endif

#
# Register services
#
%if %{ovirt_install_systemd}
install -dm 755 "%{buildroot}%{_unitdir}"
cp "%{buildroot}%{_datadir}/ovirt-engine-dwh/services/ovirt-engine-dwhd/ovirt-engine-dwhd.systemd" "%{buildroot}%{_unitdir}/ovirt-engine-dwhd.service"
%endif

%if %{ovirt_install_systemv}
install -dm 755 "%{buildroot}%{_sysconfdir}/cron.hourly"
cp "%{buildroot}%{_datadir}/ovirt-engine-dwh/services/ovirt-engine-dwhd/ovirt_engine_dwh_watchdog.cron" "%{buildroot}%{_sysconfdir}/cron.hourly"
install -dm 755 "%{buildroot}%{_initddir}"
cp "%{buildroot}%{_datadir}/ovirt-engine-dwh/services/ovirt-engine-dwhd/ovirt-engine-dwhd.sysv" "%{buildroot}%{_initddir}/ovirt-engine-dwhd"
%endif

# backward compatibly (pre-3.4.0)
# perserve old configuration
install -d "%{buildroot}%{_sysconfdir}/ovirt-engine/ovirt-engine-dwh"
touch "%{buildroot}%{_sysconfdir}/ovirt-engine/ovirt-engine-dwh/Default.properties"

#
# Package customization
#

%pre
%ovirt_create_user_engine

%post
%if %{ovirt_install_systemv}
chkconfig --add ovirt-engine-dwhd
%endif
%if %{ovirt_install_systemd}
%systemd_post ovirt-engine-dwhd.service
%endif

%preun
%if %{ovirt_install_systemv}
if [ "$1" -eq 0 ]; then
	service ovirt-engine-dwhd stop > /dev/null 2>&1 || true
	chkconfig --del ovirt-engine-dwhd
fi
%endif
%if %{ovirt_install_systemd}
%systemd_preun ovirt-engine-dwhd.service
%endif

%postun
%if %{ovirt_install_systemd}
%systemd_postun ovirt-engine-dwhd.service
%endif

%files

%ghost %config(noreplace) %{_sysconfdir}/ovirt-engine/ovirt-engine-dwh/Default.properties
%config(noreplace) %{_sysconfdir}/logrotate.d/ovirt-engine-dwhd
%dir %attr(-, %{engine_user}, %{engine_group}) %{_localstatedir}/lib/ovirt-engine-dwh/
%dir %attr(-, %{engine_user}, %{engine_group}) %{_localstatedir}/log/ovirt-engine-dwh/
%dir %{_sysconfdir}/ovirt-engine-dwh
%{_bindir}/dwh-vacuum
%{_datadir}/ovirt-engine-dwh/
%{_datadir}/ovirt-engine-dwh/bin/dwh-prolog.sh
%{_datadir}/ovirt-engine-dwh/bin/dwh-vacuum.sh
%{_datadir}/ovirt-engine-dwh/bin/generate-pgpass.sh
%{_javadir}/ovirt-engine-dwh/
%{_sysconfdir}/ovirt-engine-dwh/ovirt-engine-dwhd.conf.d/
%{_localstatedir}/lib/ovirt-engine-dwh/backups/

%if "%{name}" != "ovirt-engine-dwh"
%{_javadir}/%{name}
%endif

%if %{ovirt_install_systemd}
%{_unitdir}/ovirt-engine-dwhd.service
%endif
%if %{ovirt_install_systemv}
%config %{_sysconfdir}/cron.hourly/ovirt_engine_dwh_watchdog.cron
%{_initddir}/ovirt-engine-dwhd
%endif

%files setup
%{_datadir}/ovirt-engine/setup/ovirt_engine_setup/dwh/
%{_datadir}/ovirt-engine/setup/plugins/*/ovirt-engine-dwh/

%files grafana-integration-setup
%{_datadir}/ovirt-engine/setup/ovirt_engine_setup/grafana_dwh/
%{_datadir}/ovirt-engine/setup/plugins/*/ovirt-engine-grafana-dwh/
%{_sysconfdir}/grafana/conf/

%changelog
* Wed Mar 23 2022 jiangxinyu <jiangxinyu@kylinos.cn> - 4.4.4.1-2
- Change requires from postgresql-13-contrib and postgresql-13-server to postgresql-contrib and postgresql-server

* Thu Sep 16 2021 Ge Wang <wangge20@huawei.com> - 4.4.4.1-1
- Init package
