--- _orig/net/bro/Portfile	2013-11-22 21:30:13.000000000 -0500
+++ net/bro/Portfile	2014-03-22 20:26:45.000000000 -0400
@@ -5,6 +5,7 @@
 
 name                bro
 version             2.2
+revision            1
 categories          net security
 platforms           darwin
 maintainers         gmail.com:tanawts openmaintainer
@@ -17,11 +18,6 @@
                     traffic and looks for suspicious activity.
 
 homepage            http://www.bro.org/
-master_sites        ${homepage}downloads/release/ \
-                    ${homepage}downloads/archive/
-
-checksums           rmd160  ad1820eae034062c0e1125e4a4ac1e61960ad126 \
-                    sha256  86909361c2d804681f314604a6ce763be663937b2f045c8d25d810528a633dc9
 
 depends_build       port:cmake \
                     port:libgeoip \
@@ -41,23 +37,136 @@
 patchfiles          patch-CMakeLists.txt.diff
 
 universal_variant   no
+test.run            yes
+test.target         test-all
+
+## FIXME! ${prefix}/etc/bro, ${prefix}/var/spool/bro, ${prefix}/var/log/bro
+configure.args      --enable-mobile-ipv6 --conf-files-dir=${prefix}/etc/bro
 
-configure.args      --enable-mobile-ipv6
 
 post-destroot {
-    move ${destroot}${prefix}/etc/broccoli.conf ${destroot}${prefix}/etc/broccoli.conf.dist
+    xinstall -d ${destroot}${prefix}/share/examples/${name}
+    move ${destroot}${prefix}/etc/bro/broccoli.conf ${destroot}${prefix}/share/examples/${name}/
+    move ${destroot}${prefix}/etc/bro/node.cfg ${destroot}${prefix}/share/examples/${name}/
+    move ${destroot}${prefix}/etc/bro/networks.cfg ${destroot}${prefix}/share/examples/${name}/
+    move ${destroot}${prefix}/etc/bro/broctl.cfg ${destroot}${prefix}/share/examples/${name}/
+    reinplace "s|${prefix}/spool|${prefix}/var/spool/bro|" ${destroot}${prefix}/share/examples/${name}/broctl.cfg
+    reinplace "s|${prefix}/logs|${prefix}/var/log/bro|" ${destroot}${prefix}/share/examples/${name}/broctl.cfg
+
+    reinplace "s|/usr/bin/python|${prefix}/bin/python2.7|" ${destroot}${prefix}/bin/broctl
+    reinplace "s| /usr/bin/env python|${prefix}/bin/python2.7|" ${destroot}${prefix}/bin/trace-summary
+
+    xinstall -d ${destroot}${prefix}/Library/LaunchAgents/
+    copy ${filespath}/org.macports.bro.plist ${destroot}${prefix}/Library/LaunchAgents/
+    reinplace "s|%%PREFIX%%|${prefix}|g" ${destroot}${prefix}/Library/LaunchAgents/org.macports.bro.plist
+
+    xinstall -d ${destroot}${prefix}/etc/${name}
+    xinstall -d ${destroot}${prefix}/var/log/${name}
+    destroot.keepdirs ${destroot}${prefix}/etc/${name} \
+        ${destroot}${prefix}/var/log/${name}
 }
 
 post-activate {
-    if {![file exists ${prefix}/etc/broccoli.conf]} {
-        copy ${prefix}/etc/broccoli.conf.dist ${prefix}/etc/broccoli.conf
+    if {![file exists ${prefix}/etc/bro/broccoli.conf]} {
+        copy ${prefix}/share/examples/${name}/broccoli.conf ${prefix}/etc/bro/broccoli.conf
+    }
+    if {![file exists ${prefix}/etc/bro/node.cfg]} {
+        copy ${prefix}/share/examples/${name}/node.cfg ${prefix}/etc/bro/node.cfg
+    }
+    if {![file exists ${prefix}/etc/bro/networks.cfg]} {
+        copy ${prefix}/share/examples/${name}/networks.cfg ${prefix}/etc/bro/networks.cfg
+    }
+    if {![file exists ${prefix}/etc/bro/broctl.cfg]} {
+        copy ${prefix}/share/examples/${name}/broctl.cfg ${prefix}/etc/bro/broctl.cfg
     }
 }
 
-notes \
-"You'll need to set your BROHOME to ${prefix}/share/bro and your\
-BROPATH to ${prefix}/share/bro/site:${prefix}/share/bro/policy\
-to use the provided policies."
+startupitem.create      yes
+startupitem.netchange   yes
+startupitem.start       "${prefix}/bin/broctl start"
+startupitem.stop        "${prefix}/bin/broctl stop"
+startupitem.restart     "${prefix}/bin/broctl restart"
+
+notes "
+You'll need to set your BROHOME to ${prefix}/share/bro and your
+BROPATH to ${prefix}/share/bro/site:${prefix}/share/bro/policy
+to use the provided policies. (which user?)
+
+Check online documentation to finish install
+http://www.bro.org/sphinx/quickstart/index.html
+
+1) review config: node.cfg, network.cfg, broctl.cfg
+   especially network interface, MailTo
+   Existing files have not been modified
+
+2) Run as root or w sudo
+     # broctl
+     \[BroControl\] > install
+     \[BroControl\] > start
+3) Use scheduled task for maintenance
+  # ln -s ${prefix}/Library/LaunchAgents/org.macports.bro.plist /Library/LaunchAgents/
+  # launchctl load -w /Library/LaunchAgents/org.macports.bro.plist
+
+**** UPGRADING ****
+
+Paths have been changed to respect macports hierarchy
+${prefix}/etc -> ${prefix}/etc/bro
+${prefix}/spool -> ${prefix}/var/spool/bro
+${prefix}/logs -> ${prefix}/var/log/bro
+
+BUG: System App firewall ask at each launch if bro can accept incoming connections.
+
+"
+
+if {${subport} eq ${name}} {
+    master_sites        ${homepage}downloads/release/ \
+                        ${homepage}downloads/archive/
+    
+    checksums           bro-${version}.tar.gz \
+                        rmd160  ad1820eae034062c0e1125e4a4ac1e61960ad126 \
+                        sha256  86909361c2d804681f314604a6ce763be663937b2f045c8d25d810528a633dc9 \
+    post-patch {
+        reinplace "s|\${BroBase}/spool|\${BroBase}/var/spool/bro|" ${worksrcpath}/aux/broctl/BroControl/options.py
+        reinplace "s|\${BroBase}/logs|\${BroBase}/var/log/bro|" ${worksrcpath}/aux/broctl/BroControl/options.py
+        reinplace "s|\${PREFIX}/logs|\${PREFIX}/var/log/bro|" ${worksrcpath}/aux/broctl/CMakeLists.txt
+        reinplace "s|{BroBase}/logs|{BroBase}/var/log/bro|" ${worksrcpath}/aux/broctl/doc/broctl.rst
+        reinplace "s|\${PREFIX}/spool|\${PREFIX}/var/spool/bro|" ${worksrcpath}/aux/broctl/CMakeLists.txt
+        reinplace "s|{BroBase}/spool|{BroBase}/var/spool/bro|" ${worksrcpath}/aux/broctl/doc/broctl.rst
+    }
+
+}
+
+subport ${name}-devel {
+
+    fetch.type git
+    git.url    git://git.bro.org/bro
+    ## no support for tag, last one is release
+    #git.branch devel
+    ## 2014-03-21 NOK
+    #git.branch 0f3ed1a553ea40feb50037fbdc45733eeaadd50b
+    #git.branch 908b574c1823d2181cd628b11e6cd0117e2b449d
+    #git.branch 066473b1f1bc71fc500a0a0ec447e1603732c53a
+    patchfiles-delete   patch-CMakeLists.txt.diff
+
+    conflicts  ${name}
+}
+
+default_variants      +mhr
+
+## Extra scripts? http://www.bro.org/sphinx/scripts/scripts.html
+variant mhr {
+	master_sites-append   http://www.bro.org/sphinx/_downloads/
+	distfiles-append    detect-MHR.bro
+	checksums-append    \
+	                    detect-MHR.bro \
+	                    rmd160  a84dc9b4adb78f9af0985dde36e09758a1280ff5 \
+	                    sha256  02b3c9fa5d03a1ae2583e55ca6d17190e45214c1920968867bed13051868ca0d
+
+	post-destroot {
+		## no further actions needed. will be loaded by bro at next start
+		copy ${distpath}/detect-MHR.bro ${destroot}${prefix}/share/broctl/scripts/
+	}
+}
 
 livecheck.type      regex
 livecheck.url       ${homepage}download/
