# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem              1.0
PortGroup               active_variants 1.1

name                    couchdb
version                 3.5.1
revision                1

categories              databases
license                 Apache-2
maintainers             {ciserlohn @ci42} {makr @mohd-akram} openmaintainer

description             CouchDB is a document database server

long_description        ${description} with a restful HTTP API, capable of \
                        storing arbitrary JSON documents, building powerful \
                        data views, defined in JavaScript, and processed in \
                        parallel using Map/Reduce. \
                        It leaves out SQL and transactions for simplicity's \
                        sake, to improve scalability and to relax users' lives.

homepage                https://couchdb.apache.org/

master_sites            apache
master_sites.mirror_subdir ${name}/source/${version}/
distname                apache-${name}-${version}

checksums               rmd160  a48f35f0b5933d8e22bf5c2986a007760d3f0ec5 \
                        sha256  c22cf31d6d91a3f5aa04f0cad493babdc723213494cb5e6170a507d359c50136 \
                        size    25006439

depends_build           port:erlang

depends_lib             path:lib/pkgconfig/icu-uc.pc:icu \
                        port:ncurses \
                        path:lib/libssl.dylib:openssl \
                        port:zlib

# erlang is not universal
universal_variant       no

require_active_variants erlang ssl

set dbgroup             ${name}
set dbuser              ${name}
set logdir              ${prefix}/var/log/${name}
set dbdir               ${prefix}/var/db/${name}
set confdir             ${prefix}/etc/${name}
set datadir             ${prefix}/share/${name}
set docdir              ${prefix}/share/doc/${name}
set rundir              ${prefix}/var/run/${name}

startupitem.create      yes
startupitem.uniquename  org.apache.couchdb
startupitem.user        ${dbuser}
startupitem.group       ${dbgroup}
startupitem.executable  ${prefix}/bin/couchdb

add_users               ${dbuser} group=${dbgroup} home=${rundir} \
                        realname=CouchDB\ Database\ Server

configure.pre_args
configure.args          --disable-spidermonkey

set root                ${worksrcpath}/rel/couchdb
set dest                ${prefix}/libexec/${name}

destroot.target         release

destroot.keepdirs       ${destroot}${dbdir} \
                        ${destroot}${logdir} \
                        ${destroot}${rundir}

post-destroot {
    xinstall -d ${destroot}${dest}

    copy ${root}/bin ${root}/lib ${root}/releases ${root}/erts-16.1.1 \
        ${destroot}${dest}
    delete ${destroot}${dest}/bin/couchdb.cmd
    ln -s ${dest}/bin/couchdb ${dest}/bin/remsh ${dest}/bin/weatherreport \
        ${destroot}${prefix}/bin

    xinstall -d ${destroot}${confdir}
    copy {*}[glob ${root}/etc/*] ${destroot}${confdir}

    xinstall -d ${destroot}${docdir}
    copy ${root}/LICENSE ${destroot}${docdir}
    move ${destroot}${confdir}/local.ini ${destroot}${docdir}

    xinstall -d ${destroot}${datadir}
    copy ${root}/share/www ${destroot}${datadir}

    copy ${root}/share/docs/couchdb.1 ${destroot}${prefix}/share/man/man1

    # TODO - patch the source to use these paths directly
    ln -s ${confdir} ${destroot}${dest}/etc
    ln -s ${datadir} ${destroot}${dest}/share
    ln -s ${dbdir} ${destroot}${dest}/data

    xinstall -m 755 -o ${dbuser} -g ${dbgroup} -d \
        ${destroot}${dbdir} \
        ${destroot}${logdir} \
        ${destroot}${rundir}
}

post-activate {
    if {![file exists ${confdir}/local.ini]} {
        copy ${docdir}/local.ini ${confdir}/local.ini
    }
}

livecheck.url           https://couchdb.apache.org/downloads.html
livecheck.regex         apache-${name}-(\[0-9.\]+\[0-9\]+)
