#!/bin/bash

# by teknohog who resides at iki.fi

# This script is meant for installing and running a nightly build of
# Mozilla Firefox. It will need updating at least for the 'tarball'
# and 'web_dir' variables when new branches appear.

cd ~

web_dir=http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-trunk/
#tarball=firefox-3.6a1pre.en-US.linux-i686.tar.bz2

# try to find the latest tarball for a given platform, identified by postfix
tb_prefix=firefox-
tb_postfix=linux-$HOSTTYPE.tar.bz2
tarballs=(`curl -o - $web_dir | grep $tb_postfix | sed -e "s/.*>\($tb_prefix[a-zA-Z0-9.-]*$tb_postfix\)<.*/\1/" | sort -r`)
tarball=${tarballs[0]}

local_dir=firefox

remote_date=`curl --head $web_dir$tarball | grep Date | awk '{print $3 " " $4 " " $5 " " $6}'`

# convert to unix time for easy comparison
remote_date=`date -d "$remote_date" +%s`
local_date=`date -r $tarball +%s`
#ldir_date=`date -r $local_dir +%s`

if [ -z $local_date ] || [ $local_date -lt $remote_date ]; then
    rm $tarball
    curl -o $tarball $web_dir$tarball
else
    echo "Latest tarball already here."
fi

echo "Unpacking the tarball..."

# backup one older version
rm -r old-$local_dir
mv $local_dir old-$local_dir

# tar really needs some sort of autodetection... I think this hack is
# safer than counting on a given zip method for a branch
case "`file -ib $tarball`" in
    application/x-bzip2*)
	tar xjf $tarball
	;;
    application/x-gzip*)
	tar xzf $tarball
	;;
    *)
	echo "Tarball gzip/bzip2 detection failed, exiting"
	exit
	;;
esac

cd $local_dir/plugins/ || exit

#for file in /opt/netscape/plugins/*; do 
#    ln -s $file;
#done

ln -s /usr/lib/nsbrowser/plugins/* .

# work around a recent Minefield bug
#export LD_PRELOAD=/usr/lib/libXinerama.so

~/$local_dir/firefox
