#!/bin/sh

# Firefox 3 PHBuild, written by Andrew Brouwers for slamd64.

# XXX XXX XXX XXX
# IMPORTANT!
# If you use this script yourself, you *MUST* remove --enable-official-branding
# from mozconfig, unless you have permission from the mozilla foundation.
# XXX XXX XXX XXX

PKGNAM=mozilla-firefox
VERSION=${VERSION:-3.0.10}
BUILD=${BUILD:-1}

. /etc/pkghelpers
pkghelpers_env

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
rm -rf $PKG
mkdir -p $PKG/usr/lib64

cd $TMP

rm -rf mozilla
tar xvf $CWD/firefox-$VERSION-source.tar.bz2

cd mozilla

# Fix permissions
pkghelpers_permissions

# Fix a long standing bug that's prevented staying current on GTK+.
# Thanks to the BLFS folks.  :-)
cat >> layout/build/Makefile.in << EOF

ifdef MOZ_ENABLE_CANVAS
EXTRA_DSO_LDOPTS += \$(XLDFLAGS) -lX11 -lXrender
endif

EOF

export BUILD_OFFICIAL=1
export MOZILLA_OFFICIAL=1
export MOZILLA_FIVE_HOME=/usr/lib64/firefox-$VERSION

sed -e "s/#CFLAGS#/$SLKCFLAGS/" $CWD/mozconfig > .mozconfig
./configure \
	--with-distribution-id=com.slamd64 \
	--build=$ARCH-pc-linux \
	--target=$ARCH-pc-linux \
	--host=$ARCH-pc-linux

make -j5 || exit 1
make install DESTDIR=$PKG

# Apply patches
( cd $PKG/usr/lib64/firefox-$VERSION
  zcat $CWD/mozilla-firefox-mimeTypes-fix.diff.gz | patch -p1 --verbose || exit 1
  if [ ! $? = 0 ]; then
    exit 1
  fi

  cd $PKG/usr/lib64/firefox-$VERSION
  zcat $CWD/firefox.moz_plugin_path.diff.gz | patch -p0 --verbose || exit 1
  if [ ! $? = 0 ]; then
    exit 1
  fi
)

( cd $PKG/usr/bin
  chown -R root:root .
)

rm -rf $PKG/usr/share/idl $PKG/usr/include

mkdir -p $PKG/usr/lib64/mozilla/plugins
mkdir -p $PKG/usr/share/applications
cat $CWD/mozilla-firefox.desktop > $PKG/usr/share/applications/mozilla-firefox.desktop
mkdir -p $PKG/usr/share/pixmaps
cat $CWD/firefox.png > $PKG/usr/share/pixmaps/firefox.png

# 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/lib64/firefox-$VERSION
  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

# Icons
mkdir -p chrome/icons/default
cp icons/mozicon50.xpm chrome/icons/default/main-window.xpm
)

rm -rf $PKG/usr/lib64/firefox-devel-$VERSION

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
pkghelpers_fixup
pkghelpers_makepkg