# -*- 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
# $Id: Portfile 114257 2013-12-03 21:30:05Z ryandesign@macports.org $

PortSystem          1.0
PortGroup           compiler_blacklist_versions 1.0

name                rethinkdb
version             1.11.1
categories          databases
platforms           darwin
maintainers         nomaintainer
license             AGPL-3

description         open-source distributed database

long_description    ${name} is an ${description} that has an intuitive query \
                    language, automatically parallelized queries, and simple \
                    administration.

homepage            http://www.rethinkdb.com/
master_sites        http://download.rethinkdb.com/dist/
extract.suffix      .tgz

checksums           rmd160  e615a2031d0b3597e464d585becf6e25a9b51bdb \
                    sha256  48941171acd91ba41b97cef38f75fd0d8b8805eca767941f07e4e5c06df4b7a3

depends_build       port:coffee-script

depends_lib         port:boost \
                    port:v8 \
                    path:bin/node:nodejs \
                    port:protobuf-cpp

# nodejs only supports Intel processors and is not universal
supported_archs     i386 x86_64
universal_variant   no

# Error: GCC 4.2.2 is too old. At least GCC 4.4.3 is required
compiler.blacklist-append apple-gcc* gcc-3.3 gcc-4.* *llvm-gcc-4.2 macports-gcc-4.2 macports-gcc-4.3

# Xcode 3.2.x included clang but not clang++, so it used llvm-g++-4.2 instead.
compiler.blacklist-append {clang < 137}

platform darwin {
    if {${os.major} < 13} {
        # When protobuf-cpp and v8 are compiled against libstdc++, don't use
        # clang, which only supports C++11 (required by rethinkdb) when using
        # libc++.
        compiler.blacklist-append   *clang*
        # without this, no compilers are available at all
        compiler.fallback-append    macports-gcc-4.8
    }
}

# v8 >= 3.21 compatibility from upstream
patch.pre_args      -p1
patchfiles          3cf9e7bf13e5f0f364aecf0913e09d704e9688d2.patch

set webresdir       ${prefix}/share/${name}/web
build.args          DEBUG=0 ALLOW_WARNINGS=1 BUILD_DIR=build WEBRESDIR=${webresdir}
build.target        build/rethinkdb web-assets

set dbdir             ${prefix}/var/db/${name}/
set logdir            ${prefix}/var/log/${name}/
set default_instance  default
set config_file       ${prefix}/etc/rethinkdb/${default_instance}.conf

set rethinkuser     _${name}
add_users           ${rethinkuser} group=${rethinkuser} realname=RethinkDB\ Server

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

post-patch {
    reinplace "s|# directory=/var/lib/rethinkdb/default|directory=${dbdir}${default_instance}|" \
        ${worksrcpath}/packaging/assets/config/default.conf.sample
}

destroot {
    # TODO: explore using "make install DESTDIR=xxx" instead of copying all files to destroot manually?
    xinstall ${worksrcpath}/build/rethinkdb ${destroot}${prefix}/bin/
    xinstall -d ${destroot}${prefix}/etc/rethinkdb
    xinstall -m 644 ${worksrcpath}/packaging/assets/config/default.conf.sample ${destroot}${config_file}.sample
    xinstall -d ${destroot}${prefix}/etc/bash_completion.d/
    xinstall -m 644 ${worksrcpath}/packaging/assets/scripts/rethinkdb.bash ${destroot}${prefix}/etc/bash_completion.d/
    xinstall -m 644 ${worksrcpath}/packaging/assets/man/rethinkdb.1 ${destroot}${prefix}/share/man/man1/
    xinstall -d ${destroot}${prefix}/share/${name}/
    move ${worksrcpath}/build/rethinkdb_web_assets/ ${destroot}${webresdir}

    xinstall -m 755 -o ${rethinkuser} -g ${rethinkuser} -d \
        ${destroot}${dbdir} \
        ${destroot}${logdir}
}

# TODO: create db and default config file in case if they are absent 
notes "You can start rethinkdb as service. For that you need:
    - create default database: 'sudo -u ${rethinkuser} rethinkdb create -d ${dbdir}${default_instance}'
    - create and edit config file for the default database: 'cp ${config_file}.sample ${config_file}'
    - load service:  'sudo port load ${startupitem.name}'"

startupitem.create      yes
startupitem.executable  sudo -u ${rethinkuser} ${prefix}/bin/rethinkdb serve --config-file ${config_file}

livecheck.type      regex
livecheck.url       ${master_sites}
livecheck.regex     ${name}-(\[0-9.\]+)${extract.suffix}
