#/bin/sh
#
# SlackBuild by George Vlahavas (vlahavas~at~gmail~dot~com)
#
# This builds all available language packs for mozilla firefox
# Get the language packs (xpi files) from
# http://download-installer.cdn.mozilla.net/pub/firefox/releases/$VERSION/linux-i686/xpi/
# and put them all in a directory named firefox-xpi-$VERSION and create
# a tarball out of it (The getsource.sh script does just that).
# 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=128.1.0esr
MAINVER="${VERSION%%.*}esr"
BUILD=1gv

if [ -z $ARCH ]; then
	echo "ERROR: You have to set the ARCH variable. Use either i686 or x86_64."
	exit 1
fi

startdir=`pwd`

rm -rf {pkg,src,mozilla-firefox-l10n-packages-$MAINVER-$ARCH-$BUILD}

mkdir {pkg,src,mozilla-firefox-l10n-packages-$MAINVER-$ARCH-$BUILD}

if [ ! -f $startdir/firefox-xpi-$MAINVER.tar.xz ]; then
	echo "ERROR: firefox-xpi-$VERSION.tar.xz not found."
	exit 1
fi

tar xf $startdir/firefox-xpi-$MAINVER.tar.xz -C $startdir/src
cd $startdir/src/firefox-xpi-$MAINVER

# remove the langpacks that are going to be included with the main FF
# package
for i in `cat $startdir/LOCALES_mainpkg`; do
	rm $startdir/src/firefox-xpi-$MAINVER/$i.xpi
done

for i in `basename -a -s ".xpi" *.xpi`; do
	grep "^#$i " $startdir/firefox-langs || \
	echo "WARNING: No definition for $i"
done

for i in `basename -a -s ".xpi" *.xpi`; do
	mkdir -p $startdir/pkg/mozilla-firefox-l10n-$i/usr/lib/firefox-$MAINVER/browser/extensions
	cp -a $startdir/src/firefox-xpi-$MAINVER/$i.xpi $startdir/pkg/mozilla-firefox-l10n-$i/usr/lib/firefox-$MAINVER/browser/extensions/langpack-$i@firefox.mozilla.org.xpi
	mkdir -p $startdir/pkg/mozilla-firefox-l10n-$i/install
	LONGLANG=`grep "^#$i " $startdir/firefox-langs | sed "s/^#$i //g"`
	sed "s/__lang__/$i/g" $startdir/slack-desc.template | \
	sed "s/__longlang__/$LONGLANG/g" > $startdir/pkg/mozilla-firefox-l10n-$i/install/slack-desc
	cd $startdir/pkg/mozilla-firefox-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-firefox-l10n-packages-$MAINVER-$ARCH-$BUILD/mozilla-firefox-l10n-$i-$MAINVER-$ARCH-$BUILD.txz
	(
	cd $startdir/mozilla-firefox-l10n-packages-$MAINVER-$ARCH-$BUILD
	md5sum mozilla-firefox-l10n-$i-$MAINVER-$ARCH-$BUILD.txz > mozilla-firefox-l10n-$i-$MAINVER-$ARCH-$BUILD.md5
	)
done

rm -rf $startdir/{pkg,src}