#!/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="-D__ARM_PCS -g -O2 -march=armv5te -fstack-protector --param=ssp-buffer-size=4 -Wall"
#            export SLKCXXFLAGS="-g -std=gnu++0x -D__ARM_PCS" # -D__ARM_PCS is needed for the newer version of libffi that we include.
            export SLKCXXFLAGS="$SLKCFLAGS"
            export LDFLAGS="-Wl,--no-as-needed -Wl,--no-keep-memory -Wl,--reduce-memory-overheads"

            # The little hacks section:
            # Firefox 17.0 adds webrtc but it does not work on ARM
            SLKCONFARGS="--disable-webrtc"
            # 
            # --with-system-libvpx=/tmp/pkg-libvpx 
            export SLKCONFARGS="$SLKCONFARGS \
                                --with-arch=armv5te \
                                --with-float-abi=soft \
                                --enable-extensions=default \
                                --disable-elf-dynstr-gc \
                                --disable-mochitest \
                                --disable-tests \
                                --disable-installer \
                                --disable-javaxpcom \
                                --disable-crashreporter \
                                --disable-elf-hack \
                                --disable-methodjit \
                                --disable-tracejit \
                                --enable-system-ffi \
                                --host=$ARCH-slackware-linux-gnueabi \
                                --target=$ARCH-slackware-linux-gnueabi"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O" ;;
esac 

# This is used just for the configure script only:
MOZVERS=${MOZVERS:-release}

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

# Work-around:
sed -i 's?-Werror?-Wall?g' configure

# ARM fixes - from Ubuntu:
auto_apply_patch $PORTCWD/sources/no_neon_on_arm.patch.xz || exit 1

# FTBFS fixes:
#for i in $PORTCWD/sources/*patch* ; do
#  auto_apply_patch $i || exit 1
#done

if gcc --version | grep -q "gcc (GCC) 4.7.0" ; then
 # Enable compiling with gcc-4.7.0:
 sed -i '/fcntl.h/a#include <unistd.h>' \
   mozilla/ipc/chromium/src/base/{file_util_linux,message_pump_libevent,process_util_posix}.cc &&
 sed -i '/sys\/time\.h/a#include <unistd.h>' mozilla/ipc/chromium/src/base/time_posix.cc &&
 sed -i 's#\"PRIxPTR#\" PRIxPTR#g' mozilla/layout/base/tests/TestPoisonArea.cpp &&
 sed -i 's#\"CRLF#\" CRLF#g' mailnews/base/search/src/nsMsgSearchAdapter.cpp &&
 sed -i 's#\"CRLF#\" CRLF#g' mailnews/base/src/nsMsgFolderCompactor.cpp &&
 sed -i 's#\"CRLF#\" CRLF#g' mailnews/compose/src/nsSmtpProtocol.cpp &&
 sed -i 's#\"CRLF#\" CRLF#g' mailnews/imap/src/nsImapMailFolder.cpp &&
 sed -i 's#\"CRLF#\" CRLF#g' mailnews/imap/src/nsImapProtocol.cpp &&
 sed -i 's#\"CRLF#\" CRLF#g' mailnews/imap/src/nsImapServerResponseParser.cpp &&
 sed -i 's#\"CRLF#\" CRLF#g' mailnews/local/src/nsPop3Protocol.cpp &&
 sed -i 's#\"CRLF#\" CRLF#g' mailnews/mime/src/mimedrft.cpp &&
 sed -i 's#\"MSG_LINEBREAK#\" MSG_LINEBREAK#g' mailnews/mime/src/mimemult.cpp &&
 sed -i 's#\"MSG_LINEBREAK#\" MSG_LINEBREAK#g' mailnews/base/src/nsMsgFolderCompactor.cpp &&
 sed -i 's# ""##' mozilla/browser/base/Makefile.in
fi

# 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

# Mozilla devs enforce using an objdir for building
# and launching configure with the absolute path
# https://developer.mozilla.org/en/Configuring_Build_Options#Building_with_an_objdir
ODIR=$PWD
mkdir obj
cd obj

# Configure:
export MOZILLA_DIR=$TMPBUILD/comm-$MOZVERS/mozilla &&
export MOZILLA_OFFICIAL="1" &&
export BUILD_OFFICIAL="1" &&
export MOZ_PHOENIX="1" &&
export CFLAGS="$SLKCFLAGS" &&
export CXXFLAGS="$SLKCXXFLAGS" &&
export MOZ_MAKE_FLAGS="$NUMJOBS" &&
$ODIR/configure $SLKCONFARGS \
  --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-ldap \
  --enable-libxul \
  --enable-svg \
  --enable-canvas \
  --enable-xft \
  --enable-xinerama \
  --enable-optimize \
  --enable-reorder \
  --enable-strip \
  --enable-cpp-rtti \
  --enable-single-profile \
  --disable-accessibility \
  --disable-debug \
  --disable-tests \
  --disable-logging \
  --disable-pedantic \
  --disable-installer \
  --disable-profilesharing || exit 1

# Broken with thunderbird 12.0:
#  --enable-system-cairo \

# Build thunderbird:
echo "[**] Building thunderbird [**]"
find . -name configure.in | while read line ; do touch ${line%.*}; done
find . -name configure.in | while read line ; do touch ${line%%configure.in}/config.status; done
make $NUMJOBS || make || exit 1

# Install into package framework:
make install DESTDIR=$PKG  || exit 1
# Leave "obj" dir
cd ..

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

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

#  Thunderbird 3.x cruft?
#( 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:
# Debian re-added support in Thunderbird5.
# The 'hfp' name means that it's 'hardware floating point':
export SLACKPACKAGE=$PKGNAM-$VERSION-armhfp-$BUILD.tgz

slackmp         # run makepkg -l y -c n

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

# Clean up the residue for this hack:
rm -rf /tmp/pkg-libvpx
