# -*- 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$

PortSystem          1.0

PortGroup           perl5 1.0
perl5.branches      5.24

name                monarch
version             2.5.0
revision            4
license             GPL-2
categories          net
maintainers         nomaintainer
description         A GUI configuration tool for Nagios written in perl
long_description    ${description}
homepage            http://sourceforge.net/projects/monarch
platforms           darwin
master_sites        sourceforge

checksums           rmd160  e7d3454eb778aa39883035d26a905e151e2dc71f \
                    sha256  6f65d13b354a9a2faf0dbf689fac47225cac0ad635999905e55d0dd11e6bdf21

depends_lib-append  path:bin/mysql_config5:mysql5 \
                    port:nagios \
                    port:p${perl5.major}-libwww-perl \
                    port:p${perl5.major}-xml-namespacesupport \
                    port:p${perl5.major}-xml-sax \
                    port:p${perl5.major}-xml-libxml \
                    port:p${perl5.major}-nmap-scanner \
                    port:p${perl5.major}-dbi \
                    port:p${perl5.major}-dbd-mysql \
                    port:p${perl5.major}-cgi-ajax \
                    port:p${perl5.major}-class-accessor

patchfiles          patch-nmap_scan-nmap_scan.c.diff

use_configure       no
build {}

set monarchdir ${prefix}/share/groundwork/monarch

destroot {
    xinstall -m 755 -d ${destroot}${monarchdir}
    copy {*}[glob ${worksrcpath}/*] ${destroot}${monarchdir}
}

post-destroot {
    reinplace "s|/usr/bin/perl|${prefix}/bin/perl${perl5.major}|g" \
        {*}[glob ${destroot}${monarchdir}/*.pl]

    reinplace "s|/usr/local/groundwork/bin/perl|${prefix}/bin/perl${perl5.major}|g" \
        {*}[glob ${destroot}${monarchdir}/*.cgi] \
        {*}[glob ${destroot}${monarchdir}/*.p*]

    reinplace "s|/usr/local/groundwork/monarch|${monarchdir}|g" \
        {*}[glob ${destroot}${monarchdir}/*.cgi] \
        {*}[glob ${destroot}${monarchdir}/*.pl] \
        ${destroot}${monarchdir}/monarch.sql

# Fix mysqldump location
    reinplace "s|/usr/bin/mysqldump|${prefix}/bin/mysqldump5|g" \
                ${destroot}${monarchdir}/MonarchStorProc.pm

# Enable Nmap Scanner
    xinstall -m 755 -d ${destroot}${monarchdir}/bin

    reinplace "s|/usr/local/groundwork|${monarchdir}|g" \
        ${destroot}${monarchdir}/nmap_scan_one.pl

    xinstall -m 755 ${destroot}${monarchdir}/nmap_scan_one.pl \
        ${destroot}${monarchdir}/bin

    reinplace "s|__PREFIX__|${prefix}|g" \
        ${worksrcpath}/nmap_scan/nmap_scan.c

    system "cd ${worksrcpath}/nmap_scan && gcc -g -O2 -Wall -o nmap_scan_one nmap_scan.c"

# Install nmap_scan_one with mode 4750 and chown it to root (below) to allow normal user to run nmap with root privs
    xinstall -m 4750 ${worksrcpath}/nmap_scan/nmap_scan_one \
        ${destroot}${monarchdir}/bin

# Delete precompiled Linux version of nmap_scan_one and leftover nmap_scan_one.pl
    delete ${destroot}${monarchdir}/nmap_scan_one
    delete ${destroot}${monarchdir}/nmap_scan_one.pl

# End enable Nmap-Scanner

# Set permissions & modes
    system "chown -R nagios:nagios ${destroot}${monarchdir}/*"
    system "chmod +x ${destroot}${monarchdir}/*.cgi"
    system "chown root ${destroot}${monarchdir}/bin/nmap_scan_one"
}


notes "

**** To complete the Monarch installation ****
        
Read the README at ${prefix}/share/groundwork/monarch/ for full information.

1) Configure Nagios with the default sample files or begin with a Nagios configuration you created manually.


2) Setup MySQL and create the Monarch user
   Configure MySQL (new MySQL installs)
        sudo -u mysql ${prefix}/lib/mysql5/bin/mysql_install_db
 
   Start MySQL.
        sudo ${prefix}/share/mysql5/mysql/mysql.server start

   Set MySQL to start at system boot (optional)
        sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

   Set a root MySQL password.
        Follow the instructions that were given after you executed 'mysql_install_db' above.

   Create the Monarch MySQL user and password and set the account's permissions
    mysql5 -u root -p (login with new root password when prompted)
        mysql> grant CREATE,INSERT,SELECT,DELETE,UPDATE on monarch.* to monarch@localhost;
        mysql> grant CREATE,INSERT,SELECT,DELETE,UPDATE on monarch.* to monarch;
    mysql> SET PASSWORD FOR monarch@localhost = PASSWORD('my-password');
        mysql> exit


3) Create the Monarch MySQL database and initialize it using monarch_setup.pl.
   Login to MySQL and the create Monarch database
    mysql5 -u root -p (login with new root password when prompted)
        mysql> create database monarch;

   Run monarch_setup.pl
    cd ${monarchdir}
    sudo ./monarch_setup.pl

   Responses for the monarch_setup.pl program
    What is the host name of your MySQL database server? - localhost
    What is the name of the user that will have access to the database? - monarch
    What is the password for user monarch to access the database monarch - <my-password>
    What is the name of the database you will use for your Monarch installation? - monarch
    Enter web server's user account - nagios
    Enter web server's user group - nagios
    Please enter your web server's relative path to cgi-bin - /cgi-bin
    What is your web server's document root - /Library/WebServer/Documents
    Enter the full path of your cgi-bin directory - /Library/WebServer/CGI-Executables/
    Enter the full installation path for Monarch - ${monarchdir}
    Enter the full path to your nagios.cfg file - ${prefix}/etc/nagios
    Enter the full path to your nagios binary file - ${prefix}/bin
    
   Verify Monarch DB:
        mysql5 -u root -p
        mysql> use monarch;
        mysql> show tables;
        mysql> exit

NOTE: If you wish to delete the monarch database to start over: a) log into MySQL as
      root; b) execute 'drop database monarch'; c) repeat Step 2.  There is no need
      to repeat step 1 because the Monarch user/pass and permissions are not deleted
      by dropping (deleting) the database.


4) Set your Apache user and group to the Nagios user
        sudo pico /etc/httpd/httpd.conf
        
    user nagios
    group nagios


5) Initial Monarch setup at http://localhost/cgi-bin/monarch.cgi

        Login with the default user/pass \"super_user\" \"password\"

Control menu
   Setup -> Nagios Version -> 2.x
   Nagios Main Configuration -> Load from nagios.cfg (to import existing Nagios configuration)
   Nagios CGI Configuration -> Load from cgi.cfg (to import existing CGI configuration)
   Load (to populate the Monarch database with sample Nagios object files or manually created ones)

Now setup your hosts, services, etc using the Monarch GUI.


6) Optionally use the simplified Monarch interface

   After the initial Monarch setup you may setup your hosts, services, etc with the optional
   Monarch EZ GUI if you intend to use default settings for the advanced options and/or use
   nmap host discovery to add hosts to Nagios, use the simplified Monarch interface.

   To do this, first copy monarch_ez.cgi and monarch_scan.cgi to your cgi-bin/ directory
        cd ${monarchdir}
        sudo cp -p monarch_ez.cgi /Library/WebServer/CGI-Executables/
        sudo cp -p monarch_scan.cgi /Library/WebServer/CGI-Executables/

   Now go to http://localhost/cgi-bin/monarch_ez.cgi

   You may revert to using the full Monarch interface at any time.

NOTE: No login is required for the monarch_ez.cgi interface, so you will need to use Apache
      username(s)/password(s) just like you use for the Nagios monitor interface to maintain
      secure access.
"
livecheck.regex     "${name}-(\\d+(?:\\.\\d+)*)\\.tar"