--- Portfile.orig	2011-12-11 23:35:03.000000000 +0100
+++ Portfile	2011-12-13 21:49:37.000000000 +0100
@@ -6,12 +6,12 @@
 
 name                            opencv
 version                         2.3.1a
-revision                        1
+revision                        2
 set short_version               [strsed ${version} {g/[^0-9.].*$//}]
 categories                      graphics science
 license                         BSD
 platforms                       darwin
-maintainers                     nomaintainer
+maintainers                     gmail:marin.saric openmaintainer
 
 description                     Intel(R) Open Source Computer Vision Library
 
@@ -35,39 +35,37 @@
 depends_build-append            port:pkgconfig
 
 depends_lib-append              port:zlib \
-                                path:lib/libavcodec.dylib:ffmpeg \
                                 port:libpng \
                                 port:tiff \
                                 port:jasper \
                                 port:jpeg \
-                                port:bzip2 \
-                                port:dirac \
-                                port:faac \
-                                port:faad2 \
-                                port:lame \
-                                port:schroedinger \
-                                port:liboil \
-                                port:libtheora \
-                                port:libvorbis \
-                                port:libogg \
-                                port:x264 \
-                                port:orc
+                                port:bzip2
 
 # liboil is not universal
 universal_variant               no
 
+default_variants                +eigen +ffmpeg +python27 +tbb +also_static
+
 patchfiles                      patch-CMakeLists.txt.diff \
-                                patch-install_name.diff
+                                patch-install_name.diff \
+                                patch-OpenCVConfig.cmake.in.diff \
+                                patch-modules_highgui_CMakeLists.txt.diff \
+                                patch-samples_CMakeLists.txt.diff \
+                                patch-samples_c_build_all.sh.diff \
+                                patch-samples_c_CMakeLists.txt.diff \
+                                patch-samples_cpp_CMakeLists.txt.diff
 
 if {[variant_isset universal] && [variant_exists universal]} {
-    patchfiles-append           patch-pch-CMakeLists.txt.diff
+    configure.args-append       -DUSE_PRECOMPILED_HEADERS=OFF
 }
 
-configure.args-append           -DBUILD_NEW_PYTHON_SUPPORT=OFF \
-                                -DBUILD_EXAMPLES=ON \
-                                -DINSTALL_C_EXAMPLES=ON \
-                                -DBZIP2_LIBRARIES=${prefix}/lib/libbz2.dylib \
-                                -DZLIB_LIBRARY=${prefix}/lib/libz.dylib \
+# Common options
+configure.args-append           -DCMAKE_PREFIX_PATH=\"${prefix}\;/usr\" \
+                                -DOPENCV_BUILT_SHARED_AND_STATIC=OFF \
+                                -DBUILD_EXAMPLES=OFF \
+                                -DBUILD_TESTS=OFF \
+                                -DWITH_EIGEN=OFF \
+                                -DWITH_FFMPEG=OFF \
                                 -DWITH_OPENEXR=OFF \
                                 -DWITH_1394=OFF
 
@@ -75,6 +73,147 @@
     configure.compiler llvm-gcc-4.2
 }
 
+# Prepare the out-of-source cmake build for shared and static versions
+set sharedsrcpath "${worksrcpath}/build-shared"
+set staticsrcpath "${worksrcpath}/build-static"
+set destrootstatic "${destroot}-static"
+
+# In out-of-source builds, the cmake toplevel is one directory above
+configure.pre_args .. "${configure.pre_args}"
+
+# Utility routines to switch between different build types
+set build_state_last_opts ""
+
+proc adjust_build_vars {new_opts new_path} {
+    global build_state_last_opts
+
+    configure.dir "${new_path}"
+    build.dir "${new_path}"
+    destroot.dir "${new_path}"
+
+    configure.args-delete "${build_state_last_opts}"
+    configure.args-append "${new_opts}"
+
+    set build_state_last_opts "${new_opts}"
+}
+
+proc get_python_version {} {
+    if {[variant_isset python26]} {return "2.6"}
+    if {[variant_isset python27]} {return "2.7"}
+    return ""
+}
+
+proc python_variant_set {} {
+    return [expr {[get_python_version] != {}}]
+}
+
+proc build_state {state} {
+    global UI_PREFIX name sharedsrcpath staticsrcpath destroot destrootstatic
+
+    ui_msg "$UI_PREFIX Setting the build state for ${name} to ${state}"
+
+    set shared_opts [ list \
+                          -DINSTALL_C_EXAMPLES=ON \
+                          -DBUILD_SHARED_LIBS=ON \
+                          -DWITH_JASPER=ON ]
+
+    # Add python support to the shared version if a python variant was specified
+    if {[python_variant_set]} {
+        global frameworks_dir prefix
+
+        set py_version [get_python_version]
+        set python_framework ${frameworks_dir}/Python.framework/Versions/${py_version}
+
+        lappend shared_opts \
+            -DINSTALL_PYTHON_EXAMPLES=ON \
+            -DBUILD_NEW_PYTHON_SUPPORT=ON \
+            -DPYTHON_EXECUTABLE=${prefix}/bin/python${py_version} \
+            -DPYTHON_LIBRARY=${prefix}/lib/libpython${py_version}.dylib \
+            -DPYTHON_INCLUDE_DIR=${python_framework}/Headers \
+            -DPYTHON_PACKAGES_PATH=${python_framework}/lib/python${py_version}/site-packages
+    } else {
+        lappend shared_opts \
+            -DINSTALL_PYTHON_EXAMPLES=OFF \
+            -DBUILD_NEW_PYTHON_SUPPORT=OFF
+    }
+
+    if {[variant_isset tbb]} {
+        lappend shared_opts -DWITH_TBB=ON
+    } else {
+        lappend shared_opts -DWITH_TBB=OFF
+    }
+
+    if {[variant_isset qt4]} {
+        lappend shared_opts \
+            -DWITH_QT=ON \
+            -DWITH_QT_OPENGL=ON
+    } else {
+        lappend shared_opts -DWITH_QT=OFF
+    }
+
+    if {[variant_isset full_docs]} {
+        lappend shared_opts -DBUILD_DOCS=ON
+    } else {
+        lappend shared_opts -DBUILD_DOCS=OFF
+    }
+
+    # Python support can be turned off while building static libraries
+    # since the shared library bindings will be used anyway
+    # Same goes for the documentation
+    set static_opts [ list \
+                          -DINSTALL_C_EXAMPLES=OFF \
+                          -DBUILD_SHARED_LIBS=OFF \
+                          -DWITH_JASPER=OFF \
+                          -DBUILD_DOCS=OFF \
+                          -DBUILD_NEW_PYTHON_SUPPORT=OFF \
+                          -DINSTALL_PYTHON_EXAMPLES=OFF ]
+
+    # Unless explicitly asked, we will omit Intel TBB from the static
+    # build, because it introduces a dynamic library dependency
+    if {[variant_isset tbb_in_static]} {
+        lappend static_opts -DWITH_TBB=ON
+    } else {
+        lappend static_opts -DWITH_TBB=OFF
+    }
+
+    if {[variant_isset qt4_in_static]} {
+        lappend static_opts \
+            -DWITH_QT=ON \
+            -DWITH_QT_OPENGL=ON
+    } else {
+        lappend static_opts -DWITH_QT=OFF
+    }
+
+    switch "${state}" {
+        shared {
+            adjust_build_vars "${shared_opts}" "${sharedsrcpath}"
+            destroot.destdir "DESTDIR=${destroot}"
+        }
+        static {
+            adjust_build_vars "${static_opts}" "${staticsrcpath}"
+            destroot.destdir "DESTDIR=${destrootstatic}"
+        }
+    }
+}
+
+# Make sure to create out-of-source build directories
+post-extract {
+    global sharedsrcpath staticsrcpath
+
+    # Prepare the out-of-source build directories for
+    # both the shared and static libaries
+    system "mkdir ${sharedsrcpath} && mkdir ${staticsrcpath}"
+
+    # Start with the shared library setup
+    # This step should be done on pre-configure, but cmake portgroup
+    # already implements pre-configure
+    build_state shared
+}
+
+post-patch {
+    reinplace "s|/usr/local|${prefix}|g" ${worksrcpath}/CMakeLists.txt
+}
+
 platform darwin {
     # Use macros in /usr/include/stdint.h with C++ compiler
     # http://roundup.libav.org/issue2093
@@ -96,6 +235,82 @@
             return -code error "incompatible ${name} version active"
         }
     }
+
+    # Start with the shared libary setup
+    build_state shared
+}
+
+post-build {
+    global UI_PREFIX name sharedsrcpath staticsrcpath
+
+    if {[variant_isset also_static]} {
+        # Continue with the static library build
+        build_state static
+
+        ui_msg "$UI_PREFIX Configuring ${name} again for static libraries"
+        command_exec configure
+        ui_msg "$UI_PREFIX Building ${name} static libraries"
+        portbuild::build_main
+    }
+
+    if {[variant_isset full_docs]} {
+        # Continue building the documentation in the shared library build
+        build_state shared
+
+        ui_msg "$UI_PREFIX Building LaTeX documentation"
+        system "cd ${sharedsrcpath} && make docs"
+
+        ui_msg "$UI_PREFIX Building HTML documentation"
+        system "cd ${sharedsrcpath} && make html_docs"
+    }
+}
+
+pre-destroot {
+    # Continue to the shared library installation
+    build_state shared
+}
+
+post-destroot {
+    global UI_PREFIX name sharedsrcpath destroot destrootstatic
+
+    if {[variant_isset also_static]} {
+        # Continue with the static library installation
+        build_state static
+    
+        ui_msg "$UI_PREFIX Staging ${name} again for static libraries"
+        command_exec destroot
+
+        # Copy over the static libraries
+        set static_libs [glob ${destrootstatic}${prefix}/lib/*.a]
+        foreach static_lib ${static_libs} {
+            file rename ${static_lib} ${destroot}${prefix}/lib
+        }
+
+        # The static library config works for both static and shared OpenCV
+        set config_path ${prefix}/share/OpenCV/OpenCVConfig.cmake
+        file copy -force ${destrootstatic}${config_path} ${destroot}${config_path}
+        
+        # Continue with the shared library for install
+        build_state shared
+    }
+
+    if {[variant_isset full_docs]} {
+        ui_msg "$UI_PREFIX Staging ${name} full documentation"
+        set doc_output_path ${destroot}${prefix}/share/OpenCV/doc
+
+        file mkdir ${doc_output_path}
+        set pdf_files [glob ${sharedsrcpath}/doc/*.pdf]
+        foreach pdf_file ${pdf_files} {
+            file copy -force ${pdf_file} ${doc_output_path}
+        }
+
+        file copy ${sharedsrcpath}/doc/_html ${doc_output_path}/html
+    }
+}
+
+variant also_static description {Also produce static versions of the OpenCV library} {
+    configure.args-delete -DOPENCV_BUILT_SHARED_AND_STATIC=OFF
+    configure.args-append -DOPENCV_BUILT_SHARED_AND_STATIC=ON
 }
 
 variant dc1394 description {Use libdc1394 for FireWire camera. Breaks compatibility with Apple iSight FireWire camera.} {
@@ -107,42 +322,59 @@
 
 variant qt4 description {Use experimental qt4 backend for graphical interface.} {
     depends_lib-append          port:qt4-mac
-    configure.args-append       -DWITH_QT=ON
+}
+
+variant qt4_in_static description {Use the qt4 backend in the static build. Introduces an implicit dependency on QT4 dynamic libaries} {
+    depends_lib-append          port:qt4-mac
 }
 
 variant python26 conflicts python27 description {Add Python 2.6 bindings} {
-    depends_lib-append          port:python26
-    configure.args-delete       -DBUILD_NEW_PYTHON_SUPPORT=OFF
-    configure.args-append       -DINSTALL_PYTHON_EXAMPLES=ON \
-                                -DBUILD_NEW_PYTHON_SUPPORT=ON \
-                                -DPYTHON_EXECUTABLE=${prefix}/bin/python2.6 \
-                                -DPYTHON_LIBRARY=${prefix}/lib/libpython2.6.dylib \
-                                -DPYTHON_INCLUDE_DIR=${frameworks_dir}/Python.framework/Versions/2.6/Headers
-    post-patch {
-        reinplace "s|@@PYTHON_PKGD@@|${frameworks_dir}/Python.framework/Versions/2.6/lib/python2.6/site-packages|g" ${worksrcpath}/CMakeLists.txt
-    }
+    depends_lib-append          port:python26 port:py26-numpy
+    configure.env-append        PYTHON=${prefix}/bin/python2.6
 }
 
 variant python27 conflicts python26 description {Add Python 2.7 bindings} {
-    depends_lib-append          port:python27
-    configure.args-delete       -DBUILD_NEW_PYTHON_SUPPORT=OFF
-    configure.args-append       -DINSTALL_PYTHON_EXAMPLES=ON \
-                                -DBUILD_NEW_PYTHON_SUPPORT=ON \
-                                -DPYTHON_EXECUTABLE=${prefix}/bin/python2.7 \
-                                -DPYTHON_LIBRARY=${prefix}/lib/libpython2.7.dylib \
-                                -DPYTHON_INCLUDE_DIR=${frameworks_dir}/Python.framework/Versions/2.7/Headers
-    post-patch {
-        reinplace "s|@@PYTHON_PKGD@@|${frameworks_dir}/Python.framework/Versions/2.7/lib/python2.7/site-packages|g" ${worksrcpath}/CMakeLists.txt
-    }
+    depends_lib-append          port:python27 port:py27-numpy
+    configure.env-append        PYTHON=${prefix}/bin/python2.7
 }
 
-variant tbb description {Use Intel TBB} {
+variant tbb description {Use Intel Thread Building Blocks} {
     depends_lib-append          port:tbb
-    configure.args-append       -DWITH_TBB=ON \
-                                -DHAVE_TBB=ON \
-                                -DTBB_INCLUDE_DIRS=${prefix}/include \
-                                -DTBB_LIBRARY_DIRS=${prefix}/lib \
-                                -DOPENCV_LINKER_LIBS="-ltbb -ltbbmalloc"
+}
+
+variant tbb_in_static description {Use Intel Thread Building Block in the static build (a dynamic lib dependency)} {
+    depends_lib-append          port:tbb
+}
+
+variant full_docs description {Build full OpenCV documentation} {
+    depends_lib-append          port:texlive-basic \
+                                port:texlive-generic-recommended \
+                                port:texlive-generic-extra \
+                                port:texlive-latex-recommended \
+                                port:texlive-latex-extra \
+                                port:texlive-fonts-recommended \
+                                port:texlive-fonts-extra
+
+    if {[variant_isset python26]} {
+        depends_lib-append port:py26-sphinx
+    } elseif {[variant_isset python27]} {
+        depends_lib-append port:py27-sphinx
+    } else {
+        depends_lib-append port:py-sphinx
+    }
+}
+
+variant eigen description {Include the Eigen2/3 C++ linear algebra library support} {
+    depends_lib-append          port:eigen port:eigen3
+    configure.args-delete       -DWITH_EIGEN=OFF
+    configure.args-append       -DWITH_EIGEN=ON
+}
+
+variant ffmpeg description {Include video support through the FFmpeg library} {
+    depends_lib-append          port:ffmpeg \
+                                port:orc
+    configure.args-delete       -DWITH_FFMPEG=OFF
+    configure.args-append       -DWITH_FFMPEG=ON
 }
 
 livecheck.type                  sourceforge
