#!/bin/sh
if [ "$(id -u)" = "0" ]; then
 echo "Error: Do NOT run this script as root!" >&2
 exit 1
fi

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 -v ./update|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 -v ./update|grep thunderbird-|head -1|sed 's/.*thunderbird-//;s/\.tar.bz2//')
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"
if [ "$(uname -m)" = "x86_64" ]; then
 $LOAD http://releases.mozilla.org/pub/mozilla.org/firefox/releases/latest/linux-x86_64/$DLANG/firefox-$FF_VER.tar.bz2|tar xjvC "$TARGET"
 $LOAD http://releases.mozilla.org/pub/mozilla.org/thunderbird/releases/latest/linux-x86_64/$DLANG/thunderbird-$TB_VER.tar.bz2|tar xjvC "$TARGET"
else
 $LOAD "http://download.mozilla.org/?product=firefox-$FF_VER&os=linux&lang=$DLANG"|tar xjvC "$TARGET"
 $LOAD "http://download.mozilla.org/?product=thunderbird-$TB_VER&os=linux&lang=$DLANG"|tar xjvC "$TARGET"
fi
[ -d ~/.mozilla-thunderbird -a ! -d ~/.thunderbird ] && ln -vs ~/.mozilla-thunderbird ~/.thunderbird
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
