##
##  couchdb.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         couchdb
Summary:      Schema-Less Web Database
URL:          http://couchdb.apache.org/
Vendor:       Apache Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Apache
Version:      1.6.1
Release:      20140904

#   list of sources
Source0:      http://www.apache.org/dist/couchdb/source/%{version}/apache-couchdb-%{version}.tar.gz
Source1:      couchdb.ini
Source2:      rc.couchdb
Patch0:       couchdb.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  erlang, js, icu, curl
PreReq:       erlang, js, icu, curl

%description
    Apache CouchDB is a document-oriented database that can be queried
    and indexed in a MapReduce fashion using JavaScript. CouchDB
    also offers incremental replication with bi-directional conflict
    detection and resolution. CouchDB provides a RESTful JSON API than
    can be accessed from any environment that allows HTTP requests.
    There are myriad third-party client libraries that make this even
    easier from your programming language of choice. CouchDBs built in
    Web administration console speaks directly to the database using
    HTTP requests issued from your browser.

%track
    prog couchdb = {
        version   = %{version}
        url       = http://couchdb.apache.org/
        regex     = apache-couchdb-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n apache-couchdb-%{version}
    %patch -p0

%build
    #   configure program
    CC="%{l_cc} -fPIC" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags js icu .} -I%{l_prefix}/lib/erlang/usr/include" \
    LDFLAGS="%{l_ldflags erlang/usr/lib}" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --localstatedir=%{l_prefix}/var/couchdb \
        --with-erlang=%{l_prefix} \
        --with-js-include=%{l_prefix}/include/js \
        --with-js-lib=%{l_prefix}/lib \
        --with-openssl-bin-dir=%{l_prefix}/bin \
        --enable-js-trunk \
        --disable-init \
        --disable-launchd

    #   build program
    %{l_make} %{l_mflags}

%install
    #   install program
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

    #   strip down installation
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/etc/logrotate.d
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   install default confguration
    %{l_shtool} subst \
        -e 's;^\(database_dir =\).*;\1 %{l_prefix}/var/couchdb/db;' \
        -e 's;^\(view_index_dir =\).*;\1 %{l_prefix}/var/couchdb/db;' \
        -e 's;^\(uri_file =\).*;\1 %{l_prefix}/var/couchdb/db/couch.uri;' \
        -e 's;^\(file =\).*;\1 %{l_prefix}/var/couchdb/log/couchdb.log;' \
        $RPM_BUILD_ROOT%{l_prefix}/etc/couchdb/default.ini
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE couchdb.ini} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/couchdb/local.ini

    #   create runtime directories
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/var/couchdb/* >/dev/null 2>&1 || true
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/couchdb/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/couchdb/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/couchdb/run

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.couchdb} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/couchdb/*.ini' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/couchdb/*'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   initial jo upgrade, restart service
        ( echo "Access CouchDB via URL http://127.0.0.1:5984/ and"
          echo "interactively via its Futon Web User Interface unter"
          echo "the URL http://127.0.0.1:5984/_utils/"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} couchdb status 2>/dev/null`
        [ ".$couchdb_active" = .yes ] && %{l_rc} couchdb restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        %{l_rc} couchdb stop 2>/dev/null
        rm -f  $RPM_INSTALL_PREFIX/var/couchdb/log/* >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/couchdb/run/* >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/couchdb/db/*  >/dev/null 2>&1 || true
    fi
    exit 0

