#!/bin/bash

# seamonkey.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar jxvvf $CWD/$PACKAGE-${VERSION}.source.tar.bz2
cd mozilla
slackhousekeeping

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

# Fix ARM build bug as listed here:
# http://forums.mozillazine.org/viewtopic.php?t=271308&
# http://www.archivum.info/linux.debian.bugs.dist/2005-09/msg02539.html
patch --verbose -p0 < $PORTCWD/sources/undefinedref.patch || failpatch

# Configure:
# use make-3.80 since 3.81 is producing some errors: 
ac_cv_path_MAKE=/usr/bin/make-3.80 \
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 \
./configure \
   --prefix=/usr \
   --enable-optimize=-O2 \
   --disable-debug \
   --with-default-mozilla-five-home=/usr/lib/seamonkey-${VERSION} \
   --enable-strip \
   --disable-tests \
   --enable-svg \
   --enable-canvas \
   --disable-short-wchar \
   --enable-nspr-autoconf \
   --enable-calendar \
   --enable-extensions=default,irc \
   --enable-crypto \
   --disable-xprint \
   --without-system-nspr \
   --with-system-zlib \
   --with-system-jpeg \
   --with-system-png \
   --with-system-mng \
   --enable-application=suite \
   --enable-xft \
   $ARCH-slackware-linux || failconfig

# Build:
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 make -s export || failmake
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 make -s libs   || failmake

# Install into package:
BUILD_OFFICIAL=1 MOZILLA_OFFICIAL=1 DESTDIR=$PKG make install || failinstall

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

# Link some 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.
( cd $PKG/usr/lib && ( for somelib in libmozjs.so libnspr4.so libnss3.so libplc4.so libplds4.so libsmime3.so libsoftokn3.so libssl3.so ; do
    # Link to it:
    ln -sf seamonkey-${VERSION}/$somelib .
  done )
)

# This remains the standard plugin directory for all browsers.
mkdir -p $PKG/usr/lib/mozilla/plugins
# Some software won't compile without this symlink:
( cd $PKG/usr/lib
  if [ ! -e seamonkey ]; then
     ln -vvsf seamonkey-${VERSION} seamonkey
  fi )
mkdir -p $PKG/usr/share/applications
cp -a $CWD/*.desktop $PKG/usr/share/applications
chmod 644 $PKG/usr/share/applications/*
mkdir -p $PKG/usr/share/pixmaps
cp -a $CWD/*.png $PKG/usr/share/pixmaps
chmod 644 $PKG/usr/share/pixmaps/*

# Copy docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a LEGAL LICENSE README.txt \
      $PKG/usr/doc/$PACKAGE-$VERSION

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
