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

PortSystem          1.0
PortGroup           github 1.0

github.setup        rabbitmq rabbitmq-server 3.11.15 v
revision            0
checksums           rmd160  c30a5ee11a501b8a271bda39f92dd8125230f4c6 \
                    sha256  538be3c85e8cad10de62705714f71eaacb04339f73a81477c91478600bb8253c \
                    size    20897748

categories          net
platforms           {darwin any}
maintainers         nomaintainer
license             MPL-1.1 MIT Apache-2
supported_archs     noarch

description         The RabbitMQ AMQP Server
long_description    \
    RabbitMQ is an implementation of AMQP, the emerging standard for \
    high performance enterprise messaging. The RabbitMQ server is a \
    robust and scalable implementation of an AMQP broker.

homepage            https://www.rabbitmq.com
github.tarball_from releases
use_xz              yes
distfiles           ${name}-generic-unix-${version}${extract.suffix}
worksrcdir          [string map {- _} $name]-$version

depends_lib         port:erlang
depends_build       port:libxslt

if {${os.platform} eq "darwin" && ${os.major} <= 9} {
    depends_build-append port:py27-simplejson
    build.args      PYTHON=${prefix}/bin/python2.7
} else {
    # python3.X doesn't work
    build.args      PYTHON=python2.7
}

set serveruser      rabbitmq
set servergroup     rabbitmq
set serverhome      ${prefix}/var/lib/rabbitmq
set logdir          ${prefix}/var/log/rabbitmq
set confdir         ${prefix}/etc/rabbitmq
set mnesiadbdir     ${prefix}/var/lib/rabbitmq/mnesia
set plistloc        ${prefix}/etc/LaunchDaemons/org.macports.${name}
set rootdest        ${prefix}/lib/rabbitmq
set rootserver      ${rootdest}/lib/${worksrcdir}
set sbindir         ${destroot}${rootdest}/bin
set wrappersbin     ${destroot}${prefix}/sbin
set realsbin        ${destroot}${rootserver}/sbin
set mansrc          ${workpath}/${worksrcdir}/share/man
set mandest         ${destroot}${prefix}/share/man

add_users ${serveruser} group=${servergroup} realname=RabbitMQ\ Server home=${serverhome}

use_configure       no

build {}

destroot.keepdirs \
    ${destroot}${confdir} \
    ${destroot}${logdir} \
    ${destroot}${mnesiadbdir}

destroot {
    # This is a hack: We need to patch the startupitem, so generate it manually
    # here, then define the function to empty to avoid overwriting it later
    package require portstartupitem 1.0
    portstartupitem::startupitem_create
    proc portstartupitem::startupitem_create {args} {}
    system -W "${destroot}${plistloc}" "/usr/libexec/PlistBuddy -c \"Add :UserName string ${serveruser}\" org.macports.${name}.plist"

    xinstall -d -m 0775 ${destroot}${confdir}
    xinstall -d -g [existsgroup ${servergroup}] -m 0775 ${destroot}${logdir}
    xinstall -d -g [existsgroup ${servergroup}] -m 0775 ${destroot}${serverhome}
    xinstall -d -g [existsgroup ${servergroup}] -m 0775 ${destroot}${mnesiadbdir}
    xinstall -d -g [existsgroup ${servergroup}] -m 0775 ${destroot}${rootserver}
    xinstall -d -g [existsgroup ${servergroup}] -m 0775 ${destroot}
    xinstall -d -g [existsgroup ${servergroup}] -m 0775 ${sbindir}

    copy {*}[glob ${worksrcpath}/*] ${destroot}${rootserver}/

    reinplace -E "s:^SYS_PREFIX=\\\${RABBITMQ_HOME}$:SYS_PREFIX=${prefix}:" \
        ${realsbin}/rabbitmq-defaults

    xinstall -m 0755 ${filespath}/rabbitmq-script-wrapper \
                ${wrappersbin}/rabbitmq-server
    reinplace -E "s:@MACPORTS_PREFIX@:${prefix}:g" \
                ${wrappersbin}/rabbitmq-server
    reinplace -E "s:/usr/lib/rabbitmq/bin/:${prefix}/lib/rabbitmq/bin/:g" \
                ${wrappersbin}/rabbitmq-server
    reinplace -E "s:/var/lib/rabbitmq:${prefix}/var/lib/rabbitmq:g" \
                ${wrappersbin}/rabbitmq-server

    copy ${wrappersbin}/rabbitmq-server ${wrappersbin}/rabbitmqctl
    copy ${wrappersbin}/rabbitmq-server ${wrappersbin}/rabbitmq-plugins
    file attributes ${destroot}${serverhome} -owner ${serveruser} -group ${servergroup}
    file attributes ${destroot}${logdir} -owner ${serveruser} -group ${servergroup}

    foreach script {rabbitmq-defaults rabbitmq-env rabbitmq-plugins rabbitmq-server rabbitmqctl cuttlefish} {
        ln -s ../lib/${worksrcdir}/sbin/${script} ${sbindir}/${script}
    }

    xinstall -m 0644 -W ${mansrc}/man8 rabbitmq-plugins.8.gz \
        rabbitmq-server.8.gz rabbitmqctl.8.gz ${mandest}/man8/
    xinstall -m 0644 -W ${mansrc}/man5 rabbitmq-env.conf.5.gz ${mandest}/man5/
}

startupitem.create  yes
startupitem.init    "PATH=${prefix}/bin:${prefix}/sbin:\$PATH; export PATH"
startupitem.start   "rabbitmq-server 2>&1"
startupitem.stop    "rabbitmqctl stop 2>&1"
startupitem.logfile ${prefix}/var/log/rabbitmq/startupitem.log

github.livecheck.regex  {([0-9.]+)}
