#!/bin/sh

[ -z $PREPKG ] && exit 1

VERSION=1.7.5
FILES=$(cd $(dirname $0) ; pwd)

mkdir -p $PREPKG/{install,usr/{doc/mozilla-$VERSION,include/mozilla-$VERSION/nss,share/{gnome/apps/Internet,pixmaps}}}/

chown -R root:root .
export BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1
./configure --prefix=/usr \
  --disable-debug \
  --disable-freetype2 \
  --disable-tests \
  --disable-xprint \
  --enable-calendar \
  --enable-crypto \
  --enable-default-toolkit=gtk2 \
  --enable-extensions=default,irc \
  --enable-optimize=-O2 \
  --enable-xft \
  --with-default-mozilla-five-home=/usr/lib/mozilla-$VERSION \
  --with-system-zlib \
  --without-system-nspr &> configure.output
make $MAKEJOBS -s export &> make_export.output
make $MAKEJOBS -s libs &> make_libs.output
make DESTDIR=$PREPKG install &> make_install.output

chown -R root:bin $PREPKG/usr/bin/
cp -p LEGAL LICENSE README.txt $PREPKG/usr/doc/mozilla-$VERSION/

# Install nss headers.
find security/nss/lib/ -type f -name '*.h' \
  -exec cp -p {} $PREPKG/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 $PREPKG/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 )
)

gzip -r9 $PREPKG/usr/man/

cp $FILES/*.desktop $PREPKG/usr/share/gnome/apps/Internet/
cp $FILES/*.{gif,png} $PREPKG/usr/share/pixmaps/
cp $FILES/{doinst.sh,slack-desc} $PREPKG/install/

find $PREPKG -exec file {} \; \
  | awk -F : '/ ELF / {print $1}' \
  | xargs strip -p --strip-unneeded
