#!/bin/bash
#
# Ebay companion assistant script
# Jerone Young <jerone.young@canonical.com>
# 
# Copyright (C) 2008 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

FF_BIN=/usr/bin/firefox
FF_USER_DIR=~/.mozilla/firefox
FF_EXENTIONS_DIR=extensions
FF_EBAY_COMP_JS=components/gsEbayUserService.js
EBAY_COMPANION_FOUND=0

#This runs through firefox profile directories that a user has
#and finds if one of them has the Ebay companion installed.
for dir in `find $FF_USER_DIR/* -maxdepth 0 -type d`;
do
	for dir2 in `find $dir/$FF_EXENTIONS_DIR/* -maxdepth 0 -type d`;
	do
		if [ -a "$dir2/$FF_EBAY_COMP_JS" ]; then
			echo "User Has Ebay Companion installed!"
			echo "$dir2/$FF_EBAY_COMP_JS"
			EBAY_COMPANION_FOUND=1
			break;
		fi
	done

	if [ $EBAY_COMPANION_FOUND -eq 1 ]; then
		break;
	fi
done

#now launch firefox if found or install ebay companion into firfox
if [ $EBAY_COMPANION_FOUND -eq 1 ]; then
	$FF_BIN http://www.ebay.com &
else
	$FF_BIN /opt/branding/eBay/firefox_companion_for_ebay-*.xpi &
fi

exit 0
