#!/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/$PKGNAM
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-Os -march=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-Os" ;;
   sparc)   export SLKCFLAGS="-Os" ;;
   *)       export SLKCFLAGS="-Os" ;;
esac

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

# Apply patches:
zcat $PORTCWD/sources/icedove_*.diff.gz | patch -p1 || failpatch
patch --verbose -p1 < debian/patches/38_arm_xpcom_optim.dpatch || failpatch
patch --verbose -p1 < debian/patches/18_arm_xpcom_unused_attribute.dpatch || failpatch

# Configure:
export MOZILLA_OFFICIAL="1" &&
export BUILD_OFFICIAL="1" &&
export MOZ_CO_PROJECT="mail" &&
export CFLAGS="$SLKCFLAGS" &&
export CXXFLAGS="$SLKCFLAGS" &&
./configure \
   --enable-official-branding \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --with-default-mozilla-five-home=/usr/lib${LIBDIRSUFFIX}/thunderbird-$VERSION \
   --with-user-appdir=.thunderbird \
   --with-system-zlib \
   --with-system-png \
   --enable-application=mail \
   --enable-default-toolkit=gtk2 \
   --enable-crypto \
   --enable-xft \
   --enable-pango \
   --enable-xinerama \
   --enable-optimize \
   --enable-reorder \
   --enable-strip \
   --enable-cpp-rtti \
   --enable-single-profile \
   --enable-ldap \
   --disable-freetype2 \
   --disable-accessibility \
   --disable-debug \
   --disable-tests \
   --disable-logging \
   --disable-pedantic \
   --disable-installer \
   --disable-profilesharing \
   --disable-gnomevfs \
   --disable-gnomeui || failconfig

# Build thunderbird:
echo "[**] Building thunderbird [**]"
make MOZ_OPT_FLAGS="$SLKCFLAGS" MOZ_MAKE_FLAGS="$NUMJOBS" || failmake

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

# We do not need 20+ MB of includes:
rm -rf $PKG/usr/include

( cd $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-$VERSION
  echo "Applying firefox patch"
  zcat $CWD/mozilla-thunderbird-firefox.diff.gz | patch -p0 --verbose || exit 1
) || exit 1

mkdir -p $PKG/usr/share/applications
cat $CWD/mozilla-thunderbird.desktop > $PKG/usr/share/applications/mozilla-thunderbird.desktop
mkdir -p $PKG/usr/share/pixmaps
cat $CWD/thunderbird.png > $PKG/usr/share/pixmaps/thunderbird.png

mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-$VERSION/chrome/icons/default/
install -m 644 other-licenses/branding/thunderbird/default.xpm \
  $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-$VERSION/chrome/icons/default/
install -m 644 other-licenses/branding/thunderbird/default.xpm \
  $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-$VERSION/icons/

# 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.
( cd $PKG/usr/lib${LIBDIRSUFFIX}/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 -p updates/0
  fi
)

# Copy over the LICENSE
install -c -m 644 LICENSE $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-$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
slackmp         # run makepkg -l y -c n

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