# -*- coding: utf-8; mode: tcl; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; truncate-lines: t -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4
# $Id: Portfile 64763 2010-03-15 16:31:33Z nox@macports.org $

PortSystem          1.0

name                qt4-x11
version             4.4.3
revision            5
categories	        x11
maintainers         nomaintainer
homepage            http://qt.nokia.com/
platforms           darwin
description         Qt Tool Kit
long_description    This is Qt, TrollTech's C++ toolkit for writing cross-platform GUI applications.
master_sites        ftp://ftp.trolltech.com/qt/source/ \
                    http://ftp.iasi.roedu.net/mirrors/ftp.trolltech.com/qt/source/ \
                    http://ftp.ntua.gr/pub/X11/Qt/qt/source/ \
                    http://wftp.tu-chemnitz.de/pub/Qt/qt/source/
distname            qt-x11-opensource-src-${version}

checksums           \
    md5    376c003317c4417326ba2116370227d0          \
    sha1   a883e913e69a418e3cfcfecefe4f82c4e091c4bd  \
    rmd160 49de53663a86785f88236bdeca6db8ee96a37db8

depends_build       port:pkgconfig

depends_lib                                \
    port:zlib                              \
    path:include/gif_lib.h:giflib          \
    port:tiff                              \
    port:libpng                            \
    port:libmng                            \
    port:jpeg                              \
    port:libiconv                          \
    path:lib/pkgconfig/glib-2.0.pc:glib2   \
    port:dbus                              \
    port:unixODBC                          \
    path:bin/mysql_config5:mysql5          \
    port:postgresql83                      \
    port:openssl                           \
    port:fontconfig                        \
    port:gettext                           \
    port:xrender                           \
    port:xorg-libsm                        \
    port:xorg-libXi                        \
    port:xorg-libXrandr                    \
    port:mesa                              \
    port:gstreamer                         \
    port:gst-plugins-base                  \
    port:sqlite3

patchfiles  patch-compile.test.diff

# --prefix is not recognized
configure.pre_args-delete  --prefix=${prefix}
# -I${prefix}/include and -L${prefix}/lib must be set in ${configure.args}
#    or else some generated Makefiles will not seem them
configure.cppflags
configure.ldflags

configure.env-append \
    OPENSOURCE_CXXFLAGS=-D__USE_WS_X11__

set qt_dir ${prefix}/libexec/${name}

# -I${prefix}/include should be set in ${configure.args}, but
#    we instead patch -isystem ${prefix}/include into the configure
#    script to avoid conflicts with other ports (e.g. iconv).
# See http://trac.macports.org/ticket/16862
#
# -dbus-linked prevends qt4 from trying to dynamically load libdbus-1,
#     which it is not able to find in ${prefix}
# -openssl-linked ensures that the MacPorts openssl is used
configure.args                                                 \
    -v                                                         \
    -confirm-license                                           \
    -prefix          ${qt_dir}                                 \
    -docdir          ${prefix}/share/doc/${name}               \
    -examplesdir     ${prefix}/share/${name}/examples          \
    -demosdir        ${prefix}/share/${name}/demos             \
    -system-sqlite                                             \
    -openssl-linked                                            \
    -dbus-linked                                               \
    -I${prefix}/include/mysql5/mysql                           \
    -I${prefix}/include/postgresql83                           \
    -L${prefix}/lib                                            \
    -L${prefix}/lib/mysql5/mysql                               \
    -L${prefix}/lib/postgresql83                               \
    -liconv                                                    \
    -lresolv                                                   \
    -I${prefix}/include/gstreamer-0.10                         \
    -I${prefix}/include/glib-2.0                               \
    -I${prefix}/lib/glib-2.0/include                           \
    -I${prefix}/include/libxml2

# default: -no-nas-sound
# default: -nomake demos  -nomake examples
# default: -iconv -svg -webkit -phonon -phonon-backend -xmlpatterns
# default: -no-assistant-webkit

universal_variant   no

build.target        first
use_parallel_build  yes

destroot.destdir    INSTALL_ROOT="${destroot}"

post-patch {
    reinplace -E "s|^I_FLAGS=\$|I_FLAGS=-isystem${prefix}/include|" \
        ${worksrcpath}/configure

    # macosx seems to be a special architecture to accommodate universal builds, but here is no
    #    ${worksrcpath}/include/QtCore/qatomic_macosx.h file, which causes an error
    reinplace "s|CFG_HOST_ARCH=macosx|CFG_HOST_ARCH=`uname -p`|g" ${worksrcpath}/configure

    # Ensure the correct MacPorts X11 is used
    reinplace "s|/usr/X11R6|${prefix}|g" ${worksrcpath}/mkspecs/darwin-g++/qmake.conf

    # Avoid having to call "install_name_tool -change" after destroot.
    reinplace \
        "s|install_name\$\${LITERAL_WHITESPACE}|install_name\$\${LITERAL_WHITESPACE}\$\$\[QT_INSTALL_LIBS\]/|g" \
        ${worksrcpath}/mkspecs/darwin-g++/qmake.conf

    # ensure that MacPorts compilers are used
    reinplace "s| cc\$| ${configure.cc}|"  ${worksrcpath}/mkspecs/darwin-g++/qmake.conf
    reinplace "s| c++\$| ${configure.cxx}|" ${worksrcpath}/mkspecs/darwin-g++/qmake.conf

    # Q_OS_MAC, Q_OS_MACX, and Q_OS_DARWIN is set for all Mac systems.
    # Q_WS_MAC is NOT set for the X11 version of QT.
    # It is not clear why so many of these had to be changed.
    #
    # Excluded:
    #        ${worksrcpath}/src/corelib/global/qglobal.h
    #        ${worksrcpath}/src/network/kernel/qhostinfo_unix.cpp
    foreach file {
        qmake/generators/mac/pbuilder_pbx.cpp
        src/3rdparty/webkit/WebKit/qt/Api/qwebpage.cpp
        src/corelib/global/qglobal.cpp
        src/corelib/io/qfile.cpp
        src/corelib/io/qfsfileengine_unix.cpp
        src/corelib/plugin/qlibrary.cpp
        src/corelib/thread/qthread_unix.cpp
        src/corelib/tools/qlocale.cpp
        src/network/ssl/qsslsocket_openssl_symbols.cpp
        tools/porting/src/qt3headers1.resource
        tools/qvfb/qlock.cpp
    } {
        reinplace "s|Q_OS_DARWIN|Q_WS_MAC|g" ${worksrcpath}/${file}
    }
    # Excluded:
    #        ${worksrcpath}/src/corelib/global/qglobal.h
    foreach file {
        src/corelib/tools/qdumper.cpp
        src/qt3support/other/q3accel.cpp
        src/qt3support/other/q3process_unix.cpp
        tools/porting/src/qt3headers1.resource
        tools/porting/src/qt3headers3.resource
    } {
        reinplace "s|Q_OS_MACX|Q_WS_MAC|g" ${worksrcpath}/${file}
    }
    # Edited from command:
    # grep -rl "\(Q_OS_MAC\$\|Q_OS_MAC[^X]\)" * | grep -v \.resource\$ | grep -v ^doc/ | grep -v src/corelib/global/qglobal.h
    # Excluded:
    #        ${worksrcpath}/src/corelib/global/qglobal.h
    #        ${worksrcpath}/tools/assistant/lib/fulltextsearch/qclucene-config_p.h
    #        ${worksrcpath}/src/script/qscriptengine_p.cpp
    #        ${worksrcpath}/src/corelib/io/qprocess.cpp
    #        ${worksrcpath}/src/corelib/io/qfilesystemwatcher.cpp
    #        ${worksrcpath}/src/corelib/concurrent/qtconcurrentiteratekernel.cpp
    foreach file {
        demos/mediaplayer/mediaplayer.cpp
        demos/qtdemo/colors.cpp
        demos/qtdemo/menumanager.cpp
        examples/dialogs/standarddialogs/dialog.cpp
        examples/help/remotecontrol/remotecontrol.cpp
        examples/help/simpletextviewer/assistant.cpp
        examples/tools/echoplugin/echowindow/echowindow.cpp
        examples/tools/plugandpaint/mainwindow.cpp
        qmake/main.cpp
        qmake/option.cpp
        src/corelib/codecs/qiconvcodec.cpp
        src/corelib/codecs/qiconvcodec_p.h
        src/corelib/global/qglobal.cpp
        src/corelib/global/qlibraryinfo.cpp
        src/corelib/global/qnamespace.h
        src/corelib/io/qdir.cpp
        src/corelib/io/qfsfileengine_unix.cpp
        src/corelib/io/qprocess_unix.cpp
        src/corelib/io/qsettings.cpp
        src/corelib/io/qsettings_p.h
        src/corelib/kernel/qcoreapplication.cpp
        src/corelib/kernel/qcoreapplication_p.h
        src/corelib/plugin/qlibrary.cpp
        src/corelib/plugin/qlibrary_unix.cpp
        src/corelib/thread/qthread_unix.cpp
        src/corelib/thread/qthread.cpp
        src/corelib/tools/qlocale.cpp
        src/corelib/tools/qpoint.h
        src/corelib/tools/qrect.h
        src/corelib/tools/qstring.cpp
        src/corelib/xml/qxmlstream.h
        src/gui/dialogs/qfilesystemmodel.cpp
        src/gui/dialogs/qprintdialog.h
        src/gui/itemviews/qdirmodel.cpp
        src/gui/itemviews/qfileiconprovider.cpp
        src/gui/kernel/qapplication.h
        src/gui/kernel/qapplication_p.h
        src/gui/text/qfont.cpp
        src/gui/text/qfontdatabase.cpp
        src/gui/widgets/qdockwidget.cpp
        src/plugins/accessible/widgets/simplewidgets.cpp
        src/qt3support/other/q3polygonscanner.cpp
        src/qt3support/text/q3textedit.cpp
        src/sql/drivers/odbc/qsql_odbc.h
        src/tools/uic/cpp/cppwriteinitialization.cpp
        src/tools/uic/cpp/cppwriteinitialization.h
        tools/assistant/compat/lib/qassistantclient.cpp
        tools/assistant/lib/qhelpsearchresultwidget.cpp
        tools/assistant/tools/assistant/bookmarkmanager.cpp
        tools/assistant/tools/assistant/centralwidget.cpp
        tools/assistant/tools/assistant/indexwindow.cpp
        tools/assistant/tools/assistant/mainwindow.cpp
        tools/designer/src/designer/assistantclient.cpp
        tools/designer/src/designer/qdesigner_actions.cpp
        tools/designer/src/lib/uilib/abstractformbuilder.cpp
        tools/linguist/linguist/trwindow.cpp
        tools/linguist/shared/proparserutils.h
        tools/shared/findwidget/findwidget.cpp
    } {
        reinplace "s|Q_OS_MAC|Q_WS_MAC|g" ${worksrcpath}/${file}
    }
}

post-destroot {
    foreach doc {INSTALL LICENSE.GPL2 LICENSE.GPL3 OPENSOURCE-NOTICE.TXT README \
                     GPL_EXCEPTION_ADDENDUM.TXT GPL_EXCEPTION.TXT} {
        xinstall -c -m 644 ${worksrcpath}/$doc ${destroot}${prefix}/share/doc/${name}
    }

    # create link in ${prefix}/bin to executable files in ${qt_dir}/bin
    foreach bin [glob -type f -directory ${destroot}${qt_dir}/bin *] {
        system "ln -s ${qt_dir}/bin/[file tail ${bin}] ${destroot}${prefix}/bin"
    }
    # append -x11 to links in ${prefix}/bin to avoid conflict with qt4-mac
    foreach bin [glob ${destroot}${prefix}/bin/*] {
        file rename ${bin} ${bin}-x11
    }
    # qtconfig is not installed by qt4-mac
    file rename ${destroot}${prefix}/bin/qtconfig-x11 ${destroot}${prefix}/bin/qtconfig

    # Fix the .pc and .prl files by removing ${destroot}
    foreach fixfile [glob -nocomplain -directory ${destroot} ${qt_dir}/lib/pkgconfig/*.pc ${qt_dir}/lib/*.prl ${prefix}/share/${name}/demos/shared/*.prl] {
        reinplace  \
            "s|-L${worksrcpath}/lib|-L${qt_dir}/lib|g" \
            ${fixfile}
    }

    # install select file for qt4_select
    xinstall -m 755 -d ${destroot}${prefix}/etc/select/qt4
    xinstall -m 644 ${filespath}/${name} ${destroot}${prefix}/etc/select/qt4/
}

variant webkit description {Use WebKit as html rendering engine in Assistant} {
    configure.args-append   -assistant-webkit
}

livecheck.type      regex
livecheck.url       http://trolltech.com/downloads/opensource/appdev/linux-x11-cpp
livecheck.regex     "qt-x11-opensource-src-(4(?:\\.\\d+)*)${extract.suffix}"
