#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-mozilla
VERSION=1.7.5
ARCH=alpha
BUILD=2
TARGET=$ARCH-alphaslack-linux

rm -rf $PKG
mkdir -p $PKG/usr
( cd $TMP
rm -rf mozilla
tar xjf $CWD/mozilla-source-$VERSION.tar.bz2
cd mozilla
chown -R root.root .
zcat $CWD/mozilla-alpha-xpcom-subs-fix.patch.gz | patch -p1 --verbose
zcat $CWD/_linux.h.patch.gz | patch -p0 --verbose
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 \
./configure --prefix=/usr \
  --enable-optimize="-O3" \
  --disable-debug \
  --with-default-mozilla-five-home=/usr/lib/mozilla-$VERSION \
  --enable-strip \
  --enable-strip-libs \
  --disable-tests \
  --disable-short-wchar \
  --enable-nspr-autoconf \
  --disable-calendar \
  --disable-chatzilla \
  --without-gnome-ui \
  --without-gnome-vfs \
  --disable-gnome \
  --enable-extensions=default \
  --enable-crypto \
  --disable-xprint \
  --without-system-nspr \
  --with-system-zlib \
  --enable-toolkit-gtk2 \
  --enable-default-toolkit=gtk2 \
  --disable-toolkit-qt \
  --disable-toolkit-xlib \
  --disable-toolkit-gtk \
  --disable-svg \
  --disable-ldap \
  --enable-xft \
  --disable-freetype2 \
  --enable-ipv6 \
  --enable-xinerama \
  --with-pthreads \
  $TARGET

BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 make export
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 make 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/usr/man
  find . -name "*.?" | xargs gzip -9
)
( cd $PKG
  find . -name chatzilla* | xargs rm -rf --verbose
)
( cd $PKG/usr/lib/mozilla-${VERSION}/plugins
  chmod 0400 *.so
)
mkdir -p $PKG/usr/share/gnome/apps/Internet
cp -a $CWD/*.desktop $PKG/usr/share/gnome/apps/Internet
chown -R root.root $PKG/usr/share/gnome/apps/Internet
chmod 644 $PKG/usr/share/gnome/apps/Internet/*
mkdir -p $PKG/usr/share/pixmaps
cp -a $CWD/*.gif $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
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded
  find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded
)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $PKG
makepkg -l y -c n $TMP/mozilla-$VERSION-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/mozilla.build.log
