#!/bin/bash

destdir="/usr/lib/firefox" # default firefox directory
BEGIN="/*** BEGIN EXTRA FIREFOX WIDGETS ***/"
sed_BEGIN="$(echo "$BEGIN" | sed 's@\*@\\*@g')"
END="/*** END EXTRA FIREFOX WIDGETS ***/"
title_bar="========================"
gui=false # if True, some messages will not be displayed 
# since the GUI already displays them.




## Set up vars used in remove() and install() ##
fix_vars()
{
destdir="${destdir%/}" # remove a trailing "/" char from $destdir if present
FILE="$destdir/res/forms.css"
}


# If appended CSS is found in $FILE then run the contents
# of $FILE through a filter which will remove the appended
# CSS and store the remaining (original) css in the variable $temp.
remove_css()
{
grep "$sed_BEGIN" "$FILE" &> /dev/null
if [ "$?" = 0 ] ; then
	delete=false
	temp=$(cat "$FILE" | while read line; do
		if [ "$line" = "$BEGIN" ]; then
			delete=true
		fi

		if [ "$delete" = false ]; then
			echo "$line"
		fi

		if [ "$delete" = true ] ; then
			if [ "$line" = "$END" ] ; then
				delete=false
			fi
		fi
	done)
	unset delete
	echo "Removing the appended CSS code from $FILE"
	sudo bash -c "echo \"$temp\" > \"$FILE\""
	unset temp
else
	echo "The CSS does not appear to be installed."
	echo "$FILE will not be edited."
fi
}

# Delete folder form-widgets and its contents if it exists.
remove_images()
{
if [ -e "$destdir/res/form-widgets" ] ; then
	echo "Deleting $destdir/res/form-widgets"
	sudo rm -r "$destdir/res/form-widgets"
else
    echo "$destdir/res/form-widgets not found.  No action taken."
fi
}


## Install ##

install()
{
fix_vars

echo "Installing widgets to $destdir"

# Remove previous installations of widgets (if any).
echo "Removing any previous installations of the widgets."
remove_images
remove_css

# Install Images
echo "Installing the images to $destdir/res/form-widgets."
sudo cp -r form-widgets "$destdir/res"

# Install CSS
echo "Appending contents of forms-extra.css to $FILE."
sudo bash -c "echo \"$BEGIN\" >> \"$FILE\" ; \
cat forms-extra.css >> \"$FILE\" ; \
echo \"$END\" >> \"$FILE\""

# Display a message if the GUI is not in use.
if [ "$gui" = false ] ; then
	echo "Installation complete.  Please restart Firefox."
fi

return 0
}

## Removal (Uninstall) ##

remove()
{
fix_vars

echo "Removing widgets from $destdir"

remove_images
remove_css

# Display a message if the GUI is not in use.
if [ "$gui" = false ] ; then
	echo "Removal complete.  Please restart Firefox."
fi
return 0
}

show_help()
{
echo -e "Command Line Options"
echo -e "$title_bar\n"
echo -e "-p=path | --path=path\n\tChanges the installation/removal directory to path."
echo -e "\tFor example -p=/usr/local/firefox32 would change the"
echo -e "\tinstallation/removal directory to /usr/local/firefox32."
echo -e "\tBy default the installation/removal directory\n\tis /usr/lib/firefox."
echo -e "-i | --install\n\tBegins installation immediately without showing a menu."
echo -e "-r | --remove\n\tBegins removal immediately without showing a menu."
echo -e "-g | --gui\n\tDisables some messages that are displayed by the graphic installer."
echo -e "-h | --help | --usage\n\tShows this help message."
return 0
}

main_loop=true # by default the main loop is executed
# However some command line arguments can over rule this behaviour
# while main_loop is true, 


# Cycle through command line arguments and do appropriate
# actions.
while [ -n "$1" ] ; do
	case "$1" in
		-p=* )
			destdir="${1#-p=}"
		;;
		--path=* )
			destdir="${1#--path=}"
		;;
		-i | --install )
			main_loop=false
			installation=true
		;;
		-r | --remove )
			main_loop=false
			removal=true
		;;
		-g | --gui )
			gui=true
		;;
		-h | --help | --usage )
			main_loop=false
			show_help
		;;
		* )
			echo "install: unknown option \"$1\"" 
			echo "Try ./install --help for help."
			exit 1
		;;
	esac
	shift
done

while [ "$main_loop" = true ] ; do
	main_loop=false
	echo "1 ) Install Firefox widgets."
	echo "2 ) Remove Firefox widgets."
	echo "3 ) Specify a new installation/removal path."
	echo "4 ) Scroll the help file."
	echo "5 ) Exit."
	echo -n "Select an Option: "
	read option
	case "$option" in
		1 )
			install
		;;
		2 )
			remove
		;;
		3 )
			echo -n "Please enter a new path: "	
			read path
			echo -e "\$destdir = $path\n------------\n"
			destdir="${path}"
			unset path
			sleep 1
			main_loop=true
		;;
		4 )
			less README
			main_loop=true
		;;
		5 )
			exit 0
		;;
		* )
			main_loop=true
			echo -e "Incorrect option!\n"
			sleep 1
		;;
	esac
	echo ""
done

if [ "$installation" = true ] ; then
	install
elif [ "$removal" = true ] ; then
	remove
fi

exit 0
