--- Portfile.orig	2014-07-03 16:58:38.000000000 +0100
+++ Portfile	2014-07-09 12:00:50.000000000 +0100
@@ -1,7 +1,6 @@
 # $Id$
 #======================================================================
 # Portfile for LaTeXML
-# (draft)
 #======================================================================
 PortSystem 1.0
 name LaTeXML
@@ -14,6 +13,13 @@
    including HTML, XHTML, ePub with MathML.
 
 homepage http://dlmf.nist.gov/LaTeXML/
+
+# Written in Perl, but it is an application, not just modules
+PortGroup perl5 1.0
+perl5.branches 5.8 5.10 5.12 5.14 5.16 5.18 5.20
+perl5.setup ${name} ${version}
+perl5.link_binaries_suffix
+
 platforms darwin
 master_sites http://dlmf.nist.gov/LaTeXML/releases/
 checksums rmd160 cfafc6f4f5e276543333f907a1ec0db3c25e424f \
@@ -21,54 +27,62 @@
 # Use:
 # openssl rmd160 LaTeXML-0.8.0.tar.gz
 # openssl sha256 LaTeXML-0.8.0.tar.gz
+
 #============================================================
 # Dependencies
-depends_lib p5.16-archive-zip \
-  p5.16-file-which \
-  p5.16-getopt-long \
-  p5.16-image-size \
-  p5.16-io-string \
-  p5.16-json-xs \
-  p5.16-libwww-perl \
-  p5.16-parse-recdescent \
-  p5.16-time-hires \
-  p5.16-uri \
-  p5.16-xml-libxml \
-  p5.16-xml-libxslt \
-  p5.16-perlmagick
-
-# That last seems to be the way to depend on TeX
-# being installed, without necessarily requiring
-# _specifically_ texlive (eg MacTeX or something
-# else perhaps can work, if /usr/texbin is in
-# binpath in macports.conf)
-#
+if {${perl5.major} != ""} {
+depends_lib-append \
+  port:p${perl5.major}-archive-zip \
+  port:p${perl5.major}-file-which \
+  port:p${perl5.major}-getopt-long \
+  port:p${perl5.major}-image-size \
+  port:p${perl5.major}-io-string \
+  port:p${perl5.major}-json-xs \
+  port:p${perl5.major}-libwww-perl \
+  port:p${perl5.major}-parse-recdescent \
+  port:p${perl5.major}-time-hires \
+  port:p${perl5.major}-uri \
+  port:p${perl5.major}-xml-libxml \
+  port:p${perl5.major}-xml-libxslt \
+  port:p${perl5.major}-perlmagick
+}
 # Also requires: DB_File, Pod::Parser and Test::More
 # but those should be in Perl's core modules.
+
 #============================================================
 # LaTeXML works MUCH better if there is a TeX installed.
-# It (hopefully) will find any installation if it can find kpsewhich
-# However, we don't require a dependency here since
-# users may prefer MacTeX, or MacPort's texlive or ....
-#  bin:texhash:texlive
-notes "${name} works best with some version of TeX installed. \
-   Please install MacTeX or 'sudo port install texlive' (or other system) first."
+# - it uses some latex style files from texlive within bindings
+# - it can install its own style files for use within tex/latex
+# We could simply depend on texlive, but some folks prefer MacTeX
+# and object to 2nd multi-GB download!  So we define variants.
 
-#============================================================
-# Configuration
-# Override configure to do it the perl way.
-configure.cmd ${prefix}/bin/perl5.16 Makefile.PL
-
-# Note that installation does
-#   make install DESTDIR=${destroot}
-# which stages the installation to $destroot, NOT the eventual final installation.
+notes "${name} works best with some version of TeX installed. \
+   Please consider +texlive variant, or PRE-install MacTeX and use +mactex variant."
 
-#============================================================
-# Do this to enable testing
-# test.run yes
+# The texlive variant depends on texlive
+# and install latexml's stylefiles to texlive's texmf
+variant texlive description {Build with (macports) texlive support} {
+  notes "Using texlive for TeX: will install styles to its texmf"
+  depends_lib-append texlive
+  configure.args-append TEXMF=[exec kpsewhich --expand-var='\$TEXMFLOCAL']
+  post-activate { system "mktexlsr" }
+}
+
+# The mactex variant expects MacTeX to be installed
+# and installs latexml's stylefiles to MacTeX's texmf
+variant mactex description {Build with MacTeX support} {
+  # check if MacTeX actually seems to be there...
+  pre-configure {
+    if {![file executable /usr/texbin/kpsewhich]} {
+      ui_error "I cannot find MacTeX's kpswhich in /usr/texbin; aborting installation"
+      return -code error
+       } }
+  notes "Using MacTeX for TeX: will install styles to MacTeX's texmf
+  (outside macport's common directory structure)"
+  configure.args-append TEXMF=[exec /usr/texbin/kpsewhich --expand-var='\$TEXMFLOCAL']
+  post-activate { system /usr/texbin/mktexlsr }
+  # AND, since we're installing files outside macport's normal directories
+  destroot.violate_mtree yes
+}
 
 #============================================================
-# Get TeX to notice our style files, since this isn't done
-# when staging the installation to ${destpath}
-post-activate { system "mktexlsr" }
-
