#!/bin/sh
CWD=`pwd`
PKG=/tmp/package-mozilla
rm -rf $PKG
mkdir -p $PKG/usr

VERSION=1.7.13
ARCH=${ARCH:-i486}
BUILD=1

cd /tmp
rm -rf mozilla
tar xjvf $CWD/mozilla-${VERSION}-source.tar.bz2
cd mozilla

# Add /usr/lib/mozilla/plugins to MOZ_PLUGINS_PATH:
zcat $CWD/mozilla.moz_plugin_path.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit
rm -f xpfe/bootstrap/mozilla.in.orig

# There seems to be a little problem with the p3p extension Makefile.in.
zcat $CWD/mozilla-1.7.13.p3p.makefile.in.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1

chown -R root.root .
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 \
./configure --prefix=/usr \
  --enable-optimize=-O2 \
  --disable-debug \
  --with-default-mozilla-five-home=/usr/lib/mozilla-${VERSION} \
  --enable-strip \
  --disable-tests \
  --disable-short-wchar \
  --enable-nspr-autoconf \
  --enable-calendar \
  --enable-extensions=default,irc \
  --enable-crypto \
  --disable-xprint \
  --without-system-nspr \
  --with-system-zlib \
  --enable-default-toolkit=gtk2 \
  --disable-freetype2 \
  --enable-xft

BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 make -j3 -s export
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 make -j3 -s libs
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 DESTDIR=$PKG make install

# Install nss headers.
mkdir -p $PKG/usr/include/mozilla-${VERSION}/nss
find security/nss/lib -name "*.h" -type f -exec cp -a {} $PKG/usr/include/mozilla-${VERSION}/nss \;
chown -R root.root $PKG/usr/include/mozilla-${VERSION}/nss
chmod 644 $PKG/usr/include/mozilla-${VERSION}/nss/*

# Move nss libraries into /usr/lib.  Unless this is done, some things (like gaim's
# plugin for MSN) will be unable to load them, even if they linked with them
# successfully.  AFAIKT, these other things are at fault, but since this little
# trick fixes the problem we'll do it.  BTW, Red Hat started this lib moving mess
# for unknown reasons (didn't know how to link properly?), so that's why other 
# programs now expect to find things arranged other than a standard "make install" layout...
( cd $PKG/usr/lib/mozilla-${VERSION} && ( for nsslib in libnspr4.so libnss3.so libplc4.so libplds4.so libsmime3.so libsoftokn3.so libssl3.so ; do
    # Move library:
    mv $nsslib ..
    # Link to it... you don't just go messing with stuff without leaving it "clean"
    ln -sf ../$nsslib .
  done )
)

chown -R root.bin $PKG/usr/bin
( cd $PKG
  find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded
)
( cd $PKG/usr/man
  find . -name "*.?" | xargs gzip -9
)
mkdir -p $PKG/usr/lib/mozilla/plugins
mkdir -p $PKG/usr/share/applications
cp -a $CWD/*.desktop $PKG/usr/share/applications
chown -R root.root $PKG/usr/share/applications
chmod 644 $PKG/usr/share/applications/*
mkdir -p $PKG/usr/share/pixmaps
cp -a $CWD/*.png $PKG/usr/share/pixmaps
chown -R root.root $PKG/usr/share/pixmaps
chmod 644 $PKG/usr/share/pixmaps/*

mkdir -p $PKG/usr/doc/mozilla-$VERSION
cp -a \
  LEGAL LICENSE README.txt \
  $PKG/usr/doc/mozilla-$VERSION
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
# This is no longer needed, but we'll keep the example around in case we ever
# have to change toolkits within a Mozilla release again in the future.  It was
# a tricky one to figure out, so we'll leave the cruft.
#zcat $CWD/moz-1.4-kludge-doinst.sh.gz > $PKG/install/doinst.sh

cd /tmp/package-mozilla
makepkg -l y -c n ../mozilla-$VERSION-$ARCH-$BUILD.tgz