#!/bin/sh
set -x 
CWD=`pwd`
TMP=${TMP:-/tmp}
PKG=$TMP/package-firefox-ce

VERSION=2.0.0.14
ARCH=${ARCH:-powerpc}
BUILD=${BUILD:-1}

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

cd $TMP
rm -rf mozilla
tar jxvf $CWD/firefox-$VERSION-source.tar.bz2

cd mozilla
chown -R root.root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 640 -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 {} \;
find . -type d -exec chmod 755 {} \;

# Rebrand to Slackintosh Firefox - mb
zcat $CWD/rebranding.patch.gz | patch -p1 || exit 1
# Fixup cxxflags
cat  $CWD/cxxflags.diff       | patch -p0 || exit 1


ac_cv_visibility_pragma=no \
MOZ_PHOENIX=1 \
./configure --prefix=/usr \
	--with-default-mozilla-five-home=/usr/lib/firefox-$VERSION \
	--with-user-appdir=.firefox \
	--with-system-zlib \
	--with-system-png \
        --enable-system-cairo \
	--enable-application=browser \
	--enable-default-toolkit=gtk2 \
	--enable-crypto \
	--enable-xft \
	--enable-xinerama \
	--enable-optimize \
	--enable-reorder \
	--enable-strip \
	--enable-cpp-rtti \
	--enable-single-profile \
	--disable-freetype2 \
	--disable-accessibility \
	--disable-debug \
	--disable-tests \
	--disable-logging \
	--disable-pedantic \
	--disable-installer \
	--disable-mailnews \
	--disable-ldap \
	--disable-composer \
	--disable-profilesharing \
        $ARCH-slackware-linux
        
MOZ_PHOENIX=1 make -j 4
echo "CHECK"
MOZ_PHOENIX=1 DESTDIR=$PKG make install
echo "INST"

( 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/bin
  zcat $CWD/firefox.moz_plugin_path.diff.gz | patch -p1 --verbose || exit 1
  if [ ! $? = 0 ]; then
    exit 1
  fi
  rm -f firefox~
  chown -R root:root .
)

# /usr/lib/firefox link
( cd $PKG/usr/lib
  ln -sf firefox-$VERSION firefox
)

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

# 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
)

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

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

