#!/bin/bash

# Created by Chris Jean (gaarai.com) and released under GPL
# Based off of code from (http://queleimporta.com/downloads/flash10_en.sh)
# For more details, visit http://gaarai.com/2009/02/20/proper-adobe-flash-support-on-ubuntu-64-bit/

# Version 1.0.1

# Version History
#   2009-02-20 - 1.0.0
#     Initial Version


NORMAL="\e[0m"
RED="\e[1;31m"
BLUE="\e[1;34m"
GREEN="\e[1;32m"


LS=`sudo -p "Please supply your password to allow this script to use sudo: " ls -la .`

if test ! -n "$LS"; then
	echo -e "${RED}Sorry, sudo access is required for this script to function."
	echo -e "For more information, please visit:"
	echo -e "http://gaarai.com/2009/02/20/proper-adobe-flash-support-on-ubuntu-64-bit/$NORMAL"
	exit 1
fi


echo -e "${BLUE}Setting up...$NORMAL"

FILEURL="http://download.macromedia.com/pub/labs/flashplayer10/libflashplayer-10.0.d21.1.linux-x86_64.so.tar.gz"
FILE="libflashplayer-10.0.d21.1.linux-x86_64.so.tar.gz"
DEST="/usr/lib/mozilla/plugins/"

cd ~
sudo rm -f ~/libflashplayer.so
sudo rm -f ~/$FILE

echo


echo -en "${BLUE}Checking for required destination... $NORMAL"

if test ! -d $DEST; then
	echo -e "${RED}Not Found"
	echo "Unable to find $DEST"
	echo "This may mean that this script will not work for you"
	echo -en "Would you like to continue (y/N): $NORMAL"
	read MAKEDIR
	
	if test -z $MAKEDIR || test $MAKEDIR != "y"; then
		echo -e "${RED}Script execution cancelled by user$NORMAL"
		exit 1
	fi
	
	echo -e "${BLUE}Creating $DEST directory$NORMAL"
	
	sudo mkdir -p $DEST
	if test ! -d $DEST; then
		echo -e "${RED}Failed to create $DEST"
		echo -e "The script will not continue$NORMAL"
		exit 1
	fi
	
	echo -e "${GREEN}The $DEST directory was successfully created$NORMAL"
else
	echo -e "${GREEN}Found$NORMAL"
fi
echo


echo -en "${BLUE}Downloading Flash Player 10... $NORMAL"

wget -q $FILEURL
if test ! -f $FILE; then
	echo -e "${RED}Failed"
	echo "Was unable to download $FILEURL"
	echo -e "Running again to output the error.$NORMAL"
	
	wget $FILEURL
	if test ! -f $FILE; then
		echo -e "${RED}Please report this error at http://gaarai.com/2009/02/20/proper-adobe-flash-support-on-ubuntu-64-bit/$NORMAL"
		exit 1
	fi
	
	echo -e "${GREEN}It worked this time. Continuing. :)$NORMAL"
else
	echo -e "${GREEN}Success$NORMAL"
fi
echo


echo -en "${BLUE}Closing Firefox... $NORMAL"
sudo killall -q -9 -r firefox
echo -e "${GREEN}Success$NORMAL"
echo


echo -en "${BLUE}Removing previous installs of Flash... $NORMAL"
sudo apt-get remove -y --purge -qq flashplugin-nonfree gnash gnash-common mozilla-plugin-gnash swfdec-mozilla libflashsupport nspluginwrapper
sudo rm -f /usr/lib/mozilla/plugins/*flash*
sudo rm -f ~/.mozilla/plugins/*flash*
sudo rm -f /usr/lib/firefox/plugins/*flash*
sudo rm -f /usr/lib/firefox-addons/plugins/*flash*
sudo rm -rf /usr/lib/nspluginwrapper
echo -e "${GREEN}Success$NORMAL"
echo


echo -en "${BLUE}Installing Flash Player 10... $NORMAL"
tar xfz $FILE
sudo cp ~/libflashplayer.so $DEST

if test ! -f "${DEST}libflashplayer.so"; then
	echo -e "${RED}Failed"
	echo "Was unable to copy the ~/libflashplayer.so file to $DEST"
	echo "Please try to manually copy this file by running the following command:"
	echo -e "sudo cp ~/libflashplayer.so $DEST$NORMAL"
	exit 1
fi

echo -e "${GREEN}Success$NORMAL"
echo


echo -en "${BLUE}Cleaning up... $NORMAL"
rm ~/libflashplayer.so
rm ~/$FILE
echo -e "${GREEN}Success$NORMAL"
echo


echo -e "${GREEN}Done :-)"
echo -e "You may re-start Firefox now$NORMAL"

