#!/bin/bash

# mozilla-thunderbird.SlackBuild
# by Stuart Winter <mozes@slackware.com>
# 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="-O -march=armv5te" ## -U_FORTIFY_SOURCE " # temporary solution for gcc 4.3 to fix "buffer overflow detected" @ runtime
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O" ;;
esac 

# Extract source:
echo "[**] Extracting source [**]"
tar xf $CWD/thunderbird-$VERSION.source.tar.bz2
cd comm* || exit 1
slackhousekeeping

# Apply patches:
#mkdir patches
#tar xvvf $PORTCWD/sources/mozilla-thunderbird-*-patches*.tar* -C patches/ || exit 1

#for pf in \
#    003--mozilla-jemalloc.patch \
#    300-xulrunner-fix-jemalloc-vs-aslr.patch \
#    102-no_dynamic_nss_softokn.patch \
#    115-thunderbird-shared-error.patch ;  do
#      auto_apply_patch patches/$pf || exit 1
#done

zcat $PORTCWD/sources/thunderbird*diff* | patch -p1
for i in bz420391_attXXXX_fix_unix_installer.patch \
bz532198_lp488354_ns_invokebyindex_not_thumb2_safe.patch \
bz591331_att469858_breakpad_allow_ptrace.patch \
bz593948_att474767_lp630281_moz_app_launcher_for_default_client.patch \
bzXXX_ftbfs_static_with_system_hunspell.patch \
bzXXX_linker_flag_ordering.patch \
lp682742_arm_it_instruction.patch \
lp_710648_arm_it_instruction_breakpad.patch \
no_dynamic_nss_softokn.patch ; do
  auto_apply_patch debian/patches/$i || exit 1
done

# Fix a long standing bug that's prevented staying current on GTK+.
# Thanks to the BLFS folks.  :-)
cat << EOF >> mozilla/layout/build/Makefile.in || exit 1

ifdef MOZ_ENABLE_CANVAS
EXTRA_DSO_LDOPTS += \$(XLDFLAGS) -lX11 -lXrender
endif

EOF

# Configure:
export MOZILLA_OFFICIAL="1" &&
export BUILD_OFFICIAL="1" &&
export MOZ_PHOENIX="1" &&
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-system-zlib \
  --enable-application=mail \
  --enable-default-toolkit=cairo-gtk2 \
  --enable-startup-notification \
  --enable-crypto \
  --enable-svg \
  --enable-canvas \
  --enable-xft \
  --enable-xinerama \
  --enable-ldap \
  --enable-optimize \
  --enable-reorder \
  --enable-static \
  --enable-strip \
  --enable-system-cairo \
  --enable-cpp-rtti \
  --enable-single-profile \
  --disable-accessibility \
  --disable-debug \
  --disable-tests \
  --disable-logging \
  --disable-pedantic \
  --disable-installer \
  --disable-profilesharing || exit 1

# Build thunderbird:
echo "[**] Building thunderbird [**]"
make MOZ_MAKE_FLAGS="$NUMJOBS" || exit 1

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

# We don't need these (just symlinks anyway):
rm -rf $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-devel-$VERSION

# Nor these:
rm -rf $PKG/usr/include

( cd $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-$VERSION
  cp -a defaults/profile/mimeTypes.rdf defaults/profile/mimeTypes.rdf.orig
  zcat $CWD/mimeTypes.rdf > defaults/profile/mimeTypes.rdf || exit 1
) || exit 1

mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/mozilla/plugins
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

# These files/directories are usually created if Firefox 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 extensions/talkback\@mozilla.org ]; then
    if [ ! -r extensions/talkback\@mozilla.org/chrome.manifest ]; then
      echo > extensions/talkback\@mozilla.org/chrome.manifest
    fi
  fi
  if [ ! -d updates ]; then
    mkdir -p updates/0
  fi
)

# Need some default icons in the right place:
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-$VERSION/chrome/icons/default
install -m 644 other-licenses/branding/thunderbird/default16.png \
  $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-$VERSION/icons/
install -m 644 other-licenses/branding/thunderbird/default16.png \
  $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-$VERSION/chrome/icons/default/
( cd $PKG/usr/lib${LIBDIRSUFFIX}/thunderbird-$VERSION
  install -m 644 icons/{default,mozicon50}.xpm chrome/icons/default/
)

# Copy over the LICENSE
install -p -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

# Change the architecture of the package name since Thunderbird 3.1
# doesn't support armv4:
export SLACKPACKAGE=$PKGNAM-$VERSION-armv5t-$BUILD.tgz

slackmp         # run makepkg -l y -c n

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