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

rm -rf $PKG
mkdir -p $PKG/usr
( cd $TMP
rm -rf mozilla
tar xjf $CWD/mozilla-$VERSION-source.tar.bz2
cd mozilla
chown -R root.root .
zcat $CWD/mozilla-alpha-xpcom-subs-fix.patch.gz | patch -p1 --verbose || exit
zcat $CWD/_linux.h.patch.gz | patch -p0 --verbose || exit
zcat $CWD/mozilla-1.7.13.p3p.makefile.in.diff.gz | patch -p1 --verbose || exit
zcat $CWD/nsHttpConnectionMgr.cpp.diff.gz | patch -p0 --verbose || exit
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 \
  --without-gnome-vfs \
  --disable-gnome \
  --enable-extensions=default,irc \
  --enable-crypto \
  --enable-calendar \
  --without-system-nspr \
  --enable-toolkit-gtk \
  --enable-default-toolkit=gtk \
  --disable-xprint \
  --enable-xft \
  --disable-freetype2 \
  --enable-ipv6 \
  --enable-xinerama \
  --with-system-zlib \
  $TARGET || exit
#  --disable-ldap \

BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 make -s export || exit
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 make -s libs || exit
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 DESTDIR=$PKG make install || exit
# 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/*
# plugins 
mkdir -p $PKG/usr/lib/mozilla
( cd $PKG/usr/lib/mozilla-$VERSION
    mv plugins ../mozilla
    ln -sf ../mozilla/plugins plugins
)
chmod 400 $PKG/usr/lib/mozilla/plugins/*
# 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
)
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
