#!/bin/bash
#
# installswiftfox.sh
#               Script to automatically download and install the newest release version of swiftfox for linux, on an Ubuntu system.
#               Requires an internet connection to work (obviously).
# 
# Author:       nanotube <nanotube@users.sf.net>.
#               
# Version:      installswiftfox.sh  1.8  03-Jul-2006  nanotube@users.sf.net
# 
#
#######

## Make sure that we exit if any commands do not complete successfully.
set -o errexit
trap 'echo "Previous command did not complete successfully. Exiting."' ERR

## Set version-specific variables
if grep -q "5.10" /etc/issue ; then
  PLUGINPATH=/usr/lib/mozilla-firefox/plugins
elif grep -q "6.06" /etc/issue ; then
  PLUGINPATH=/usr/lib/firefox/plugins
else
  echo "This script only works on Breezy or Dapper."
  exit 1
fi

## Get the newest swiftfox release version from getswiftfox.com
VERSION=`wget -q -O - http://getswiftfox.com/releases.htm |grep "<title>" |awk '{print $2}' |cut -d\< -f1`

echo -e -n "The most recent release of swiftfox is detected to be $VERSION. Please make sure this is correct before proceeding. (You can confirm by going to http://getswiftfox.com/) Is it correct [y/n]? "
while true
do
  read ans
  case $ans in
       Y|y) break ;;
  [Yy][Ee][Ss]) break ;;
       N|n) echo "If this does not agree with the latest version as listed on getswiftfox.com, please contact nanotube@users.sf.net and let me know."; exit ;;
  [Nn][Oo]) echo "If this does not agree with the latest version as listed on getswiftfox.com, please contact nanotube@users.sf.net and let me know."; exit ;;
         *) echo -n "Invalid command. Please answer yes or no [y/n] " ;;
  esac
done

## Get available localizations
PROCESSORS=( `wget -q -O - http://getswiftfox.com/releases.htm |grep "href=\"rel-" | sed -e 's/.*href="rel-//' -e 's/\.htm.*//' -e 's/celeron/pentium/' |sort | uniq` )

LIMIT=${#PROCESSORS[*]}

## Get user choice of localization

echo "Please choose your processor architecture for swiftfox. Enter the number of your choice from the list below. If you are not sure which one fits your CPU, please check the list on http://getswiftfox.com/releases.htm"
for ((i=0; i < LIMIT ; i++))
do
  echo "$i: ${PROCESSORS[$i]}"
done

CHOICE=$(($LIMIT + 1))

echo -n "Enter your choice of localization: "
while [ "$CHOICE" -gt "$(($LIMIT-1))" -o "$CHOICE" -lt "0" ]
do
  read CHOICE
  if echo -n "$CHOICE" | grep -q "[^0-9]"; then
    echo -n "Your input contains non-numeric characters. Please try again: "
    CHOICE=$(($LIMIT + 1))
    continue
  elif [ -z "$CHOICE" ]; then
    echo -n "Please enter the number of your choice from the list: "
    CHOICE=$(($LIMIT + 1))
    continue
  elif [ "$CHOICE" -gt "$(($LIMIT-1))" -o "$CHOICE" -lt "0" ]; then
    echo -n "Your input is not in the range of available localizations. Please try again: "
  fi
done

echo -n "You have chosen CPU architecture \"${PROCESSORS[$CHOICE]}\". Is that correct [y/n]? "
while true
do
  read ans
  case $ans in
       Y|y) break ;;
  [Yy][Ee][Ss]) break ;;
       N|n) echo "In that case, start over by running this script again."; exit ;;
  [Nn][Oo]) echo "In that case, start over by running this script again."; exit ;;
         *) echo -n "Invalid command. Please answer yes or no [y/n] " ;;
  esac
done

## Proceed to download and install swiftfox.

echo -e "\nUpdating repositories list\n"
sudo aptitude update

echo -e "\nMaking sure libstdc++5 and the old Firefox are installed\n"
sudo aptitude install firefox libstdc++5

if [ -d ~/.mozilla ]; then
  echo -e "\nBacking up old Firefox preferences\n"
  cp -R ~/.mozilla ~/.mozilla_backup_`date -Iseconds`
else
  echo -e "\nOld firefox preferences not found. Nothing to back up. Proceeding with installation.\n"
fi

echo -e "\nChanging to home directory\n"
cd

echo -e "\nDownloading Swiftfox from the Swiftfox site\n"
wget -c http://getswiftfox.com/builds/releases/swiftfox-$VERSION-${PROCESSORS[$CHOICE]}.tar.bz2

echo -e "\nDownloading Swiftfox md5 sum from the Swiftfox site\n"
wget -c http://getswiftfox.com/builds/releases/md5sum-${PROCESSORS[$CHOICE]}

echo -e "\nVerifying md5sum...\nIf the script terminates at this point, the download was corrupt, and you should delete the swiftfox downloads and run this script again.\n"
md5sum --check md5sum-${PROCESSORS[$CHOICE]} --status

echo -e "\nUnzipping the .tar.gz file\n"
sudo tar -C /opt -xjf swiftfox-$VERSION-${PROCESSORS[$CHOICE]}.tar.bz2

echo -e "\nRemoving the unzipped .tar.gz\n"
rm -f swiftfox-$VERSION-${PROCESSORS[$CHOICE]}.tar.bz2 md5sum-${PROCESSORS[$CHOICE]}

echo -e "\nLinking plugins\n"
cd /opt/swiftfox/plugins/
sudo ln -s -f $PLUGINPATH/* .

echo -e "\nLinking firefox launcher to Swiftfox\n"
sudo dpkg-divert --divert /usr/bin/firefox.ubuntu --rename /usr/bin/firefox
sudo ln -s /opt/swiftfox/firefox /usr/bin/firefox
sudo dpkg-divert --divert /usr/bin/mozilla-firefox.ubuntu --rename /usr/bin/mozilla-firefox
sudo ln -s /opt/swiftfox/firefox /usr/bin/mozilla-firefox

echo -e "\nSwiftfox version \"$VERSION\" for architecture \"${PROCESSORS[$CHOICE]}\" has been installed successfully."

exit
