#!/bin/sh
TARGET="$HOME"/mozilla
[ -x /usr/bin/curl ] && LOAD="/usr/bin/curl -s"
[ -x /usr/bin/wget ] && LOAD="/usr/bin/wget -qO-"
FF_VER=$($LOAD http://releases.mozilla.org/pub/mozilla.org/firefox/releases/latest/MD5SUMS|grep firefox-|head -1|sed 's/.*firefox-//;s/\.tar.bz2//')
#TB_VER=$($LOAD http://releases.mozilla.org/pub/mozilla.org/thunderbird/releases/latest/MD5SUMS|grep thunderbird-|head -1|sed 's/.*thunderbird-//;s/\.tar.bz2//')
TRY=1;while TB_VER=$($LOAD http://releases.mozilla.org/pub/mozilla.org/thunderbird/releases|awk -F'"' '/3.3/{print $6}'|tail -1|sed s#/##);case $TB_VER in 3.3*) false;;esac; do TRY=$((TRY+1));echo -n . >&2;sleep .1;done
echo "$TRY attempt(s) needed to get this Thunderbird version: $TB_VER" >&2
case $LANG in
 de*) DLANG=de ;;
   *) DLANG=en-US ;;
esac
killall -9 firefox-bin thunderbird-bin 2>/dev/null
rm -rf "$TARGET"/firefox "$TARGET"/thunderbird
mkdir -p "$TARGET"
ARCH=i686
[ "$(uname -m)" = "x86_64" ] && ARCH=x86_64
#if [ "$(uname -m)" = "x86_64" ]; then
 #$LOAD http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-firefox-3.6.x/$($LOAD "http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-firefox-3.6.x/?C=M;O=D"|awk -F'"' '/en-US.linux-x86_64.tar.bz2/{print $8}'|head -1)|tar xjvC "$TARGET"
 $LOAD http://releases.mozilla.org/pub/mozilla.org/firefox/releases/latest/linux-$ARCH/$DLANG/firefox-$FF_VER.tar.bz2|tar xjvC "$TARGET"
#else
# $LOAD "http://download.mozilla.org/?product=firefox-$FF_VER&os=linux&lang=$DLANG"|tar xjvC "$TARGET"
#fi
#$LOAD "http://download.mozilla.org/?product=thunderbird-$TB_VER&os=linux&lang=$DLANG"|tar xjvC "$TARGET"
#$LOAD http://releases.mozilla.org/pub/mozilla.org/thunderbird/releases/$TB_VER/linux-$ARCH/en-US/miramar-$TB_VER.tar.bz2|tar xjvC "$TARGET"
$LOAD http://releases.mozilla.org/pub/mozilla.org/thunderbird/releases/$TB_VER/linux-$ARCH/en-US/miramar-$TB_VER.tar.bz2|tar xjvC "$TARGET"
[ -d ~/.mozilla-thunderbird -a ! -d ~/.thunderbird ] && ln -vs ~/.mozilla-thunderbird ~/.thunderbird
mkdir -p ~/.mozilla/plugins
rm -f ~/.mozilla/plugins/libflashplayer.so
rm -f ~/.mozilla/firefox/pluginreg.dat
URL=http://fpdownload.macromedia.com/get/flashplayer/current
FILE=install_flash_player_10_linux.tar.gz
if [ "$(uname -m)" = "x86_64" ]; then
 #URL=http://download.macromedia.com/pub/flashplayer/installers/current/9
 #FILE=install_flash_player_9.tar.gz
 #$LOAD $URL/$FILE|tar xzvC "$TARGET"/firefox/plugins
 URL=http://download.macromedia.com/pub/labs/flashplayer10
 #FILE=libflashplayer-10.0.45.2.linux-x86_64.so.tar.gz
 #FILE=$($LOAD http://labs.adobe.com/downloads/flashplayer10_64bit.html|awk -F'"' '/libflashplayer/{print $12}'|sed 's|.*/||')
 #FILE=$($LOAD http://labs.adobe.com/downloads/flashplayer10.html|awk -F'"' '/flashplayer/&&/linux/&&/64/{print $12}'|sed 's|.*/||')
 FILE=$($LOAD http://labs.adobe.com/downloads/flashplayer10_square.html|awk -F'"' '/flashplayer/&&/linux/&&/64/{print $12}'|sed 's|.*/||')
 $LOAD $URL/$FILE|tar xzvC ~/.mozilla/plugins
else
 $LOAD $URL/$FILE|tar xzvC ~/.mozilla/plugins
fi
grep -q lenny /etc/apt/sources.list && sed -i s/libvdpau/libvdpax/g ~/.mozilla/plugins/libflashplayer.so
[ -x /usr/bin/nspluginscan ] && /usr/bin/nspluginscan
[ "$KDE_SESSION_VERSION" = "4" -a -x /usr/bin/kbuildsycoca4 ] && /usr/bin/kbuildsycoca4
rm -f ~/Desktop/Firefox.desktop
rm -f ~/Desktop/Thunderbird.desktop
mkdir -p ~/Desktop
cat <<EOT > ~/Desktop/Firefox.desktop
#!/usr/bin/env xdg-open
[Desktop Entry]
Exec=$TARGET/firefox/firefox
Icon=$TARGET/firefox/chrome/icons/default/default48.png
Name=Firefox
StartupNotify=false
Type=Application
EOT
chmod 744 ~/Desktop/Firefox.desktop
cat <<EOT > ~/Desktop/Thunderbird.desktop
#!/usr/bin/env xdg-open
[Desktop Entry]
Exec=$TARGET/thunderbird/thunderbird
Icon=$TARGET/thunderbird/chrome/icons/default/default48.png
Name=Thunderbird
StartupNotify=false
Type=Application
EOT
chmod 744 ~/Desktop/Thunderbird.desktop
