File draupnir.spec of Package draupnir

#
# spec file for package draupnir
#
# Copyright (c) 2024 SUSE LLC
# Copyright (c) 2024 Georg Pfuetzenreuter <mail+rpm@georg-pfuetzenreuter.net>
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#


%define name0        Draupnir
%define mydatadir    %{_datadir}/%{name}
%define myhomedir    %{_sharedstatedir}/%{name}
%define mylibdir     %{_libdir}/%{name}
%define mylibexecdir %{_libexecdir}/%{name}
%define mysysconfdir %{_sysconfdir}/%{name}
%if 0%{suse_version} > 1600
%define node_version 22
%else
%define node_version 20
%endif
Name:           draupnir
Version:        2.6.1+git.0.f1dad52
Release:        0
Summary:        Matrix moderation bot
License:        AFL-3.0 AND Apache-2.0 AND CC-BY-SA-4.0 AND CC0-1.0
URL:            https://the-draupnir-project.github.io/draupnir-documentation
Source0:        %{name0}-%{version}.tar.zst
Source1:        vendor.tar.zst
Source2:        %{name}-appservice.service
Source3:        %{name}-bot.service
Source4:        %{name}.apparmor
BuildRequires:  apparmor-rpm-macros
BuildRequires:  fdupes
BuildRequires:  rsync
BuildRequires:  sysuser-tools
BuildRequires:  zstd
Requires:       nodejs%{node_version}
%sysusers_requires
%{?systemd_ordering}

%description
A Matrix moderation bot.

%prep
%autosetup -a 1 -n %{name0}-%{version} -p 1
find . -type f -execdir sed -i '1!b;/usr\/bin\/env node$/d' {} +  #sed -Ei 's?(/usr/bin/)env (node)$?\1\2?' {} +

tee > %{name}.sysuser <<EOF
u %{name} - 'Draupnir service user' %{myhomedir} -
EOF

%build
# nothing to build, javascript vendoring :(

%sysusers_generate_pre %{name}.sysuser %{name}

%install
install -d %{buildroot}%{mydatadir} %{buildroot}%{myhomedir} %{buildroot}%{mylibdir} %{buildroot}%{mylibexecdir} %{buildroot}%{mysysconfdir} %{buildroot}%{_sysconfdir}/apparmor.d %{buildroot}%{_unitdir}

rsync -r lib node_modules package.json version.txt %{buildroot}%{mydatadir}

mv %{buildroot}%{mydatadir}/node_modules/@matrix-org/matrix-sdk-crypto-nodejs/matrix-sdk-crypto.linux-x64-gnu.node %{buildroot}%{mylibexecdir}
ln -s %{mylibexecdir}/matrix-sdk-crypto.linux-x64-gnu.node %{buildroot}%{mydatadir}/node_modules/@matrix-org/matrix-sdk-crypto-nodejs/matrix-sdk-crypto.linux-x64-gnu.node

mv %{buildroot}%{mydatadir}/node_modules/better-sqlite3/build/Release %{buildroot}%{mylibdir}/better-sqlite3
ln -s %{mylibdir}/better-sqlite3 %{buildroot}%{mydatadir}/node_modules/better-sqlite3/build/Release

sed 's?/data/storage?%{myhomedir}?' config/default.yaml > %{buildroot}%{mysysconfdir}/production.yaml

putconf () {
  sed \
    -e 's?__DATADIR__?%{mydatadir}?'        \
    -e 's?__HOMEDIR__?%{myhomedir}?'        \
    -e 's?__LIBDIR__?%{mylibdir}?'          \
    -e 's?__LIBEXECDIR__?%{mylibexecdir}?'  \
    -e 's?__SYSCONFDIR__?%{mysysconfdir}?g' \
    -e 's?__NODE_VERSION__?%{node_version}?g' \
    $1 > %{buildroot}$2
}

for unitfile in %{SOURCE2} %{SOURCE3}
do
  putconf $unitfile %{_unitdir}/$(basename $unitfile)
done

putconf %{SOURCE4} %{_sysconfdir}/apparmor.d/%{name}

install -Dm644 %{name}.sysuser %{buildroot}%{_sysusersdir}/%{name}.conf

%fdupes %{buildroot}%{mydatadir}

%pre -f %{name}.pre
%service_add_pre %{name}-appservice.service
%service_add_pre %{name}-bot.service

%post
%{apparmor_reload %{_sysconfdir}/apparmor.d/%{name}}
%service_add_post %{name}-appservice.service
%service_add_post %{name}-bot.service

%preun
%service_del_preun %{name}-appservice.service
%service_del_preun %{name}-bot.service

%postun
%service_del_postun %{name}-appservice.service
%service_del_postun %{name}-bot.service

%files
%license LICENSES
%doc README.md
%dir %{mydatadir}
%attr(0750, %{name}, %{name}) %dir %{myhomedir}
%dir %{mylibdir}
%dir %{mylibexecdir}
%attr(0750, %{name}, %{name}) %dir %{mysysconfdir}
%{mydatadir}/lib
%{mydatadir}/node_modules
%{mydatadir}/package.json
%{mydatadir}/version.txt
%{mylibdir}/better-sqlite3
%{mylibexecdir}/matrix-sdk-crypto.linux-x64-gnu.node
%attr(0640, %{name}, %{name}) %config(noreplace) %{mysysconfdir}/production.yaml
%{_sysusersdir}/%{name}.conf
%{_unitdir}/%{name}-appservice.service
%{_unitdir}/%{name}-bot.service

%dir %{_sysconfdir}/apparmor.d
%config %{_sysconfdir}/apparmor.d/%{name}

%changelog
openSUSE Build Service is sponsored by