#!/bin/sh
CWD=`pwd`
TMP=${TMP:-/tmp}
PKG=$TMP/package-thunderbird-ce

VERSION=2.0.0.9
ARCH=${ARCH:-powerpc}
BUILD=${BUILD:-1}

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
rm -rf $PKG
mkdir -p $PKG/usr/lib

cd $TMP
rm -rf mozilla
tar jxvf $CWD/thunderbird-$VERSION-source.tar.bz2

cd mozilla
chown -R root.root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 640 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -type d -exec chmod 755 {} \;

ac_cv_visibility_pragma=no \
MOZ_THUNDERBIRD=1 \
./configure --prefix=/usr \
	--with-default-mozilla-five-home=/usr/lib/thunderbird-$VERSION \
	--with-user-appdir=.thunderbird \
	--with-system-zlib \
	--with-system-png \
	--enable-application=mail \
	--enable-default-toolkit=gtk2 \
	--enable-crypto \
	--enable-xft \
	--enable-xinerama \
	--enable-optimize \
	--enable-reorder \
	--enable-strip \
	--enable-cpp-rtti \
	--enable-single-profile \
	--disable-freetype2 \
	--disable-accessibility \
	--disable-activex \
	--disable-activex-scripting \
	--disable-debug \
	--disable-tests \
	--disable-logging \
	--disable-pedantic \
	--disable-installer \
	--disable-profilesharing

zcat $CWD/mozilla-thunderbird-firefox.diff.gz | patch -p0 --verbose || exit 1

MOZ_THUNDERBIRD=1 make -j 4 || exit 1
MOZ_THUNDERBIRD=1 DESTDIR=$PKG make install


( cd $PKG/usr/bin
  chown -R root:root .
)

( cd $PKG/usr/lib
  ln -sf thunderbird-$VERSION thunderbird
)

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

# 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/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
)

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n $TMP/thunderbird-$VERSION-$ARCH-$BUILD.tgz

