# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 91579 2012-04-06 04:54:55Z jeremyhu@macports.org $

PortSystem      1.0
PortGroup       archcheck 1.0

name            firefox-x11
set component   firefox
categories      www x11
version         7.0.1
revision        2
platforms       darwin
maintainers     nomaintainer
description     Mozilla.org's popular stand-alone web browser

long_description \
    Firefox empowers you to browse faster, more safely and more \
    efficiently than with any other browser.

universal_variant no
supported_archs i386 x86_64

homepage        http://www.mozilla.com/${component}/

master_sites    http://releases.mozilla.org/pub/mozilla.org/${component}/releases/${version}/source/
distname        ${component}-${version}.source
dist_subdir     ${component}

use_bzip2       yes

checksums           sha1    94bbc7152832371dc0be82f411730df043c5c6ac \
                    rmd160  cabd83f829ba54711f132749282d7d49e33b6334 \
                    sha256  81ecf63fca67186094d49dd5a6fe7f5dbd143a39f872ad126de38f246334fc04

worksrcdir      mozilla-release

depends_build \
	port:findutils \
	port:pkgconfig \
	port:autoconf213 \
	port:yasm

depends_lib \
	port:heimdal \
	port:gconf \
	port:esound \
	port:libcanberra \
	port:findutils \
	port:gtk2 \
	port:mesa \
	port:xorg-libXt \
	port:libnotify

archcheck.files \
	libexec/heimdal/lib/libkrb5.dylib \
	lib/libcanberra.dylib \
	lib/libgconf-2.dylib \
	lib/libgtk-x11-2.0.dylib \
	lib/libnotify.dylib \
	lib/libXt.dylib

use_autoconf yes
autoconf.cmd autoconf213

set pkg_name ${name}

configure.args \
	--with-xulrunner-stub-name=xulrunner-stub \
	--disable-static \
	--enable-shared \
	--disable-official-branding \
	--with-branding=browser/branding/unofficial \
	--with-distribution-id=org.macports \
	--enable-default-toolkit=cairo-gtk2 \
	--x-includes=${prefix}/include \
	--x-libraries=${prefix}/lib \
	--enable-libxul \
	--disable-system-lcms \
	--disable-system-cairo \
	--disable-system-sqlite \
	--with-default-mozilla-five-home=${prefix}/lib/${pkg_name} \
	--enable-oji \
	--enable-plugins \
	--enable-mathml \
	--enable-extensions="default,spellcheck" \
	--enable-permissions \
	--enable-cookie \
	--enable-image-decoders=all \
	--enable-image-encoder=all \
	--enable-canvas \
	--enable-jsd \
	--disable-xpctools \
	--enable-crypto \
	--enable-pango \
	--enable-svg \
	--enable-svg-renderer=cairo \
	--enable-xinerama \
	--with-pthreads \
	--disable-gnomevfs \
	--disable-gnomeui \
	--enable-postscript \
	--enable-safe-browsing \
	--disable-crashreporter \
	--enable-optimize='-O2' \
	--disable-prebinding \
	--enable-strip \
	--enable-install-strip \
	--disable-debug \
	--disable-installer \
	--disable-updater \
	--disable-pedantic \
	--disable-tests \
	--disable-mochitest \
	--disable-necko-wifi

# xpctools and necko-wifi broke in 3.6b1

# XCode 3.1's gcc-4.0 and gcc-4.2 have issues
# llvm-gcc-4.2 has trouble with some of the mmx asm
if {${configure.compiler} == "gcc-4.0"} {
    depends_build-append path:bin/clang:clang
    configure.compiler macports-clang
} elseif {${configure.compiler} == "llvm-gcc-4.2"} {
    configure.compiler clang
}

# Yes, this is ugly... but the real fix is in the works
# https://bugzilla.mozilla.org/show_bug.cgi?id=529878
configure.cc ${configure.cc} -arch ${configure.build_arch}
configure.cxx ${configure.cxx} -arch ${configure.build_arch}

variant debug description {build with debugging symbols} {
    configure.args-delete   --disable-debug \
                            --disable-mochitest \
                            --enable-optimize='-O2' \
                            --enable-strip \
                            --enable-install-strip

    configure.args-append   --enable-debug='-ggdb3' \
                            --enable-mochitest \
                            --disable-optimize \
                            --disable-strip \
                            --disable-install-strip
}

variant gnome description {use gnome libraries where appropriate} {
    depends_lib-append \
	port:gnome-vfs \
	port:libgnomeui \
	port:libgnome \
	port:gnome-icon-theme

    configure.args-delete --disable-gnomevfs \
                          --disable-gnomeui

    configure.args-append --enable-gnomevfs \
                          --enable-gnomeui
}

variant official_branding description {Enables official Firefox branding.  You may not redistribute resulting packages built with this variant.} {
    configure.args-delete \
        --disable-official-branding \
        --with-branding=browser/branding/unofficial
    configure.args-append \
	--enable-official-branding
}

patchfiles patch-dylib_file.diff

post-patch {
    # https://bugzilla.mozilla.org/show_bug.cgi?id=485857
    reinplace "s:XP_MACOSX:__APPLE__:g" ${worksrcpath}/xpcom/base/nsStackWalk.cpp 

    # https://bugzilla.mozilla.org/show_bug.cgi?id=641755
    reinplace "s:XP_MACOSX:__APPLE__:g" ${worksrcpath}/gfx/ycbcr/update.sh
    reinplace "s:XP_MACOSX:__APPLE__:g" ${worksrcpath}/gfx/ycbcr/yuv_row_posix.cpp

    # https://bugzilla.mozilla.org/show_bug.cgi?id=486034
    # find -xtype -> gfind -xtype
    reinplace "s:find:gfind:g" ${worksrcpath}/toolkit/mozapps/installer/packager.mk

    # Don't use the system X11
    reinplace "s:/usr/X11R6:${prefix}:" ${worksrcpath}/dom/plugins/base/nsPluginsDirUnix.cpp
    reinplace "s/libXt.so.6:libXext.so/libXt.dylib:libXext.dylib/" ${worksrcpath}/dom/plugins/base/nsPluginsDirUnix.cpp

    # Set the right install_name on the libs
    reinplace "s:@executable_path:${prefix}/lib/${pkg_name}:g" ${worksrcpath}/config/config.mk
    reinplace "s:@executable_path:${prefix}/lib/${pkg_name}:g" ${worksrcpath}/config/rules.mk
    reinplace "s:@executable_path:${prefix}/lib/${pkg_name}:g" ${worksrcpath}/js/src/config/config.mk
    reinplace "s:@executable_path:${prefix}/lib/${pkg_name}:g" ${worksrcpath}/js/src/config/rules.mk
    reinplace "s:@executable_path:${prefix}/lib/${pkg_name}:g" ${worksrcpath}/nsprpub/configure.in
    reinplace "s:@executable_path:${prefix}/lib/${pkg_name}:g" ${worksrcpath}/nsprpub/configure
    reinplace "s:@executable_path:${prefix}/lib/${pkg_name}:g" ${worksrcpath}/security/coreconf/Darwin.mk
    reinplace "s:@executable_path:${prefix}/lib/${pkg_name}:g" ${worksrcpath}/security/nss/lib/freebl/config.mk

    # Fix the correct library filenames for loading at runtime
    reinplace "s:libatk-1.0.so.0:${prefix}/lib/libatk-1.0.dylib:" ${worksrcpath}/accessible/src/atk/nsApplicationAccessibleWrap.cpp

    reinplace "s:libgssapi_krb5.so.2:${prefix}/libexec/heimdal/lib/libgssapi.2.dylib:" ${worksrcpath}/extensions/auth/nsAuthGSSAPI.cpp

    reinplace "s:libgnomeui-2.so.0:${prefix}/lib/libgnomeui-2.dylib:" ${worksrcpath}/modules/libpr0n/decoders/icon/gtk/nsIconChannel.cpp
    reinplace "s:libgnome-2.so.0:${prefix}/lib/libgnome-2.dylib:" ${worksrcpath}/modules/libpr0n/decoders/icon/gtk/nsIconChannel.cpp
    reinplace "s:libgnomevfs-2.so.0:${prefix}/lib/libgnomevfs-2.dylib:" ${worksrcpath}/modules/libpr0n/decoders/icon/gtk/nsIconChannel.cpp

    reinplace "s:libgconf-2.so.4:${prefix}/lib/libgconf-2.dylib:" ${worksrcpath}/extensions/pref/system-pref/src/gconf/nsSystemPrefService.cpp

    reinplace "s:libcups.so.2:/usr/lib/libcups.2.dylib:" ${worksrcpath}/widget/src/gtk2/nsCUPSShim.cpp

    reinplace "s:libgnomeui-2.so.0:${prefix}/lib/libgnomeui-2.dylib:" ${worksrcpath}/toolkit/xre/nsNativeAppSupportUnix.cpp
    reinplace "s:libgnome-2.so.0:${prefix}/lib/libgnome-2.dylib:" ${worksrcpath}/toolkit/xre/nsNativeAppSupportUnix.cpp

    reinplace "s:libgnomeui-2.so.0:${prefix}/lib/libgnomeui-2.dylib:" ${worksrcpath}/toolkit/crashreporter/client/crashreporter_linux.cpp
    reinplace "s:libgnome-2.so.0:${prefix}/lib/libgnome-2.dylib:" ${worksrcpath}/toolkit/crashreporter/client/crashreporter_linux.cpp

    reinplace "s:libgconf-2.so.4:${prefix}/lib/libgconf-2.dylib:" ${worksrcpath}/toolkit/crashreporter/client/crashreporter_gtk_common.cpp

    reinplace "s:libgtk-x11-2.0.so.0:${prefix}/lib/libgtk-x11-2.0.dylib:" ${worksrcpath}/widget/src/gtk2/nsBidiKeyboard.cpp
    reinplace "s:libXss.so.1:${prefix}/lib/libXss.1.0.0.dylib:" ${worksrcpath}/widget/src/gtk2/nsIdleServiceGTK.cpp
    reinplace "s:libXinerama.so.1:${prefix}/lib/libXinerama.1.dylib:" ${worksrcpath}/widget/src/gtk2/nsScreenManagerGtk.cpp
    reinplace "s:libesd.so.0:${prefix}/lib/libesd.0.dylib:" ${worksrcpath}/widget/src/gtk2/nsSound.cpp
    reinplace "s:libcanberra.so.0:${prefix}/lib/libcanberra.0.dylib:" ${worksrcpath}/widget/src/gtk2/nsSound.cpp

    reinplace "s:libXss.so.1:${prefix}/lib/libXss.1.0.0.dylib:" ${worksrcpath}/widget/src/qt/nsIdleServiceQt.cpp
    reinplace "s:libesd.so.0:${prefix}/lib/libesd.0.dylib:" ${worksrcpath}/widget/src/qt/nsSound.cpp

    # Set our system plugins path
    reinplace "s:/usr/lib/mozilla/plugins:${prefix}/lib/nsplugins:" ${worksrcpath}/xpcom/io/nsAppFileLocationProvider.cpp

    reinplace "s:/etc/gre\.:${prefix}/etc/gre.:g" \
	${worksrcpath}/xpcom/build/nsXPCOMPrivate.h
}

configure.env \
	MOZCONFIG="${worksrcpath}/browser/config/mozconfig" \
	MOZILLA_FIVE_HOME="${prefix}/lib/${pkg_name}" \
	_MACOSX_DEPLOYMENT_TARGET="${macosx_deployment_target}"

build.args-append \
	includedir="${prefix}/include/${pkg_name}" \
	idldir="${prefix}/share/idl/${pkg_name}" \
	installdir="${prefix}/lib/${pkg_name}" \
	sdkdir="${prefix}/lib/${pkg_name}" \
	SKIP_COPY_XULRUNNER=1

destroot.args-append \
	includedir="${prefix}/include/${pkg_name}" \
	idldir="${prefix}/share/idl/${pkg_name}" \
	installdir="${prefix}/lib/${pkg_name}" \
	sdkdir="${prefix}/lib/${pkg_name}" \
	SKIP_COPY_XULRUNNER=1

post-destroot   {
    file mkdir ${destroot}${prefix}/share/applications

    copy \
        ${filespath}/share-applications-${component}.desktop \
        ${destroot}${prefix}/share/applications/${name}.desktop

    move ${destroot}${prefix}/bin/${component} ${destroot}${prefix}/bin/${pkg_name}
}

livecheck.type  regex
livecheck.url   http://releases.mozilla.org/pub/mozilla.org/${component}/releases
livecheck.regex ">(\\d+(?:\\.\\d+)*).<"
