#!/bin/bash

# mozilla-thunderbird.SlackBuild
# by Stuart Winter <stuart@armedslack.org>
# 26-Apr-2005

# 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="-O" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
echo "[**] Extracting source [**]"
tar jxvvf $PORTCWD/sources/thunderbird-$VERSION-source.tar.bz2
cd mozilla
slackhousekeeping

# Apply patches:
#bzcat $PORTCWD/sources/thunderbird-0.7.3-freetype-compile.patch.bz2 | patch -p1 --verbose || failpatch
# 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
patch --verbose -p1 < $PORTCWD/sources/50_arch_arm_fix.dpatch || failpatch

# Configure:
export BUILD_OFFICIAL=1
export MOZILLA_OFFICIAL=1
export MOZ_THUNDERBIRD=1
export PACKAGE=mozilla-thunderbird
./configure \
   --enable-application=mail \
   --prefix=/usr \
   --infodir=/usr/info \
   --program-prefix=mozilla- \
   --enable-default-toolkit=gtk2 \
   --with-default-mozilla-five-home=/usr/lib/thunderbird-$VERSION \
   --with-user-appdir=.mozilla-thunderbird \
   --with-system-mng=/usr \
   --with-system-png=/usr \
   --with-system-jpeg=/usr \
   --with-system-zlib=/usr \
   --without-system-nspr \
   --enable-crypto \
   --disable-mathml \
   --disable-installer \
   --disable-activex \
   --disable-activex-scripting \
   --disable-tests \
   --disable-oji \
   --disable-necko-disk-cache \
   --disable-debug \
   --enable-optimize="$SLKCFLAGS" \
   --enable-strip \
   --enable-single-profile \
   --disable-profilesharing \
   --enable-extensions=wallet,spellcheck,xmlextras,typeaheadfind,webservices \
   --enable-necko-protocols=http,file,jar,viewsource,res,data \
   --enable-image-decoders=default,-xbm \
   --enable-xft \
   --disable-xprint \
   --target=$ARCH-slackware-linux \
   --host=$ARCH-slackware-linux \
   --build=$ARCH-slackware-linux || failconfig

# Build thunderbird:
echo "[**] Building thunderbird [**]"
make || failmake

# Install into package framework:
make install DESTDIR=$PKG 

# Install the wrapper script (this doesn't get installed above):
if [ ! -x $PKG/usr/lib/thunderbird-$VERSION/thunderbird ]; then
   install -m755 mail/app/thunderbird $PKG/usr/lib/thunderbird-$VERSION/
fi

# Slackware fixups:
( cd $PKG/usr/lib
  ln -fs thunderbird-$VERSION thunderbird
  cd thunderbird-$VERSION
  zcat $CWD/mozilla-thunderbird-simple.diff.gz  | patch -p1 --verbose --backup --suffix=.orig || failpatch
  zcat $CWD/mozilla-thunderbird-firefox.diff.gz | patch -p1 --verbose --backup --suffix=.orig || failpatch
  rm -f defaults/pref/all-thunderbird.js.orig )
mkdir -p $PKG/usr/bin
( cd $PKG/usr/bin
  ln -sf ../lib/thunderbird-$VERSION/thunderbird . )

# This stuff isn't in the Slackware package:
( cd $PKG
  rm -rf usr/lib/pkgconfig
  rm -rf usr/include
  rm -rf usr/share/{idl,aclocal} )

# Create the extensions files so that the non-root users don't get
# a warning when they start the browser:
( cd $PKG/usr/lib/thunderbird-$VERSION
  export LD_LIBRARY_PATH="$PWD"
  export MOZILLA_FIVE_HOME="$PWD"
  ./regxpcom
  ./regchrome
  touch `find . -name *.rdf`
  ./thunderbird -register )

# Install icons:
mkdir -p $PKG/usr/share/{applications,pixmaps}
install -m644 $CWD/mozilla-thunderbird.desktop $PKG/usr/share/applications
install -m644 $CWD/thunderbird.png $PKG/usr/share/pixmaps

# These files/directories are usually created if Thunderbird is run as root, which on many
# systems might (and possibly should) be never.  Therefore, if we don't see them we'll
# put stubs in place to prevent startup errors.
# This shouldn't be necessary since we do the register thing above, but I'll leave it
# in just incase.
( cd $PKG/usr/lib/thunderbird-$VERSION
  if [ -d chrome ]; then
     if [ ! -r chrome/app-chrome.manifest ]; then
       echo > chrome/app-chrome.manifest
     fi
  fi
  if [ ! -d updates ]; then
     mkdir -pm755 updates/0
  fi )

# 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
