## -------------------------------------------------------------------
##
## Copyright (c) 2014 Basho Technologies, Inc.
##
## This file is provided to you under the Apache License,
## Version 2.0 (the "License"); you may not use this file
## except in compliance with the License.  You may obtain
## a copy of the License at
##
##   http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing,
## software distributed under the License is distributed on an
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
## KIND, either express or implied.  See the License for the
## specific language governing permissions and limitations
## under the License.
##
## -------------------------------------------------------------------
%global nfsmountable 0
%global _scl_prefix {{scl_prefix}}
%global scl {{scl}}

%{?scl:%scl_package oz-worker}
%{!?scl:%global pkg_name %{name}}

Name: %{?scl_prefix}{{package_name}}
Version: {{version_}}
Release: {{release_}}%{?dist}
License: {{license_type}}
Group: Development/Libraries
Source: {{tarname_}}
Source1: init.script
URL: {{vendor_url}}
Vendor: {{vendor_name}}
Packager: {{vendor_contact_name}} <{{vendor_contact_email}}>
BuildRoot: %{_tmppath}/%{name}-{{revision_}}-%{release}-root
Summary: {{package_shortdesc}}
Obsoletes: %{?scl_prefix}{{package_name}}

Requires: initscripts
{{rpm_requires}}
{{rpm_build_requires}}

%undefine _missing_build_ids_terminate_build
%define _unpackaged_files_terminate_build 0

%description
{{package_desc}}

%define _sysconfdir %{_scl_root}/etc
%define _localstatedir %{_scl_root}/var
%define _sharedstatedir %{_scl_root}/var/lib

%define init_script %{_sysconfdir}/init.d/{{package_install_name}}
%define debug_package %{nil}
%define __prelink_undo_cmd /bin/cat prelink library

%define platform_bin_dir %{_{{bin_or_sbin}}dir}
%define platform_data_dir %{_localstatedir}/lib/{{package_install_name}}
%define platform_etc_dir %{_sysconfdir}/{{package_install_name}}
%define platform_base_dir %{_libdir}/{{package_install_name}}
%define platform_lib_dir %{platform_base_dir}/lib
%define platform_log_dir %{_localstatedir}/log/{{package_install_name}}
%define platform_run_dir %{_localstatedir}/run


%prep
{{rpm_prep}}
%setup -q -n {{tarname_base_}}

# Setup vars.config like other platforms, but do it inside of spec file
cat > rpm.vars.config <<EOF
%% Platform-specific installation paths
{platform_bin_dir,    "%{platform_bin_dir}"}.
{platform_data_dir,   "%{platform_data_dir}"}.
{platform_etc_dir,    "%{platform_etc_dir}"}.
{platform_base_dir,   "%{platform_base_dir}"}.
{platform_lib_dir,    "%{platform_lib_dir}"}.
{platform_log_dir,    "%{platform_log_dir}"}.
{platform_run_dir,    "%{platform_run_dir}"}.

{runner_script_dir,   "%{platform_bin_dir}"}.
{runner_base_dir,     "%{platform_base_dir}"}.
{runner_data_dir,     "%{platform_data_dir}"}.
{runner_etc_dir,      "%{platform_etc_dir}"}.
{runner_log_dir,      "%{platform_log_dir}"}.
{runner_lib_dir,      "%{platform_lib_dir}"}.
{runner_patch_dir,    "%{platform_lib_dir}/{{package_patch_dir}}"}.
{runner_run_dir,      "%{platform_run_dir}"}.
{runner_user,         "{{package_runner_user}}"}.
{runner_wait_process, "{{package_runner_wait_process}}"}.
{pipe_dir,            "%{_localstatedir}/run/{{package_install_name}}/"}.
{app_version,         "{{revision_}}"}.
EOF

%build
. /opt/rh/devtoolset-7/enable
. /opt/onedata/%{scl}/enable
mkdir -p %{_builddir}/tmp/cmake3
ln -nsf /usr/bin/cmake3 %{_builddir}/tmp/cmake3/cmake
OVERLAY_VARS="--overlay_vars=rpm.vars.config" \
LD_LIBRARY_PATH="/opt/onedata/{{scl}}/root/usr/lib64" \
LDFLAGS="-L/opt/onedata/{{scl}}/root/usr/lib64" \
CFLAGS="-I/opt/onedata/{{scl}}/root/usr/include" \
CXXFLAGS="-I/opt/onedata/{{scl}}/root/usr/include -L/opt/onedata/{{scl}}/root/usr/lib64" \
CMAKE_CXX_FLAGS="-I/opt/onedata/{{scl}}/root/usr/include" \
CPLUS_INCLUDE_PATH="/opt/onedata/{{scl}}/root/usr/include" \
CMAKE_INCLUDE_PATH="/opt/onedata/{{scl}}/root/usr/include" \
PATH=$PATH:/opt/onedata/{{scl}}/root/usr/lib64:%{_builddir}/tmp/cmake3 \
make REBAR_PROFILE=package rel

%install
%define relpath       %{_builddir}/%{buildsubdir}/_build/package/rel/{{package_install_name}}
%define buildroot_etc %{buildroot}%{_sysconfdir}/{{package_install_name}}
%define buildroot_lib %{buildroot}%{_libdir}/{{package_install_name}}
%define buildroot_var %{buildroot}%{_localstatedir}

mkdir -p %{buildroot_etc}
mkdir -p %{buildroot_lib}
mkdir -p %{buildroot}%{_localstatedir}/lib/{{package_install_name}}
mkdir -p %{buildroot}%{_localstatedir}/log/{{package_install_name}}
mkdir -p %{buildroot}%{_localstatedir}/run/{{package_install_name}}

cp -R %{relpath}/lib       %{buildroot_lib}
cp -R %{relpath}/erts-*    %{buildroot_lib}
cp -R %{relpath}/releases  %{buildroot_lib}

mkdir -p %{buildroot}%{_{{bin_or_sbin}}dir}
if [ -d %{relpath}/bin ]; then \
   find %{relpath}/bin -type f \
        -exec install -p -D -m 0755 {} %{buildroot}%{_{{bin_or_sbin}}dir}/ \; ;fi

# Scan for manpages that are optional for each command in the package_commands list
# If found:
#     * install manpages
#     * add manpages to the 'additional_files_list' that will later be added in the %files section
# mkdir -p %{buildroot}%{_mandir}/man1
# touch additional_files_list
if [ -d %{_builddir}/%{buildsubdir}/doc/man/man1 ]; then \
   {{#package_commands}}if [ -f %{_builddir}/%{buildsubdir}/doc/man/man1/{{name}}.1.gz ]; then \
                            install -p -D -m 0455 %{_builddir}/%{buildsubdir}/doc/man/man1/{{name}}.1.gz \
                                    %{buildroot}%{_mandir}/man1 \
                            ; echo "%{_mandir}/man1/{{name}}.1.gz" >> additional_files_list \
                        ; fi && \
   {{/package_commands}}echo -n; fi

cp -R %{relpath}/etc/* %{buildroot_etc}

mkdir -p %{buildroot_var}/lib/{{package_install_name}}
cp -R %{relpath}/data/* \
      %{buildroot_var}/lib/{{package_install_name}}

mkdir -p %{buildroot}%{?scl:%_root_sysconfdir}%{!?scl:%_sysconfdir}/init.d
install -m755 %SOURCE1 %{buildroot}%{?scl:%_root_sysconfdir}%{!?scl:%_sysconfdir}/init.d/{{package_install_name}}

# Needed to work around check-rpaths which seems to be hardcoded into recent
# RPM releases
export QA_RPATHS=3


%pre
# Pre-install script
if ! getent group {{package_install_group}} >/dev/null 2>&1; then
   groupadd -r {{package_install_group}}
fi

if getent passwd {{package_install_user}} >/dev/null 2>&1; then
   usermod -d %{_localstatedir}/lib/{{package_install_name}} {{package_install_user}} || true
else
   useradd -r -g {{package_install_group}} \
           --home %{_localstatedir}/lib/{{package_install_name}} \
           --comment "{{package_install_user_desc}}" \
           --shell /bin/bash \
           {{package_install_user}}
fi


%post
# Post Installation Script

# For distros with SELinux (RHEL/Fedora)
if [ `which selinuxenabled > /dev/null 2>&1` ] ; then
   # Fixup perms for SELinux (if it is enabled)
   selinuxenabled && find %{_localstatedir}/lib/{{package_install_name}} -name "*.so" -exec chcon -t textrel_shlib_t {} \;
fi

# Make sure shell library file is readable
chmod 0755 %{_libdir}/{{package_install_name}}/lib/env.sh

# Add application to chkconfig, but default to "off"
/sbin/chkconfig --add {{package_install_name}}
/sbin/chkconfig {{package_install_name}} off

# Run user-defined init scripts
{{post_install_rpm_script}}

%preun
# Pre-uninstall script

# Only on uninstall, not upgrades
if [ "$1" = 0 ] ; then
   /sbin/service {{package_install_name}} stop > /dev/null 2>&1
   /sbin/chkconfig --del {{package_install_name}}
fi
exit 0


# Man pages are optional and might be missing, read from file
%files
%defattr(-,{{package_install_user}},{{package_install_group}})
%{_libdir}/*
%dir %{_sysconfdir}/{{package_install_name}}
%config(noreplace) %{_sysconfdir}/{{package_install_name}}/*
%{_{{bin_or_sbin}}dir}/*
%{_localstatedir}/lib/{{package_install_name}}
%{_localstatedir}/log/{{package_install_name}}
%{_localstatedir}/run/{{package_install_name}}
%{?scl:%_root_sysconfdir}%{!?scl:%_sysconfdir}/init.d/{{package_install_name}}

%clean
rm -rf %{buildroot}
