--- _orig/net/cacti/Portfile	2013-09-19 18:01:45.000000000 -0400
+++ net/cacti/Portfile	2014-04-20 14:57:27.000000000 -0400
@@ -1,40 +1,75 @@
 # $Id: Portfile 111384 2013-09-19 21:52:20Z markd@macports.org $
 
-PortSystem	1.0
+PortSystem          1.0
 
-name			cacti
-version			0.8.8b
-categories		net
-license			GPL-2+
-maintainers		markd openmaintainer
-platforms		darwin
-supported_archs	noarch
+name                cacti
+version             0.8.8b
+revision            1
+categories          net
+license             GPL-2+
+maintainers         markd openmaintainer
+platforms           darwin
+supported_archs     noarch
 
-description		Cacti is a complete RRDtool network graphing solution.
+description         Cacti is a complete RRDtool network graphing solution.
 
-long_description	Cacti is a complete network graphing solution designed \
+long_description    Cacti is a complete network graphing solution designed \
                         to harness the power of RRDtool's data storage and \
                         graphing functions.
 
-homepage		http://www.cacti.net
-master_sites		http://www.cacti.net/downloads
+homepage            http://www.cacti.net
+master_sites        http://www.cacti.net/downloads
 
-checksums		md5 acb40deae073ca22e5c01a8e3ba389fb \
-			rmd160 a2c88961565c6b5d593b4f2603514139800c9145
+checksums           md5 acb40deae073ca22e5c01a8e3ba389fb \
+                    rmd160 a2c88961565c6b5d593b4f2603514139800c9145
 
-depends_lib		path:bin/mysql_config5:mysql5 \
-			port:rrdtool
+depends_lib         port:rrdtool
+
+## http://bugs.cacti.net/view.php?id=2261
+patchfiles          patch-cacti-r7304.diff patch-cacti-r7331.diff
 
 use_configure   no
 build {}
 
-set cactiversion 0.8.8a
 set cactidir ${prefix}/share/cacti
 
+post-patch {
+    reinplace "s|/usr/local/bin/rrdtool|${prefix}/bin/rrdtool|g;s|/usr/bin/php|${prefix}/bin/php55|;s|/usr/bin/snmp|${prefix}/bin/snmp|g" ${worksrcpath}/install/index.php
+}
+post-configure {
+    addgroup cactiuser
+    add_users cactiuser gid=[existsgroup cactiuser] home=${cactidir} shell=/sbin/nologin realname=Cacti\ user
+}
+
 destroot {
 # Copy Cacti files
-	file mkdir ${destroot}${cactidir}
-	system "cp -R ${worksrcpath}/* ${destroot}${cactidir}"
+    xinstall -d ${destroot}${cactidir}
+    system "cp -R ${worksrcpath}/* ${destroot}${cactidir}"
+    xinstall -d ${destroot}${prefix}/Library/LaunchAgents/
+    copy ${filespath}/org.macports.${name}.plist ${destroot}${prefix}/Library/LaunchAgents/
+    reinplace "s|%%PREFIX%%|${prefix}|g;s|%%CACTIDIR%%|${cactidir}|g" ${destroot}${prefix}/Library/LaunchAgents/org.macports.${name}.plist
+    xinstall -d ${destroot}${prefix}/share/examples/${name}
+    move ${destroot}${cactidir}/include/config.php ${destroot}${prefix}/share/examples/${name}/
+    move ${destroot}${cactidir}/include/global.php ${destroot}${prefix}/share/examples/${name}/
+    copy ${filespath}/nginx-cacti.conf ${destroot}${prefix}/share/examples/${name}/
+    reinplace "s|%%PREFIX%%|${prefix}|g" ${destroot}${prefix}/share/examples/${name}/nginx-cacti.conf
+    copy ${filespath}/mp-cacti.conf ${destroot}${prefix}/share/examples/${name}/
+    reinplace "s|%%PREFIX%%|${prefix}|g" ${destroot}${prefix}/share/examples/${name}/mp-cacti.conf
+    xinstall -d -m 755 -o cactiuser ${destroot}${prefix}/var/log/${name}
+
+    destroot.keepdirs ${destroot}${prefix}/var/log/${name}
+}
+
+post-activate {
+    if ![file exists ${cactidir}/include/config.php ] {
+        copy ${prefix}/share/examples/${name}/config.php ${cactidir}/include/
+    }
+    if ![file exists ${cactidir}/include/global.php ] {
+        copy ${prefix}/share/examples/${name}/global.php ${cactidir}/include/
+    }
+    if { ! [file exists ${prefix}/etc/nginx/nginx-cacti.conf ] && [file exists ${prefix}/etc/nginx] } {
+        copy ${prefix}/share/examples/${name}/nginx-cacti.conf ${prefix}/etc/nginx/nginx-cacti.conf
+    }
 }
 
 
@@ -44,77 +79,67 @@
 The full documentation may be found at http://www.cacti.net/documentation.php.
 
 
-1) Install PHP and MySQL 5 (not covered).  These instructions assume MySQL 5 was
-   installed from MacPorts.
+1) Install PHP and MySQL 5 (not covered). You can choose appropriate MySQL with variants.
 
    Check to make sure the PHP variable \"mysql.default_socket\" is set to path
-   ${prefix}/var/run/mysql5/mysqld.sock by viewing the output of this terminal command:
-	php -i
+   ${prefix}/var/run/mysql*/mysqld.sock by viewing the output of this terminal command:
+        $ php -i
 
    If not, locate the php.ini file for your version of PHP and type the path there.
 
 
-2) Set Cacti permissions.
-   sudo chown -R <cacti-user>:<cacti-group> ${prefix}/share/${name}/
+2) Set Cacti permissions for the scheduled task
+        $ sudo chown -R cactiuser:cactiuser ${cactidir}/rra/ ${cactidir}/log/
 
-
-3) Setup MySQL and prepare it for Cacti.
-   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
-	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.
+3) prepare MySQL for Cacti.
 
    Create a cacti MySQL user and cacti database.
-	mysql5 -u root -p (login with root password set above when prompted)
+    mysql5 -u root -p (login with root password set above when prompted)
         mysql> GRANT ALL ON cacti.* to cacti@localhost;
         mysql> SET PASSWORD FOR cacti@localhost = PASSWORD('<my-cactidb-pwd>');
         mysql> flush privileges;
-	mysql> create database cacti;
-	mysql> exit;
+    mysql> create database cacti;
+    mysql> exit;
 
    Import the cacti database.
-	sudo cat ${cactidir}/cacti.sql | mysql5 -u root -p cacti (cacti is the db name)
+    cat ${cactidir}/cacti.sql | mysql5 -u root -p cacti (cacti is the db name)
 
    Verify the Cacti Database.
-	mysql5 -u root -p
-	mysql> use cacti;
-	mysql> show tables;
-	mysql> exit;
-
-
-4) Edit ${cactidir}/include/config.php to match your MySQL information and url path.
-
-	\$database_type = \"mysql\";
-	\$database_default = \"cacti\";
-	\$database_hostname = \"localhost\";
-	\$database_username = \"cacti\";
-	\$database_password = \"<my-cactidb-pwd>\";
-
-
-5) Place a symlink for Cacti inside your Apache document root.
-
-	ln -s ${cactidir}  <Apache-docroot>/cacti
-
-
-6) Edit the Cacti user's crontab file.
-	sudo -u <cactiuser> crontab -e
-
-  Insert the crontab entry below:
-	*/5 * * * * ${prefix}/bin/php ${cactidir}/poller.php > /dev/null 2>&1
-
+    mysql5 -u cacti -p cacti
+    mysql> show tables;
+    mysql> exit;
+
+
+4) If not existing, edit ${cactidir}/include/config.php to match your MySQL information and url path.
+
+    \$database_type = \"mysql\";
+    \$database_default = \"cacti\";
+    \$database_hostname = \"localhost\";
+    \$database_username = \"cacti\";
+    \$database_password = \"<my-cactidb-pwd>\";
+
+   If using socket file, remember to check php.ini as said in 1)
+
+5) Include in your webserver
+   If Apache, Place a symlink for Cacti inside your Apache document root.
+        # ln -s ${cactidir}  <Apache-docroot>/cacti
+   If Nginx, include appropriate config. Example is included and copied
+   in ${prefix}/etc/nginx if existing
+
+
+6) Add poller.php scheduled task. A default plist has been installed to use w php55.
+   Adjust depending on your setup
+        # ln -s ${prefix}/Library/LaunchAgents/org.macports.${name}.plist /Library/LaunchAgents/
+        # launchctl load -w /Library/LaunchAgents/org.macports.${name}.plist
+   Poller log files are ${prefix}/var/log/cacti/poller*.log
+   You can enable log rotation by using template for system newsyslog
+        # cp ${prefix}/share/examples/${name}/mp-cacti.conf /private/etc/newsyslog.d/
 
 7) If you wish to use Cacti plugins, you must perform these additional steps.
 
    a) Download the Cacti plugin(s) you want to use and copy the unzipped directory to ${cactidir}/plugins/
       For example:
-	${cactidir}/plugins/weathermap/
+    ${cactidir}/plugins/weathermap/
 
    b) In the Cacti web GUI, go to Configuration -> Plugin Management and you should see your plugin(s) listed.
       Under 'Actions', click the the install icon to install each plugin.
@@ -124,17 +149,9 @@
 
 
 8) Go to http://localhost/cacti/install/index.php.
-	The default user/password is admin/admin.  Select 'New Install', enter
-	the paths for SNMP / RRDtool / PHP (see below), and click 'Finish'.
-	You may now use Cacti at http://localhost/cacti/index.php.
-
-Verify paths:
-	snmpwalk binary path: /usr/bin/snmpwalk
-	snmpget binary path: /usr/bin/snmpget
-
-	RRDtool binary path: ${prefix}/bin/rrdtool
-	PHP binary path: ${prefix}/bin/php (if not using MacPorts PHP, use appropriate path)
-
+    The default user/password is admin/admin.  Select 'New Install', enter
+    the paths for SNMP / RRDtool / PHP (adjusted to macports), and click 'Finish'.
+    You may now use Cacti at http://localhost/cacti/index.php.
 
 9) Create interface traffic graphs
    a) Devices -> Add
@@ -148,4 +165,101 @@
         -Select a graph type from the menu in the bottom right and click the 'Create' button beside it
 
    c) Place some or all a device's graphs on a graph tree from the Devices or Graph Management section
+
+10) If you want to use snmp on your mac, you can either use
+   * Apple default SNMP
+       # /usr/bin/snmpconf -g basic_setup
+       $ sudo launchctl load -w /System/Library/LaunchDaemons/org.net-snmp.snmpd.plist
+   * Macports
+       $ sudo port install net-snmp +server
+       # ${prefix}/bin/snmpconf -g basic_setup
+       # mv snmpd.conf ${prefix}/etc/
+       $ sudo port load net-snmp
 "
+
+if {![variant_isset mysql5] && ![variant_isset mysql51] && ![variant_isset mysql55] && ![variant_isset mariadb] && ![variant_isset percona]} {
+    default_variants +mysql56
+}
+
+default_variants-append      +superlinks
+
+## http://wotsit.thingy.com/haj/cacti/superlinks-plugin.html
+variant superlinks description { plugin superlinks } {
+    distfiles-append    http://wotsit.thingy.com/haj/cacti/superlinks-0.8.zip
+#    post-patch {
+#        system "cd ${worksrcpath}/plugins && unzip ${distpath}/superlinks-0.8.zip"
+#    }
+}
+
+## http://www.network-weathermap.com/manual/latest/pages/cacti-plugin.html
+variant weathermap description { plugin weathermap } {
+    depends_lib-append   port:pcre port:gd
+    distfiles-append     http://www.network-weathermap.com/files/php-weathermap-0.97c.zip
+
+    destroot-append {
+        ## unpack in cactidir/plugins/weathermap
+        ## chown -R cactiuser cactidir/plugins/weathermap/output
+    }
+
+notes-append "
+As a reminder, you will need php*-pcre php*-gd corresponding to your PHP release target.
+You will need to enable the plugin in ${cactidir}/includes/config.php. Add the following line:
+    \$plugins\[\] = 'weathermap';
+"
+}
+
+## http://forums.cacti.net/viewtopic.php?p=173542#p173542
+variant advancedping description { template advancedping } {
+    depends_run-append   port:fping
+    distfiles-append http://forums.cacti.net/download/file.php?id=18479 \
+        http://forums.cacti.net/download/file.php?id=18480
+
+    reinplace "s|/usr/sbin/fping|${prefix}/sbin/fping|g" ss_fping.php
+}
+
+## include Mac template ?
+
+## cpu snmp removed by apple. no but some random conditions can happen, it seems
+## http://macosx.com/forums/mac-os-x-system-mac-software/273289-net-snmp-cpu-stats.html
+
+variant mysql5 \
+    conflicts mysql51 mysql55 mysql56 mariadb percona \
+    description "Enable MySQL 5.1 support" {
+
+    depends_lib-append          port:mysql5
+}
+
+variant mysql51 \
+    conflicts mysql5 mysql55 mysql56 mariadb percona \
+    description "Enable MySQL 5.1 support" {
+
+    depends_lib-append          port:mysql51
+}
+
+variant mysql55 \
+    conflicts mysql5 mysql51 mysql56 mariadb percona \
+    description "Enable MySQL 5.5 support" {
+
+    depends_lib-append          port:mysql55
+}
+
+variant mysql56 \
+    conflicts mysql5 mysql51 mysql55 mariadb percona \
+    description "Enable MySQL 5.6 support" {
+
+    depends_lib-append          port:mysql56
+}
+
+variant mariadb \
+    conflicts mysql5 mysql51 mysql55 mysql56 percona \
+    description "Enable MariaDB (MySQL) support" {
+
+    depends_lib-append          port:mariadb
+}
+
+variant percona \
+    conflicts mysql5 mysql51 mysql55 mysql56 mariadb \
+    description "Enable Percona (MySQL) support" {
+        depends_lib-append          port:percona
+}
+
