# -*- 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 92837 2012-05-09 00:51:48Z ryandesign@macports.org $

PortSystem              1.0

name                    xymon-server
set shortname           xymon
conflicts               xymon
epoch                   1
version                 4.3.7
revision                1
categories              net
platforms               darwin
license                 GPL-2
maintainers             free.fr:fclaire

description             Xymon network monitor Server

long_description        Xymon is a system for monitoring of hosts and networks, \
                        inspired by the Big Brother system. It provides real-time \
                        monitoring, an easy web-interface, historical data, \
                        availability reports and performance graphs. \
                        Xymon was previously known as \"Hobbit\".

homepage                http://www.xymon.com/
master_sites            sourceforge:project/xymon/Xymon/${version}
distname                ${shortname}-${version}
dist_subdir             ${shortname}

checksums               sha1    29339157a1a6155d134ed1ad9985391d43034ee8 \
                        rmd160  9547eac47a5893f978cace54b5761e9cabe75941

depends_lib             port:rrdtool \
                        port:fping \
                        port:pcre \
                        port:libpng \
                        port:openssl \
                        port:openldap

universal_variant       no

patchfiles              patch-build-Makefile.rules.diff \
                        patch-lib-Makefile.diff \
                        patch-client-xymonclient-darwin.sh.diff

if {"darwin" == ${os.platform} && ${os.major} > 8} {
    set xymonuser       _xymon
} else {
    set xymonuser       xymon
}
set xymongroup          ${xymonuser}
set xytopdir            ${prefix}/lib/${shortname}
set xyetcdir            ${xytopdir}/etc
set xybindir            ${xytopdir}/server/bin
set xylogdir            ${prefix}/var/log/${shortname}
set xytmpdir            ${xytopdir}/tmp
set xyvardir            ${xytopdir}/data
set xywebdir            ${xytopdir}/web
set xywwwdir            ${xytopdir}/www

configure.cmd           ./configure.server
configure.args-append   --rrdinclude ${prefix}/include \
                        --rrdlib ${prefix}/lib \
                        --pcreinclude ${prefix}/include \
                        --pcrelib ${prefix}/lib \
                        --sslinclude ${prefix}/include \
                        --ssllib ${prefix}/lib \
                        --ldapinclude ${prefix}/include \
                        --ldaplib ${prefix}/lib \
                        --fping ${prefix}/sbin/fping
configure.env-append    ENABLESSL=y \
                        ENABLELDAP=y \
                        ENABLELDAPSSL=y \
                        XYMONUSER=${xymonuser} \
                        USEXYMONPING=n \
                        XYMONTOPDIR=${xytopdir} \
                        XYMONHOSTURL=/${shortname} \
                        CGIDIR=${xytopdir}/cgi-bin \
                        XYMONCGIURL=/${shortname}-cgi \
                        SECURECGIDIR=${xytopdir}/cgi-secure \
                        SECUREXYMONCGIURL=/${shortname}-secgci \
                        HTTPDGID=_www \
                        XYMONLOGDIR=${xylogdir} \
                        XYMONHOSTNAME=localhost \
                        XYMONHOSTIP=127.0.0.1 \
                        XYMONHOSTOS=${os.platform} \
                        MANROOT=${prefix}/share/man \
                        INSTALLBINDIR=${xytopdir}/server/bin \
                        INSTALLETCDIR=${xyetcdir} \
                        INSTALLEXTDIR=${xytopdir}/server/ext \
                        INSTALLTMPDIR=${xytmpdir} \
                        INSTALLWEBDIR=${xywebdir} \
                        INSTALLWWWDIR=${xywwwdir} \
                        XYMONVAR=${xyvardir}

destroot.env-append     INSTALLROOT=${destroot}
destroot.keepdirs       ${destroot}${xytopdir} \
                        ${destroot}${xylogdir} \
                        ${destroot}${xytmpdir} \
                        ${destroot}${xyvardir}/acks \
                        ${destroot}${xyvardir}/data \
                        ${destroot}${xyvardir}/disabled \
                        ${destroot}${xyvardir}/hist \
                        ${destroot}${xyvardir}/histlogs \
                        ${destroot}${xyvardir}/hostdata \
                        ${destroot}${xyvardir}/logs \
                        ${destroot}${xyvardir}/rrd \
                        ${destroot}${xyvardir} \
                        ${destroot}${xywebdir} \
                        ${destroot}${xywwwdir}/html \
                        ${destroot}${xywwwdir}/rep \
                        ${destroot}${xywwwdir}/snap \
                        ${destroot}${xywwwdir}/notes \
                        ${destroot}${xywwwdir}/wml

add_users ${xymonuser} group=${xymongroup} realname=Xymon\ User home=${xytopdir} shell=/bin/bash

post-destroot {
    # Adding own startupitem - xymon needs to be run under ${xymonuser} user
    xinstall -d ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}/
    xinstall -m 644 ${filespath}/org.macports.${name}.plist ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}/
    # The startup script needs to be adapted to use ${xymonuser} user
    reinplace s%@XYMONUSER@%${xymonuser}%g ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}/org.macports.${name}.plist
    reinplace s%@XYMONLAUNCH@%${xybindir}/xymonlaunch%g ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}/org.macports.${name}.plist
    reinplace s%@XYMONTASKS@%${xyetcdir}/tasks.cfg%g ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}/org.macports.${name}.plist
    reinplace s%@XYMONSERVER@%${xyetcdir}/xymonserver.cfg%g ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}/org.macports.${name}.plist
    reinplace s%@XYMONLAUNCHLOG@%${xylogdir}/xymonlaunch.log%g ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}/org.macports.${name}.plist
    reinplace s%@XYMONLAUNCHPID@%${xylogdir}/xymonlaunch.pid%g ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}/org.macports.${name}.plist

    # Creation of a symbolic link towards startupitem
    xinstall -d -m 755 ${destroot}/Library/LaunchDaemons
    ln -sf ${prefix}/etc/LaunchDaemons/org.macports.${name}/org.macports.${name}.plist ${destroot}/Library/LaunchDaemons/
    # And a link to have the xymon client working
    ln -sf ${xytmpdir} ${destroot}${xytopdir}/client/

    # Adding a suffix to config files
    foreach f { alerts.cfg analysis.cfg cgioptions.cfg client-local.cfg columndoc.csv combo.cfg critical.cfg graphs.cfg holidays.cfg \
                hosts.cfg protocols.cfg rrddefinitions.cfg snmpmibs.cfg tasks.cfg xymon-apache.conf xymonmenu.cfg xymonserver.cfg } {
        move ${destroot}${xyetcdir}/${f} ${destroot}${xyetcdir}/${f}.${version}
    }
}

post-activate {
    # If not already present we put default config files
    foreach f { alerts.cfg analysis.cfg cgioptions.cfg client-local.cfg columndoc.csv combo.cfg critical.cfg graphs.cfg holidays.cfg \
                hosts.cfg protocols.cfg rrddefinitions.cfg snmpmibs.cfg tasks.cfg xymon-apache.conf xymonmenu.cfg xymonserver.cfg } {
        if {![file exists ${xyetcdir}/${f}]} {
            file copy ${xyetcdir}/${f}.${version} ${xyetcdir}/${f}
            system "chown ${xymonuser} ${xyetcdir}/${f}"
        }
    }
}

notes "  *** To complete the Xymon install ***

Run the following commands in your terminal:
    $ sudo echo \"kern.sysv.shmmni=64\" >> /etc/sysctl.conf
    $ sudo echo \"kern.sysv.shmseg=12\" >> /etc/sysctl.conf
    $ sudo mv ${xyetcdir}/xymon-apache.conf /etc/apache2/\[other|sites\]/
    $ sudo /usr/sbin/apachectl restart
    $ sudo dscl . -append /Groups/admin GroupMembership ${xymonuser}
Reboot your system
Start xymon server:
    $ sudo launchctl load -w /Library/LaunchDaemons/org.macports.xymon-server.plist
Open \"http://localhost/xymon/\" in your server's browser

Full install instructions here: http://trac.macports.org/wiki/howto/SetupXymonServer

"

use_parallel_build  no

livecheck.type      regex
livecheck.url       http://sourceforge.net/projects/${shortname}/files/
livecheck.regex     ${shortname}-(\[0-9.\]+)\\.tar.gz

variant snmp description {add integrated SNMP support} {
    configure.env-append    SNMP=1
}
