Index: Portfile
===================================================================
--- Portfile	(revision 84373)
+++ Portfile	(working copy)
@@ -5,8 +5,6 @@
 name		swig
 version		2.0.4
 
-# remember to update the binding ports at the same time as swig
-
 categories	devel
 maintainers	kevin jwa
 
@@ -24,7 +22,7 @@
 
 homepage	http://www.swig.org/
 platforms	darwin
-master_sites	sourceforge
+master_sites	sourceforge:project/swig/swig/swig-${version}
 
 checksums           md5     4319c503ee3a13d2a53be9d828c3adc0 \
                     sha1    ace563563a8f6f367ba82236e5bea240adea0d94 \
@@ -33,44 +31,115 @@
 depends_build	port:bison \
     port:gsed
 
-depends_lib	port:pcre
-
 post-patch {
     # The configure script should use GNU sed
     reinplace "s/\[\[:<:\]\]sed\[\[:>:\]\]/gsed/g" ${worksrcpath}/configure
 }
 
-foreach lang {python perl5 gcj guile mzscheme ruby php ocaml pike lua chicken allegrocl clisp r tcl csharp octave java go} {
-    configure.args-append --without-${lang}
-}
-
 build.target
 
-set languages   [list typemaps]
-set docdir      ${prefix}/share/doc/${name}-${version}
-
 test.run        yes
 test.target     check
 
-pre-destroot {
-    # This must be evaluated after variants selection.
-    destroot.args   lib-languages="[join ${languages}]"
+array set bindings {
+    allegro     {port:allegro           allegrocl}
+    chicken     {port:chicken           chicken}
+    clisp       {port:clisp             clisp}
+    csharp      {port:mono              csharp}
+    gcj         {port:gcc44             "gcj=${prefix}/bin/gcj-mp-4.4 --with-gcjh=${prefix}/bin/gcjh-mp-4.4"}
+    go          {port:go                go}
+    guile       {port:guile             guile}
+    java        {bin:java:kaffe         java}
+    lua         {port:lua               lua}
+    mzscheme    {port:mzscheme          mzscheme}
+    ocaml       {port:ocaml             ocaml}
+    octave      {path:bin/octave:octave octave}
+    perl        {path:bin/perl:perl5    perl5=${prefix}/bin/perl}
+    php5        {path:bin/php:php5      php}
+    pike        {port:pike              pike}
+    python      {port:python_select     python}
+    r           {port:R                 r}
+    ruby        {port:ruby              ruby}
+    tcl         {port:tcl               tcl}
 }
+array set prettynames {allegrocl Allegro chicken Chicken clisp CLISP csharp "C#"
+    gcj GCJ go Go guile Guile java Java lua Lua mzscheme MzScheme
+    ocaml "Objective Caml" octave Octave perl5 Perl php "PHP 5" pike Pike
+    python Python r R ruby Ruby tcl Tcl}
 
-post-destroot {
-    xinstall -d ${destroot}${docdir}
-    xinstall -m 0644 -W ${worksrcpath} ANNOUNCE CHANGES LICENSE README \
-        TODO ${destroot}${docdir}
+options         swig.lang
+default         swig.lang ""
+
+foreach lang [lsort [array names bindings]] {
+    set arg      [lindex $bindings($lang) 1]
+    set arg_name [lindex [split ${arg} =] 0]
+    subport swig-${lang} "
+        depends_lib-append      [lindex $bindings($lang) 0]
+        configure.args-append   --with-${arg}
+        destroot.args           lib-languages=\"${arg_name}\"
+        swig.lang               $arg_name
+    "
+    if {${swig.lang} != $arg_name} {
+        configure.args-append --without-${arg_name}
+    }
 }
 
-variant doc description {Install extra documentation} {
+subport swig-gcj {
+    variant gcc43 conflicts gcc44 description {build using GCJ 4.3} {
+        depends_lib-delete port:gcc44
+        depends_lib-append port:gcc43
+        configure.args-delete --with-gcj=${prefix}/bin/gcj-mp-4.4 --with-gcjh=${prefix}/bin/gcjh-mp-4.4
+        configure.args-append --with-gcj=${prefix}/bin/gcj-mp-4.3 --with-gcjh=${prefix}/bin/gcjh-mp-4.3
+    }
+    
+    # just a placeholder variant until gcc45 gets added
+    variant gcc44 conflicts gcc43 description {build using GCJ 4.4} {}
+    
+    if {![variant_isset gcc43]} {
+        default_variants +gcc44
+    }
+}
+
+if {${swig.lang} == ""} {
+    depends_lib	    port:pcre
+
+    set docdir      ${prefix}/share/doc/${name}-${version}
+    
+    destroot.args   lib-languages="typemaps"
+    
     post-destroot {
-        xinstall -d ${destroot}${docdir}/html
-        eval xinstall -m 0644 [glob ${worksrcpath}/Doc/Manual/*.{css,html,pdf,png}] \
-            ${destroot}${docdir}/html
+        xinstall -d ${destroot}${docdir}
+        xinstall -m 0644 -W ${worksrcpath} ANNOUNCE CHANGES LICENSE README \
+            TODO ${destroot}${docdir}
     }
+    
+    variant doc description {Install extra documentation} {
+        post-destroot {
+            xinstall -d ${destroot}${docdir}/html
+            eval xinstall -m 0644 [glob ${worksrcpath}/Doc/Manual/*.{css,html,pdf,png}] \
+                ${destroot}${docdir}/html
+        }
+    }
+    
+    livecheck.type      regex
+    livecheck.url       http://www.swig.org/download.html
+    livecheck.regex     ${name}-(\\d+(?:\\.\\d+)*)
+} else {
+    description     $prettynames(${swig.lang}) binding for swig
+    long_description    \
+        SWIG is a software development tool that connects programs written in C \
+        and C++ with a variety of high-level programming languages. This is the \
+        $prettynames(${swig.lang}) binding.
+    supported_archs     noarch
+    depends_lib-append  port:swig
+    livecheck.type      none
+    post-destroot {
+        delete ${destroot}${prefix}/bin
+        delete ${destroot}${prefix}/share/man
+        foreach f [glob -directory ${destroot}${prefix}/share/swig/${version} *] {
+            if {[file tail $f] != ${swig.lang}} {
+                delete $f
+            }
+        }
+    }
 }
-
-livecheck.type      regex
-livecheck.url       http://www.swig.org/download.html
-livecheck.regex     ${name}-(\\d+(?:\\.\\d+)*)
