#!/bin/sh
CWD=`pwd`
TMP=${TMP:-/tmp/ff}
PKG=$TMP/package-mozilla-firefox

VERSION=${VERSION:-3.0.19}
ARCH=${ARCH:-x86_64}
BUILD=${BUILD:-1}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

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

cd $TMP

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

cd mozilla
chown -R root.root .

# Make sure the perms/ownerships are sane:
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

# 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

echo "ac_cv_visibility_pragma=no" > .mozconfig

export MOZILLA_OFFICIAL="1" && 
export BUILD_OFFICIAL="1" && 
export MOZ_PHOENIX="1" && 
./configure --prefix=/usr \
	--enable-application=browser \
	--enable-optimize=-O2 \
	--disable-debug \
	--disable-tests \
	--enable-official-branding \
	--with-default-mozilla-five-home=/usr/lib/firefox-$VERSION \
	--with-user-appdir=.firefox \
	--libdir=/usr/lib \
	--enable-static \
	--disable-shared \
	--disable-libxul \
	--enable-default-toolkit=cairo-gtk2 \
	--enable-64bit \
	--enable-xft \
	--with-distribution-id=bluewhite64.com \
	--build=$ARCH-pc-linux \
	--target=$ARCH-pc-linux \
	--host=$ARCH-pc-linux


make -j 6 || exit 1
make install DESTDIR=$PKG

( cd $PKG/usr/lib/firefox-$VERSION
  zcat $CWD/mozilla-firefox-thunderbird.diff.gz | patch -p1 --verbose || exit 1
  if [ ! $? = 0 ]; then
    exit 1
  fi

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

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

( cd $PKG/usr/bin
  chown -R root:root .
)
mkdir -p $PKG/usr/lib/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/lib/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
# We make sure that the default icons are in the right place ;)
mkdir -p chrome/icons/default
#cp icons/default.xpm chrome/icons/default/default.xpm
cp icons/mozicon50.xpm chrome/icons/default/main-window.xpm
)

( cd $PKG/usr/lib
  ln -s firefox-$VERSION firefox
 )

( cd $PKG/usr/include 
  ln -s firefox-$VERSION firefox
 )

( cd $PKG/usr/include/firefox-$VERSION
  ln -s stable nspr 
 )

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

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

cd $PKG
makepkg -l y -c n $TMP/mozilla-firefox-$VERSION-$ARCH-$BUILD.tgz

