## -------------------------------------------------------------------
##
## Copyright (c) 2014 Basho Technologies, Inc.; 2021 TI Tokyo
##
## 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.
##
## -------------------------------------------------------------------

Name: stanchion
Version: %{_version}
Release: %{_release}%{?dist}
License: "Apache 2.0"
Group: Development/Libraries
Source: %{_tarname}
URL: "https://tiot.jp"
Vendor: "TI Tokyo"
Packager: "Stanchion Package Maintainer" <"andriy.zavada@tiot.jp">
BuildRoot: %{_tmppath}/%{name}-%{_revision}-%{release}-root
Summary: "Request serialization service for Riak CS"
BuildRequires: systemd
Requires: logrotate
Requires(post): systemd
Requires(preun): systemd
Requires(postun): systemd

%description
"Stanchion is a request serialization service for Riak CS"

%define debug_package %{nil}
%define __prelink_undo_cmd /bin/cat prelink library

%define platform_etc_dir %{_sysconfdir}/stanchion
%define platform_base_dir %{_libdir}/stanchion
%define platform_bin_dir %{platform_base_dir}/bin
%define platform_lib_dir %{platform_base_dir}/lib
%define platform_log_dir %{_localstatedir}/log/stanchion

%prep
%setup -q -n %{_tarname_base} -c %{_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_etc_dir,  "%{platform_etc_dir}"}.
{platform_base_dir, "%{platform_base_dir}"}.
{platform_lib_dir,  "%{platform_lib_dir}"}.
{platform_log_dir,  "%{platform_log_dir}"}.
EOF

%define _build_id_links none

%build
make rel-rpm

%install
%define relpath %{_builddir}/%{buildsubdir}/rel/stanchion
%define buildroot_lib %{buildroot}%{_libdir}/stanchion
%define buildroot_etc %{buildroot}%{_sysconfdir}/stanchion
%define buildroot_bin %{buildroot_lib}/bin
%define buildroot_sbin %{buildroot}%{_sbindir}
%define buildroot_unit %{buildroot}%{_unitdir}
%define buildroot_run %{buildroot}%{_rundir}

mkdir -p \
  %{buildroot_etc} \
  %{buildroot_lib} \
  %{buildroot_bin} \
  %{buildroot_sbin} \
  %{buildroot_unit} \
  %{buildroot_run}/stanchion \
  %{buildroot}%{_localstatedir}/lib/stanchion \
  %{buildroot}%{_localstatedir}/log/stanchion

cp -R %{relpath}/lib       %{buildroot_lib}
cp -R %{relpath}/erts-*    %{buildroot_lib}
cp -R %{relpath}/releases  %{buildroot_lib}
cp -R %{relpath}/share     %{buildroot_lib}
cp rel/pkg/rpm/%{name}.service %{buildroot_unit}

if [ -d %{relpath}/bin ]; then \
   cd %{relpath}/bin
   find . -type f -exec install -Dm 0755 {} %{buildroot_bin}/{} \;
   cd -
fi

cp %{relpath}/etc/stanchion.conf %{buildroot_etc}
cp %{relpath}/etc/advanced.config %{buildroot_etc}
for f in stanchion-admin stanchion-debug; do
    cp %{relpath}/bin/$f %{buildroot}/usr/sbin
done
cp %{relpath}/usr/bin/stanchion %{buildroot}/usr/sbin

mkdir -p %{buildroot}%{_localstatedir}/lib/stanchion

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


%pre
if ! getent group stanchion >/dev/null 2>&1; then
   groupadd -r stanchion
fi

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

%post
# 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/stanchion -name "*.so" -exec chcon -t textrel_shlib_t {} \;
fi

%systemd_post %{name}.service

%preun
%systemd_preun %{name}.service

%postun
%systemd_postun %{name}.service
rm -f %{_sysconfdir}/stanchion/stanchion.conf.new
rm -rf %{_localstatedir}/lib/stanchion
rm -rf %{_localstatedir}/log/stanchion

%files
%defattr(-,root,root)
%{_libdir}/*
%{_unitdir}/%{name}.service
%{_sbindir}/*

%defattr(-,stanchion,stanchion)
%{_sysconfdir}/stanchion
%{_localstatedir}/lib/stanchion
%{_localstatedir}/log/stanchion

%config(noreplace) %{_sysconfdir}/stanchion/*

%clean
rm -rf %{buildroot}
