#!/bin/bash

# mozilla-firefox.SlackBuild
# by Stuart Winter <stuart@armedslack.org>
# Based on mozilla.SlackBuild from Slackware-10.0.
# 08-March-2005

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Major version number - needed for /usr/lib/firefox dir:
#MAJORVER=1.5
# In 1.5.0.2 this /usr/lib dir is $VERSION
MAJORVER=$VERSION

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
            # -O2 causes segfaults when firefox starts :-( 
            # (so for 1.5 I used -O -- with 2.0 -O2 works fine).
   arm)     export ARCH_OPTIMZ="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export ARCH_OPTIMZ="-O" ;;
   sparc)   export ARCH_OPTIMZ="-O2" ;;
   *)       export ARCH_OPTIMZ="-O2" ;;
esac

# Extract source:
tar jxvvf $PORTCWD/sources/firefox*.tar.bz2
cd mozilla
slackhousekeeping

# Apply patches:
#bzcat $PORTCWD/sources/firefox-1.0-gcc4-compile.patch | patch -p0 --verbose || failpatch
# Fix ARM build bug as listed here:
# http://forums.mozillazine.org/viewtopic.php?t=271308&
# http://www.archivum.info/linux.debian.bugs.dist/2005-09/msg02539.html 
patch --verbose -p0 < $PORTCWD/sources/undefinedref.patch || failpatch

# Clean up:
make distclean

# Configure:
export MOZ_PHOENIX=1
export MOZ_FIREBIRD=1
export MOZ_FIREFOX=1
export BUILD_OFFICIAL=1
export MOZILLA_OFFICIAL=1
./configure \
   --prefix=/usr \
   --enable-application=browser \
   --enable-default-toolkit=gtk2 \
   --with-default-mozilla-five-home=/usr/lib/firefox-$MAJORVER \
   --with-user-appdir=.mozilla \
   --with-system-mng=/usr \
   --with-system-png=/usr \
   --with-system-jpeg=/usr \
   --disable-short-wchar \
   --disable-mailnews \
   --disable-composer \
   --disable-ldap \
   --enable-postscript \
   --disable-installer \
   --disable-updater \
   --disable-freetype2 \
   --disable-xprint \
   --enable-crypto \
   --enable-strip-libs \
   --disable-svg \
   --enable-mathml \
   --disable-tests \
   --disable-gtktest \
   --disable-debug \
   --enable-xft \
   --enable-optimize="-pipe -w $ARCH_OPTIMZ" \
   --with-system-zlib=/usr \
   --with-gssapi=/usr \
   --without-system-nspr \
   --enable-extensions=default \
   --enable-xinerama \
   --disable-pedantic \
   --disable-long-long-warning \
   --enable-single-profile \
   --disable-profilesharing \
   --disable-gnomevfs || failconfig

#   --enable-extensions=cookie,xml-rpc,xmlextras,pref,transformiix,universalchardet,webservices,inspector,negotiateauth \

# Build:
make || failmake

# Install into package framework:
make install DESTDIR=$PKG || failinstall

# This stuff isn't in the official binary release:
rm -rf $PKG/usr/{share,include}
rm -rf $PKG/usr/lib/pkgconfig

# This stuff is in the Slackware package:
( cd $PKG/usr/lib
  rm -rf firefox 
  ln -fs firefox-$MAJORVER firefox )

# Install icons & desktop icon:
mkdir -p $PKG/usr/lib/mozilla/plugins
mkdir -p $PKG/usr/share/{applications,pixmaps}
install -m644 $CWD/mozilla-firefox.desktop $PKG/usr/share/applications/
install -m644 $CWD/firefox.png $PKG/usr/share/pixmaps

# Remove /usr/local paths:
cd $PKG/usr/bin
zcat $CWD/firefox.moz_plugin_path.diff.gz | patch -p1 --verbose --fuzz=20 --no-backup-if-mismatch || failpatch
# Not needed for firefox 1.07
#zcat $CWD/mozilla-firefox-simple.diff.gz  | patch -p1 --verbose --fuzz=20 || failpatch

# Use Thunderbird as the default external email app:
cd $PKG/usr/lib/firefox-$MAJORVER
zcat $CWD/mozilla-firefox-thunderbird.diff.gz | patch -p1 --verbose --suffix=.orig --fuzz=20 --no-backup-if-mismatch || failpatch
rm -f defaults/pref/firefox.js.orig

# Create the extensions files so that the non-root users don't get 
# a warning when they start the browser:
export LD_LIBRARY_PATH="$PWD" 
export MOZILLA_FIVE_HOME="$PWD" 
./regxpcom 
./regchrome 
touch `find . -name *.rdf` 

# These files/directories are usually created if Firefox is run as root, which on many
# systems might (and possibly should) be never.  Therefore, if we don't see them we'll
# put stubs in place to prevent startup errors.
( cd $PKG/usr/lib/firefox
  if [ -d extensions/talkback\@mozilla.org ]; then
    if [ ! -r extensions/talkback\@mozilla.org/chrome.manifest ]; then
      echo > extensions/talkback\@mozilla.org/chrome.manifest
    fi
  fi
  if [ ! -d updates ]; then
    mkdir -p updates/0
  fi )

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
