#/bin/sh
#
# SlackBuild by George Vlahavas (vlahavas~at~gmail~dot~com)
#
# This builds all available language packs for mozilla thunderbird
# Get the language packs (xpi files) from
# http://ftp.osuosl.org/pub/mozilla.org/thunderbird/releases/$VERSION/linux-i686/xpi/
# and put them all in a directory named thunderbird-xpi-$VERSION and create
# a tarball out of it.
# Don't mind the linux-i686 in the URL, the localization packages work
# for x86_64 platforms as well, as long as you use the correct
# installation directories.

VERSION=3.1.11
ARCH=i486
BUILD=1gv

startdir=`pwd`

rm -rf {pkg,src,mozilla-thunderbird-l10n-packages-$VERSION-$ARCH-$BUILD}

mkdir {pkg,src,mozilla-thunderbird-l10n-packages-$VERSION-$ARCH-$BUILD}

tar xf $startdir/thunderbird-xpi-$VERSION.tar.gz -C $startdir/src
cd $startdir/src/thunderbird-xpi-$VERSION

for i in `ls *.xpi | sed "s/\.xpi//"`; do
	grep "^#$i " $startdir/thunderbird-langs || \
	( echo "No definition for $i" && exit 1 )
done

for i in `ls *.xpi`; do
	unzip $i -d `echo $i | sed "s/\.xpi//"`
done

for i in `ls *.xpi | sed "s/\.xpi//"`; do
	mkdir -p $startdir/pkg/mozilla-thunderbird-l10n-$i/usr/lib/thunderbird-$VERSION/extensions/langpack-$i@thunderbird.mozilla.org
	cp -a $startdir/src/thunderbird-xpi-$VERSION/$i/* $startdir/pkg/mozilla-thunderbird-l10n-$i/usr/lib/thunderbird-$VERSION/extensions/langpack-$i@thunderbird.mozilla.org/
	mkdir -p $startdir/pkg/mozilla-thunderbird-l10n-$i/usr/lib/thunderbird-$VERSION/defaults/pref
	cp $startdir/langpacks.js $startdir/pkg/mozilla-thunderbird-l10n-$i/usr/lib/thunderbird-$VERSION/defaults/pref/
	mkdir -p $startdir/pkg/mozilla-thunderbird-l10n-$i/install
	LONGLANG=`grep "^#$i " $startdir/thunderbird-langs | sed "s/^#$i //g"`
	sed "s/__lang__/$i/g" $startdir/slack-desc.template | \
	sed "s/__longlang__/$LONGLANG/g" > $startdir/pkg/mozilla-thunderbird-l10n-$i/install/slack-desc
	cd $startdir/pkg/mozilla-thunderbird-l10n-$i

	chown -R root:root .
	find . \
	\( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
	-exec chmod 755 {} \; -o \
	\( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
	-exec chmod 644 {} \;

	if [ $ARCH = "x86_64" ];then
		mv usr/lib usr/lib64
	fi

	/sbin/makepkg -l y -c n $startdir/mozilla-thunderbird-l10n-packages-$VERSION-$ARCH-$BUILD/mozilla-thunderbird-l10n-$i-$VERSION-$ARCH-$BUILD.txz
	(
	cd $startdir/mozilla-thunderbird-l10n-packages-$VERSION-$ARCH-$BUILD
	md5sum mozilla-thunderbird-l10n-$i-$VERSION-$ARCH-$BUILD.txz > mozilla-thunderbird-l10n-$i-$VERSION-$ARCH-$BUILD.md5
	)
done

rm -rf $startdir/{pkg,src}