--- _orig/net/asterisk/Portfile	2013-07-15 12:03:06.000000000 -0400
+++ net/asterisk/Portfile	2014-04-19 15:17:32.000000000 -0400
@@ -4,7 +4,7 @@
 PortSystem          1.0
 
 name                asterisk
-version             1.6.2.20
+version             12.1.1
 categories          net
 license             GPL-2
 platforms           darwin
@@ -33,23 +33,35 @@
                     port:spandsp-devel \
                     port:speex \
                     port:sqlite3 \
-                    port:wget
+                    port:wget \
+                    port:jansson
+## Optional components: libpri DAHDI
+
+## need specific ice-cpp w patch https://wiki.asterisk.org/wiki/display/TOP/System+Requirements
+depends_build       port:cmake port:boost
+# port:zeroc-ice34 => fails to build
 
-checksums           rmd160  8584d0f2e274517ce84576dfa7e936b9befb9fe2 \
-                    sha256  4f3ef1de4644195e0d91816d6741c46ddb5d9ad28b07e631f594674ac9006a27
+checksums           rmd160  def51844b5bca9ad3faa8964272c58cbfaf6c150 \
+                    sha256  c53f51b2dea22ca5b2f042562cfc9e021f9dbda64293ef3652f569f86f65b4ba
 
 startupitem.create      yes
 startupitem.netchange   yes
-startupitem.executable  ${prefix}/sbin/asterisk -d
+startupitem.executable  ${prefix}/sbin/asterisk -d -U asterisk -G asterisk -C ${prefix}/etc/asterisk.conf
 
 universal_variant   no
 
-patchfiles          makeopts.in.diff menuselect_Makefile.diff
+#patchfiles          makeopts.in.diff menuselect_Makefile.diff
+patchfiles          patch-makeopts.in.diff
 
 configure.args      --without-h323 \
-                    --without-netsnmp
+                    --without-netsnmp \
+                    --with-crypto=${prefix} --with-curses=${prefix} \
+                    --with-iconv=${prefix} --with-sqlite3=${prefix} \
+                    --with-ssl=${prefix}
 
 configure.optflags  -O3
+## http://comments.gmane.org/gmane.comp.telephony.pbx.asterisk.devel/62632
+configure.compiler  macports-gcc-4.9
 
 build.env           ASTCFLAGS=-I${prefix}/include \
                     ASTLDFLAGS=-L${prefix}/lib \
@@ -67,15 +79,68 @@
                     ${destroot}${prefix}/var/spool/asterisk/tmp \
                     ${destroot}${prefix}/var/spool/asterisk/voicemail
 
+#pre-configure {
+#    system "cd ${worksrcpath} && ./cmake/init-cmake.sh -DCMAKE_INSTALL_PREFIX=${prefix}"
+#}
+
+post-configure {
+    addgroup asterisk
+    add_users asterisk gid=[existsgroup asterisk] home=${prefix}/var/spool/asterisk shell=/sbin/nologin realname=Asterisk\ user
+}
+
 variant home_sound_cache description {Use/Build sound file cache from .asterisk_sounds_cache in your home directory} {
     configure.args-append --with-sounds-cache=~/.asterisk_sounds_cache
 }
 
+variant snmp description { snmp support } {
+    depends_lib           port:net-snmp
+    configure.args-delete --without-netsnmp
+    configure.args-append --with-netsnmp=${prefix}
+}
+
+variant libpri description { libpri support } {
+    depends_lib           port:libpri
+    configure.args-append --with-pri=${prefix}
+}
+
+variant dahdi description { dahdi support } {
+    depends_lib           port:dahdi
+    configure.args-append --with-dahdi=${prefix}
+}
+
+variant ldap description { ldap support } {
+    depends_lib-append    port:openldap
+    configure.args-append --with-ldap=${prefix}
+}
+
+variant libiodbc description { use libiodbc for ODBC access } {
+    depends_lib-append port:libiodbc
+}
+
+variant unixodbc description { use unixodbc for ODBC access } {
+    depends_lib-append port:unixODBC
+}
+
 post-destroot {
-    system "rsync -a ${worksrcpath}/configs/*.sample ${destroot}${prefix}/etc/asterisk"
-    xinstall -m 644 ${filespath}/asterisk.conf.sample.in ${destroot}${prefix}/etc/asterisk/asterisk.conf.sample
-    reinplace "s|@PREFIX@|${prefix}|g" ${destroot}${prefix}/etc/asterisk/asterisk.conf.sample
-    xinstall -d ${destroot}${prefix}/var/log/asterisk \
+    xinstall -d ${destroot}${prefix}/share/examples/${name}
+    system "rsync -a ${worksrcpath}/configs/*.sample ${destroot}${prefix}/share/examples/${name}/"
+    xinstall -m 644 ${filespath}/asterisk.conf.sample.in ${destroot}${prefix}/share/examples/${name}/asterisk.conf.sample
+    reinplace "s|@PREFIX@|${prefix}|g" ${destroot}${prefix}/share/examples/${name}/asterisk.conf.sample
+    xinstall -d -o asterisk -g asterisk ${destroot}${prefix}/var/log/asterisk \
                 ${destroot}${prefix}/var/run/asterisk \
                 ${destroot}${prefix}/var/spool/asterisk/outgoing
 }
+
+post-activate {
+     foreach f [glob -directory ${prefix}/share/examples/${name} *.sample] {
+         set fname [file tail $f]
+         set fname trimright .sample
+         if ![file exists ${prefix}/etc/${name}/${fname} ] {
+            copy ${f} ${prefix}/etc/${name}/${fname}
+         }
+     }
+}
+
+livecheck.type     regex
+livecheck.url       ${homepage}/downloads/asterisk/all-asterisk-versions
+livecheck.regex     "<p>Latest Version - (\\d+(?:\\.\\d+)*)</p>"
