File nextcloud.spec of Package nextcloud

#
# spec file for package nextcloud
#
# Copyright (c) 2024 SUSE LLC
#
# 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/
#


%if 0%{?suse_version}
%define apache_docroot %{apache_serverroot}/htdocs
%define apache_confdir %{apache_sysconfdir}/conf.d
%define ap_pkgname apache2
%else
%if 0%{?fedora_version} || 0%{?rhel_version} || 0%{?centos_version} || 0%{?almalinux_version}
# apache_serverroot #(apxs -q PREFIX) == /etc/httpd
%define apache_serverroot /var/www
%define apache_docroot /var/www/html
# apache_sysconfdir #(apxs -q SYSCONFDIR) == /etc/httpd/conf
%define apache_confdir /etc/httpd/conf.d
%define apache_user apache
%define apache_group apache
%define ap_pkgname httpd
%define __jar_repack 0
%else
%define apache_serverroot /var/www
%define apache_docroot /var/www/html
%define apache_confdir /etc/httpd/conf.d
%define apache_user www
%define apache_group www
%define ap_pkgname httpd
%endif
%endif

%if 0%{?suse_version}
%define nc_dir          %{apache_serverroot}/%{name}
%define nc_datadir      /srv/%{name}-data
%else
%if 0%{?fedora_version} || 0%{?rhel_version} || 0%{?centos_version} || 0%{?almalinux_version}
# /var/www/nextcloud
%define nc_dir          %{apache_serverroot}/%{name}
%define nc_datadir      /srv/%{name}-data
%else
%define nc_dir          %{apache_serverroot}/%{name}
%define nc_datadir      /srv/%{name}-data
%endif
%endif
%define nc_user         %{apache_user}
%define statedir        /run

Name:           nextcloud
Version:        27.1.11
Release:        0
Summary:        Content Collaboration Platform
License:        AGPL-3.0-only
Group:          Productivity/Networking/Web/Utilities
URL:            https://nextcloud.com
Source0:        https://download.nextcloud.com/server/releases/%{name}-%{version}.tar.bz2
Source1:        https://download.nextcloud.com/server/releases/%{name}-%{version}.tar.bz2.asc
# Source2:        https://nextcloud.com/nextcloud.asc
# OBS wants a 'keyring' file which is obviously the puplic key
Source2:        nextcloud.keyring
Source5:        %{name}-cron
Source6:        %{name}-cron.service
Source7:        %{name}-cron.timer
Source8:        %{name}.http
Source9:        %{name}.http.config.inc
Source10:       %{name}.http.mod_php.inc
Source11:       README
Source12:       README.AlmaLinux.md
Source13:       README.SELinux
Source14:       README.SUSE
Source20:       robots.txt
Source99:       %{name}.rpmlintrc
Source100:      %{name}-apps.tar.bz2
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  apache-rpm-macros
BuildRequires:  cron
BuildRequires:  fdupes
%endif
BuildRequires:  unzip
#
Requires:       curl
Requires:       php-bcmath
Requires:       php-bz2
Requires:       php-dom
Requires:       php-gd
Requires:       php-gmp
Requires:       php-intl
Requires:       php-json
Requires:       php-mbstring
Requires:       php-posix
Requires:       php-zip
# Not with prefork, but event (apache2-event)
Recommends:     php-fpm
#
%if 0%{?fedora_version} || 0%{?rhel_version} || 0%{?centos_version} || 0%{?almalinux_version}
Requires:       libxml2
Requires:       php < 8.3.0
Requires:       php >= 8.1.0
Recommends:     php >= 8.2.0
#
Requires:       php-mysqlnd
Requires:       php-process
Requires:       php-xml
# NOT included in RHEL 9 php >= 8.1 appstream
## you need REMI's repo
Recommends:     php-ldap
Recommends:     php-pecl-apcu
Recommends:     php-sodium
#
Recommends:     mariadb-server >= 10.2
Recommends:     mod_ssl
Recommends:     sqlite
# dnf install epel-release
Recommends:     php-pecl-redis6
Suggests:       php-pecl-imagick
%endif
#
%if 0%{?suse_version}
Requires:       libxml2-2
Requires:       php-cli
Requires:       php-ctype
Requires:       php-curl
# SUSE does not include the fileinfo module in php-common.
Requires:       php-fileinfo
Requires:       php-iconv
Requires:       php-imagick
Requires:       php-opcache
Requires:       php-openssl
Requires:       php-pear
Requires:       php-xmlreader
Requires:       php-xmlwriter
Requires:       php-zip
Requires:       php-zlib
Recommends:     cron
Recommends:     mariadb >= 10.2
Recommends:     php-APCu
Recommends:     php-exif
Recommends:     php-ffmpeg
Recommends:     php-mysql
Recommends:     php-pcntl
Recommends:     php-redis
Recommends:     php-sqlite
Recommends:     php-sysvsem
Recommends:     sqlite3
%{?systemd_requires}
%endif

%description
Nextcloud is a suite of client-server software for creating file
hosting services and using them.

%package apache
Summary:        Apache configuration for nextcloud
Group:          Productivity/Networking/Web/Utilities
BuildRequires:  %{ap_pkgname} >= 2.4
Requires:       %{ap_pkgname}
Requires:       %{name} = %{version}
%if 0%{?suse_version}
# only with prefork
Recommends:     mod_php_any < 8.3.0
Recommends:     mod_php_any >= 8.1.0
Supplements:    packageand(%{ap_pkgname}:%name)
%endif

%description apache
This subpackage contains the Apache configuration files

%package apps
Summary:        Nextcloud Apps for offline installation
Group:          Productivity/Networking/Web/Utilities
Requires:       %{name} = %{version}

%description apps
This subpackage contains some important apps when you don't have
 access to nextcloud app store.
  * files_antivirus (5.5.7)
  * twofactor_admin (4.5.0)
  * twofactor_email (2.7.4)
  * twofactor_reminder (1.1.0)

%prep
%setup -q -n %{name} -a 100
cp %{SOURCE11} .
cp %{SOURCE12} .
cp %{SOURCE13} .
cp %{SOURCE14} .
### Don't remove git files!!
### git files should not be removed, otherwise nextcloud rise up integrity check failure in some situations.
###
## delete unneeded gitfiles
#rm -r `find . -name ".gitignore" -or -name ".gitkeep" -or -name ".github"`
## remove entries in signature.json to prevent integrity check failure
#find . -iname signature.json \
#    -exec sed -i "/\/.gitignore\": ./d" "{}" \;  \
#    -exec sed -i "/\/.gitkeep\": ./d" "{}" \; \
#    -exec sed -i "/\/.github\": ./d" "{}" \;
#rm -f 3rdparty/symfony/debug/Resources/ext/*.{c,h}

%build

%install
# no server side java code contained, alarm is false
install -d %{buildroot}/%{nc_dir}/search
install -d -m 0750 %{buildroot}/%{nc_datadir}

cp -a .htaccess .user.ini * %{buildroot}/%{nc_dir}/
rm -f %{buildroot}/%{nc_dir}/README*

# install apache config and php includes
install -p -D -m 644 %{SOURCE8} %{buildroot}/%{apache_confdir}/%{name}.conf
install -p -D -m 644 %{SOURCE9} %{buildroot}/%{apache_confdir}/%{name}.config.inc
install -p -D -m 644 %{SOURCE10} %{buildroot}/%{apache_confdir}/%{name}.mod_php.inc
sed -i \
  -e "s|@NC_DIR@|%{nc_dir}|g" \
  -e "s|@APACHE_CONFDIR@|%{apache_confdir}|g" %{buildroot}/%{apache_confdir}/%{name}.conf
sed -i \
  -e "s|@NC_DIR@|%{nc_dir}|g" \
  -e "s|@APACHE_CONFDIR@|%{apache_confdir}|g" %{buildroot}/%{apache_confdir}/%{name}.config.inc

# not needed for distro packages
#rm -f ${idir}/indie.json

%if 0%{?suse_version}
# link duplicate doc files
%fdupes -s %{buildroot}/%{nc_dir}
%endif

# CronJob
install -d -m 0755 %{buildroot}%{_sysconfdir}/cron.d
install -D -m 0644 %{SOURCE5} %{buildroot}%{_sysconfdir}/cron.d/%{name}
install -D -m 0644 %{SOURCE6} %{buildroot}%{_unitdir}/%{name}-cron.service
install -D -m 0644 %{SOURCE7} %{buildroot}%{_unitdir}/%{name}-cron.timer
sed -i \
  -e "s|@APACHE_USER@|%{apache_user}|g" \
  -e "s|@APACHE_SERVERROOT@|%{apache_serverroot}|g" %{buildroot}%{_sysconfdir}/cron.d/%{name}
sed -i \
  -e "s|@APACHE_USER@|%{apache_user}|g" \
  -e "s|@APACHE_SERVERROOT@|%{apache_serverroot}|g" %{buildroot}%{_unitdir}/%{name}-cron.service
%if 0%{?suse_version}
install -d %{buildroot}%{_sbindir}
ln -sf %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-cron
%endif

%pre
%if 0%{?suse_version}
%service_add_pre %{name}-cron.timer %{name}-cron.service
%endif

# avoid fatal php errors, while we are changing files
# https://github.com/nextcloud
#
# We don't do this for new installs. Only for updates.
# If the first argument to pre is 1, the RPM operation is an initial installation. If the argument is 2,
# the operation is an upgrade from an existing version to a new one.
# Upgrade mode
if [ ${1:-0} -eq 2 ]; then
  if [ ! -s %{statedir}/apache_stopped_during_nextcloud_install ]; then
    ### On first install of new package we do not have the new datadir yet
    ### Hence we do the data migration and do it only once
    if [ ! -d %{nc_datadir} ]; then
      echo "o %{name} pre-install: Nextcloud Data migration needed"
      touch %{statedir}/%{name}_data_migration_needed
    fi
    echo "o %{name} pre-install: Checking for running Apache"
    # FIXME: this above should make it idempotent -- a requirement with openSUSE.
    # it does not work.
%if 0%{?suse_version} && 0
  service apache2 status | grep running > %{statedir}/apache_stopped_during_nextcloud_install
  service apache2 stop
%endif
%if 0%{?fedora_version} || 0%{?rhel_version} || 0%{?centos_version} || 0%{?almalinux_version}
  service httpd status | grep running > %{statedir}/apache_stopped_during_nextcloud_install
  service httpd stop
%endif
  fi
fi
if [ -s %{statedir}/apache_stopped_during_nextcloud_install ]; then
  echo "o %{name} pre-install: Stopping Apache"
fi

# Install mode
if [ ${1:-0} -eq 1 ]; then
  echo "o %{name} pre-install: First install starting"
else
  echo "o %{name} pre-install: Upgrade starting ..."
fi
# https://github.com/nextcloud
if [ -x %{_bindir}/php -a -f %{nc_dir}/occ ]; then
  echo "o %{name} pre-install: occ maintenance:mode --on"
  su %{nc_user} -s /bin/sh -c "cd %{nc_dir}; PATH=%{_bindir}:$PATH php ./occ maintenance:mode --on" || true
  echo yes > %{statedir}/occ_maintenance_mode_during_nextcloud_install
fi

%post
%if 0%{?suse_version}
%service_add_post %{name}-cron.timer %{name}-cron.service
%endif

# Install mode
if [ ${1:-0} -eq 1 ]; then
%if 0%{?suse_version}
%if 0%{?sle_version} >= 150400 || 0%{?suse_version} > 1500
a2enmod php8
%else
a2enmod php7
%endif
%endif
  if [ ! -f  "%{nc_dir}/config/config.php" ]; then
    DOMAIN="$(hostname -f)"
    cat > %{nc_dir}/config/config.php << EOF
<?php
\$CONFIG = array (
  'datadirectory' => '%{nc_datadir}',
  'default_phone_region' => 'DE',
  'mail_smtpmode' => 'smtp',
  'mail_smtphost' => 'localhost',
  'mail_smtpport' => '25',
  'mail_from_address' => 'root',
  'mail_domain' => '${DOMAIN}',
);
EOF
    chown %{apache_user}: %{nc_dir}/config/config.php
  fi
fi

# # Upgrade mode
# if [ ${1:-0} -eq 2 ]; then
#   ### On first install of new package we do not have the new datadir yet
#   ### Hence we do the data migration and do it only once
#   if [ -f %{statedir}/%{name}_data_migration_needed ]; then
#     echo "o %{name} post-install: Nextcloud Data migration ..."
#     # copy config.php to new destination
#     if [ -s %{nc_dir_old}/config/config.php ]; then
#       cp -a %{nc_dir_old}/config/config.php %{nc_dir}/config/
#       sed -i -e "s|\(.*'datadirectory'\).*|\1 => '%{nc_datadir}',|g"  %{nc_dir}/config/config.php
#       mv %{nc_dir_old}/config %{nc_dir_old}/config.bkp
#     fi
#     # Migrate old data dir to new destination
#     if [ -d %{nc_datadir_old} ]; then
#       rsync -aH %{nc_datadir_old}/ %{nc_datadir}/
#       mv %{nc_datadir_old} %{nc_datadir_old}.bkp
#     fi
#     # Migrate apps
#     if [ -d %{nc_dir_old}/apps ]; then
#       rsync -aH %{nc_dir_old}/apps %{nc_dir}/
#       mv %{nc_dir_old}/apps %{nc_dir_old}/apps.bkp
#     fi
#     # Migrate core/img/filetypes
#     if [ -d %{nc_dir_old}/core ]; then
#       rsync -aH %{nc_dir_old}/core %{nc_dir}/
#       mv %{nc_dir_old}/core %{nc_dir_old}/core.bkp
#     fi
#   fi
#   rm -f %{statedir}/%{name}_data_migration_needed
# fi

if [ -s %{statedir}/apache_stopped_during_nextcloud_install ]; then
  echo "o %{name} post-install: Restarting Apache"
  ## If we stopped apache in pre section, we now should restart. -- but *ONLY* then!
  ## Maybe delegate that task to occ upgrade? They also need to handle this, somehow.
  service %{ap_pkgname} start
fi

if [ -s %{statedir}/occ_maintenance_mode_during_nextcloud_install ]; then
  echo "o %{name} post-install: occ maintenance:repair (fix possible errors)"
  su %{nc_user} -s /bin/sh -c "cd %{nc_dir}; PATH=%{_bindir}:$PATH php ./occ maintenance:repair" || true
  echo "o %{name} post-install: occ db:add-missing-* (add missing db things)"
  su %{nc_user} -s /bin/sh -c "cd %{nc_dir}; PATH=%{_bindir}:$PATH php ./occ maintenance:mimetype:update-db" || true
  su %{nc_user} -s /bin/sh -c "cd %{nc_dir}; PATH=%{_bindir}:$PATH php ./occ db:add-missing-columns" || true
  su %{nc_user} -s /bin/sh -c "cd %{nc_dir}; PATH=%{_bindir}:$PATH php ./occ db:add-missing-indices" || true
  su %{nc_user} -s /bin/sh -c "cd %{nc_dir}; PATH=%{_bindir}:$PATH php ./occ db:add-missing-primary-keys" || true
  echo "o %{name} post-install: occ update apps"
  su %{nc_user} -s /bin/sh -c "cd %{nc_dir}; PATH=%{_bindir}:$PATH php ./occ app:update --all" || true
  echo "o %{name} post-install: occ upgrade"
  su %{nc_user} -s /bin/sh -c "cd %{nc_dir}; PATH=%{_bindir}:$PATH php ./occ upgrade" || true
  echo "o %{name} post-install: occ maintenance:mode --off"
  su %{nc_user} -s /bin/sh -c "cd %{nc_dir}; PATH=%{_bindir}:$PATH php ./occ maintenance:mode --off" || true
fi

rm -f %{statedir}/apache_stopped_during_nextcloud_install
rm -f %{statedir}/occ_maintenance_mode_during_nextcloud_install

# Install mode
if [ ${1:-0} -eq 1 ]; then
  echo "o %{name} post-install: First install complete"
else
  echo "o %{name} post-install: Upgrade complete"
fi

%preun
%if 0%{?suse_version}
%service_del_preun %{name}-cron.timer %{name}-cron.service
%endif

%postun
%if 0%{?suse_version}
%service_del_postun %{name}-cron.timer %{name}-cron.service
%endif

%files
%defattr(-,root,root)
%if 0%{?suse_version}
%doc README README.SUSE
%endif
%if 0%{?fedora_version} || 0%{?rhel_version} || 0%{?centos_version} || 0%{?almalinux_version}
%doc README README.AlmaLinux.md README.SELinux
%endif
%config(noreplace) %{_sysconfdir}/cron.d/%{name}
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-cron
%endif
%{_unitdir}/%{name}-cron.service
%{_unitdir}/%{name}-cron.timer
%{nc_dir}
%defattr(-,%{apache_user},%{apache_group})
%attr(0750,%{apache_user},%{apache_group}) %{nc_dir}/occ
%config(noreplace) %{nc_dir}/.user.ini
%{nc_dir}/apps
%exclude %{nc_dir}/apps/twofactor_admin
%exclude %{nc_dir}/apps/twofactor_email
%exclude %{nc_dir}/apps/twofactor_reminder
%{nc_dir}/config
%dir %{nc_dir}/core/img/filetypes
%{nc_dir}/core/img/filetypes/*
%{nc_dir}/core/js/mimetypelist.js
%attr(0750,%{apache_user},%{apache_group}) %{nc_datadir}

%files apache
%config(noreplace) %{apache_confdir}/%{name}.conf
%config(noreplace) %{apache_confdir}/%{name}.config.inc
%config(noreplace) %{apache_confdir}/%{name}.mod_php.inc

%files apps
%defattr(-,%{apache_user},%{apache_group})
%{nc_dir}/apps/twofactor_admin
%{nc_dir}/apps/twofactor_email
%{nc_dir}/apps/twofactor_reminder

%changelog
openSUSE Build Service is sponsored by