# spec file for package coolwsd
#
# Copyright (c) 2023 Collabora
#
# This file is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

Name:           coolwsd
Version:        @PACKAGE_VERSION@
Release:        1
Vendor:         Collabora Productivity Ltd.
Summary:        Collabora Online WebSocket Daemon
License:        EULA
Source0:        coolwsd-@PACKAGE_VERSION@.tar.gz
BuildRequires:  libcap-devel pam-devel rsync
Requires:       collaboraoffice >= 26.04.0, collaboraoffice-ure >= 26.04.0, collaboraofficebasis-core >= 26.04.0, collaboraofficebasis-writer >= 26.04.0, collaboraofficebasis-impress >= 26.04.0, collaboraofficebasis-graphicfilter >= 26.04.0, collaboraofficebasis-en-US >= 26.04.0, collaboraofficebasis-calc >= 26.04.0, collaboraofficebasis-ooofonts >= 26.04.0, collaboraofficebasis-images >= 26.04.0, collaboraofficebasis-draw >= 26.04.0, collaboraofficebasis-extension-pdf-import >= 26.04.0, collaboraofficebasis-ooolinguistic >= 26.04.0, collaboraofficebasis-math >= 26.04.0
Requires(post): coreutils grep sed cpio
Provides: loolwsd
Obsoletes: loolwsd collaboraoffice-dict-br collaboraoffice-dict-et collaboraoffice-dict-gd collaboraoffice-dict-gu collaboraoffice-dict-hi collaboraoffice-dict-lt collaboraoffice-dict-lv collaboraoffice-dict-sr collaboraoffice-dict-te collaboraofficebasis-as collaboraofficebasis-bn-IN collaboraofficebasis-ast collaboraofficebasis-br collaboraofficebasis-ca-valencia collaboraofficebasis-et collaboraofficebasis-gd collaboraofficebasis-gu collaboraofficebasis-hi collaboraofficebasis-km collaboraofficebasis-kn collaboraofficebasis-lt collaboraofficebasis-lv collaboraofficebasis-ml collaboraofficebasis-mr collaboraofficebasis-nn collaboraofficebasis-or collaboraofficebasis-pa-IN collaboraofficebasis-sr collaboraofficebasis-sr-Latn collaboraofficebasis-ta collaboraofficebasis-te

%define _source_filedigest_algorithm 8
%define _binary_filedigest_algorithm 8
%define _source_payload w2.xzdio
%define _binary_payload w2.xzdio

%description

%package deprecated
Summary: Collabora Online WebSocket Daemon Deprecated Utilities
Requires: %{name} = %{version}-%{release}
Requires(post): /usr/sbin/setcap

%description deprecated

%prep
%setup -n coolwsd-@PACKAGE_VERSION@

%build
%configure \
	--with-lokit-path=bundled/include \
	--with-lo-path=/opt/collaboraoffice \
	--disable-tests \
%if 0%{?config_options:1}
	%{config_options}
%endif

env BUILDING_FROM_RPMBUILD=yes make %{?_smp_mflags}

%check
#env BUILDING_FROM_RPMBUILD=yes make check

%install
env BUILDING_FROM_RPMBUILD=yes make install DESTDIR=%{buildroot}
install -D -m 444 coolwsd.service %{buildroot}/usr/lib/systemd/system/coolwsd.service
# Apache webserver on RH-like distributions
install -d -m 755 %{buildroot}/etc/httpd/conf
install -D -m 755 etc/apache2/coolwsd.conf %{buildroot}/etc/httpd/conf
mkdir -p %{buildroot}/etc/pam.d
echo "auth       required     pam_unix.so" > %{buildroot}/etc/pam.d/coolwsd
echo "account    required     pam_unix.so" >>  %{buildroot}/etc/pam.d/coolwsd

%files
%defattr(-,root,root,-)
/usr/bin/coolwsd
/usr/bin/loolwsd
/usr/bin/coolwsd-systemplate-setup
/usr/bin/loolwsd-systemplate-setup
/usr/bin/coolforkit-ns
/usr/bin/coolconvert
/usr/bin/coolconfig
/usr/bin/loolconfig
/usr/bin/coolstress
/usr/share/coolwsd/discovery.xml
/usr/share/coolwsd/favicon.ico
/usr/share/coolwsd/browser
/usr/share/doc/coolwsd/README.vars
/usr/share/doc/coolwsd/metrics.txt
/usr/share/doc/coolwsd/protocol.txt
/usr/share/doc/coolwsd/THIRDPARTYLICENSES
/usr/share/doc/coolwsd/collabora-online-sbom.spdx.json
/usr/share/man/man1/coolwsd.1.gz
/usr/share/man/man1/coolforkit-ns.1.gz
/usr/share/man/man1/coolconvert.1.gz
/usr/share/man/man1/coolconfig.1.gz
/usr/share/man/man1/coolstress.1.gz
/usr/share/man/man1/coolwsd-systemplate-setup.1.gz
/usr/lib/systemd/system/coolwsd.service

%config(noreplace) /etc/pam.d/coolwsd
%config(noreplace) %attr(640, cool, root) /etc/coolwsd/coolwsd.xml
%config /etc/coolwsd/coolkitconfig.xcu
%config(noreplace) /etc/nginx/snippets/coolwsd.conf
%config(noreplace) /etc/apache2/conf-available/coolwsd.conf
%config(noreplace) /etc/httpd/conf/coolwsd.conf

%doc README.md

%files deprecated
%defattr(-,root,root,-)
/usr/bin/coolforkit-caps
/usr/bin/coolmount
/usr/share/man/man1/coolforkit-caps.1.gz
/usr/share/man/man1/coolmount.1.gz

%pre
getent group cool >/dev/null || groupadd -r cool
getent passwd cool >/dev/null || useradd -g cool -r cool -d /opt/cool -s /bin/bash

%post
if [ -f /etc/loolwsd/loolwsd.xml ]; then /usr/bin/coolconfig migrateconfig --write; fi
# compatibility with older systemd versions
SYSTEMD_VERSION=$(busctl --system get-property org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager Version | grep -Eo [0-9]{3} | head -n 1)
if [[ "$SYSTEMD_VERSION" -lt "228" ]]; then
    sed -i "/^ProtectSystem/d" /usr/lib/systemd/system/coolwsd.service
fi
if [[ "$SYSTEMD_VERSION" -lt "231" ]]; then
    sed -i "/^ReadWritePaths/d" /usr/lib/systemd/system/coolwsd.service
fi
if [[ "$SYSTEMD_VERSION" -lt "232" ]]; then
    sed -i "/^ProtectControlGroups/d" /usr/lib/systemd/system/coolwsd.service
fi

if [ $1 -eq 1 ] ; then
    # Initial installation
    systemctl preset coolwsd.service >/dev/null 2>&1 || :
fi

systemctl is-active -q coolwsd && COOLWSD_IS_ACTIVE=1 || COOLWSD_IS_ACTIVE=0
if [ $COOLWSD_IS_ACTIVE == "1" ]; then systemctl stop coolwsd; fi

# Figure out where LOKit is installed, let's hope it is not a mount point
# Create a directory for coolwsd on the same file system
lokitroot=/opt/collaboraoffice
coolparent=`cd ${lokitroot} && cd .. && /bin/pwd`

rm -rf ${coolparent}/cool || true
mkdir -p ${coolparent}/cool/child-roots ${coolparent}/cool/cache
chown cool:cool ${coolparent}/cool
chown cool:cool ${coolparent}/cool/child-roots
chown cool:cool ${coolparent}/cool/cache

fc-cache ${lokitroot}/share/fonts/truetype
coolwsd-systemplate-setup ${coolparent}/cool/systemplate ${lokitroot} >/dev/null 2>&1
coolconfig generate-proof-key >/dev/null 2>&1

if [ $COOLWSD_IS_ACTIVE == "1" ]; then systemctl start coolwsd; fi

%post deprecated
setcap cap_fowner,cap_chown,cap_sys_chroot=ep /usr/bin/coolforkit-caps
setcap cap_sys_admin=ep /usr/bin/coolmount

%preun
if [ $1 -eq 0 ]; then
    systemctl --no-reload disable coolwsd.service > /dev/null 2>&1 || :
    systemctl stop coolwsd.service > /dev/null 2>&1 || :
fi

%postun
if [ $1 -eq 0 ]; then
    systemctl daemon-reload >/dev/null 2>&1 || :
fi

%changelog
* Mon Aug 12 2024 Caolán McNamara
- split out a deprecated subpackage of the helpers not needed with namespaces
* Mon Aug 03 2015 Mihai Varga
- added the cronjob
* Tue May 19 2015 Tor Lillqvist
- Initial RPM release
