# Licensed 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.
#

%define name couchdb
%define prefix /opt/%{name}

Summary:       RESTful document oriented database
License:       Apache License v2.0
Name:          %{name}
Version:       %{_version}
Release:       1%{?dist}
Source:        https://www.apache.org/dist/couchdb/source/${version}/apache-couchdb-%{version}.tar.gz
Source1:       %{name}.service
Source2:       %{name}-nouveau.service
Source3:       %{name}-nouveau
Prefix:        %{prefix}
Group:         Applications/Databases
URL:           https://couchdb.apache.org/
Vendor:        The Apache Software Foundation
BuildArch:     x86_64 ppc64le aarch64 s390x
ExclusiveArch: x86_64 ppc64le aarch64 s390x
Exclusiveos:   linux
Packager:      CouchDB Developers <dev@couchdb.apache.org>
Patch1:        0001-build-with-sm68-on-aarch64.patch

BuildRequires: esl-erlang
BuildRequires: gcc
Requires(pre): shadow-utils

BuildRequires: git
BuildRequires: help2man
BuildRequires: libcurl-devel
BuildRequires: libicu-devel
BuildRequires: nodejs >= 6.10.1
BuildRequires: java-21-openjdk-devel

Requires(post): curl
Requires(post): %SPIDERMONKEY%
Requires(post): libicu >= 4.2.1
Requires(post): procps

Suggests: java-21-openjdk-headless

BuildRequires:		xfsprogs-devel
%{?systemd_requires}
BuildRequires:		systemd

%description
Apache CouchDB is a distributed, fault-tolerant and schema-free
document-oriented database accessible via a RESTful HTTP/JSON API. Among other
features, it provides robust, incremental replication with bi-directional
conflict detection and resolution, and is queryable and indexable using a
table-oriented view engine with JavaScript acting as the default view
definition language.

CouchDB is written in Erlang, but can be easily accessed from any environment
that provides means to make HTTP requests. There are a multitude of third-party
client libraries that make this even easier for a variety of programming
languages and environments.

# NOTE: Stripping binaries causes issues so we skip it.
%define __os_install_post %{nil}

%prep

%build
if [ -d ./nouveau ]; then
    ./configure --spidermonkey-version=%SM_VER% --with-nouveau
else
    ./configure --spidermonkey-version=%SM_VER%
fi
%{__make} release

%clean
%{__rm} -rf %{buildroot}

%pre

if ! /usr/bin/getent passwd couchdb > /dev/null; then /usr/sbin/adduser \
  --system --home /opt/couchdb --no-create-home \
  --shell /bin/bash --comment "CouchDB Administrator" \
  --user-group couchdb; fi

%install
%{__install} -d -m0755 %{buildroot}/opt
%{__cp} -r rel/couchdb %{buildroot}/opt
%{__install} -d -m0750 %{buildroot}%{_sharedstatedir}/%{name}
%{__install} -d -m0750 %{buildroot}%{_sharedstatedir}/%{name}/nouveau
/bin/find %{buildroot}/opt/%{name} -name *.ini -exec %{__chmod} 0640 {} \;
%{__install} -Dp -m0644 %{SOURCE1} %{buildroot}%{_unitdir}/%{name}.service
%{__install} -Dp -m0644 %{SOURCE2} %{buildroot}%{_unitdir}/%{name}-nouveau.service
%{__install} -Dp -m0644 %{SOURCE3} %{buildroot}/etc/sysconfig/%{name}-nouveau
%{__ln_s} -f -T %{_sharedstatedir}/%{name} %{buildroot}/opt/%{name}/data

%post
if %{__grep} -q "^-setcookie monster$" /opt/%{name}/etc/vm.args; then
  # -v is a bash 4.2+ feature
  if [[ -v COUCHDB_COOKIE ]]; then
    echo "Using defined COUCHDB_COOKIE value."
    cookie=${COUCHDB_COOKIE}
  else
    echo "Generating random cookie value."
    cookie=$(dd if=/dev/random bs=1 count=38 status=none | base64 | tr -cd [:alnum:])
  fi
  escaped_cookie=$(printf '%s' "$cookie" | sed 's:\\:\\\\:g')
  %{__sed} -i "/^-setcookie monster/c\-setcookie '${escaped_cookie}'" /opt/%{name}/etc/vm.args
elif %{__grep} -q "^[# ]*-setcookie$" /opt/%{name}/etc/vm.args; then
  # -v is a bash 4.2+ feature
  if [[ -v COUCHDB_COOKIE ]]; then
    echo "Using defined COUCHDB_COOKIE value."
    cookie=${COUCHDB_COOKIE}
  else
    echo "Generating random cookie value."
    cookie=$(dd if=/dev/random bs=1 count=38 status=none | base64 | tr -cd [:alnum:])
  fi
  escaped_cookie=$(printf '%s' "$cookie" | sed 's:\\:\\\\:g')
  %{__sed} -i "/^[# ]*-setcookie/c\-setcookie '${escaped_cookie}'" /opt/%{name}/etc/vm.args
fi

if [[ -v COUCHDB_NOUVEAU_ENABLE ]]; then
    echo "Enabling nouveau in /opt/couchdb/etc/default.d/10-nouveau.ini"
    echo " * Make sure to install java version 11+ (dnf install java-21-openjdk-headless)"
    echo " * Enable couchdb-nouveau systemd unit"
    cat <<EOF > /opt/couchdb/etc/default.d/10-nouveau.ini
[nouveau]
enable = true
EOF
fi

%{__chown} -R couchdb:couchdb /opt/%{name}
%{__chmod} a+x /opt/%{name}/bin/*
%systemd_post %{name}.service
%systemd_post %{name}-nouveau.service

%preun
%systemd_preun %{name}.service
%systemd_preun %{name}-nouveau.service

%postun
%systemd_postun_with_restart %{name}.service
%systemd_postun_with_restart %{name}-nouveau.service

if [ $1 -eq 0 ] ; then
    # uninstall, remove auto-generated nouveau enable file
    %{__rm} /opt/couchdb/etc/default.d/10-nouveau.ini
fi

%files
%attr(0755, %{name}, %{name}) /opt/couchdb
%attr(0755, %{name}, %{name}) %dir %{_sharedstatedir}/%{name}
%attr(0755, %{name}, %{name}) %dir %{_sharedstatedir}/%{name}/nouveau
%config(noreplace) /opt/couchdb/etc/local.ini
%config(noreplace) /opt/couchdb/etc/vm.args
%config(noreplace) /opt/couchdb/etc/nouveau.yaml
%config(noreplace) /etc/sysconfig/couchdb-nouveau
%{_unitdir}/%{name}.service
%{_unitdir}/%{name}-nouveau.service

%changelog
* Thu Aug 22 2024 CouchDB Developers <dev@couchdb.apache.org> 3.4
- Add nouveau service with a suggested dependency for java 21
- Remove file-based logger, use journalctl for both couchdb and couchdb-nouveau
* Tue May 2 2017 CouchDB Developers <dev@couchdb.apache.org> 2.0.0-1
- New upstream version
- New sysvinit and systemd service files
- New backported couchup script
