#!/bin/sh


# This script is used to find an existing Mozilla
# instance in your system and to supply its path
# to Liferea.

dist_bin=`dirname $0`
params="$@"

run_with_mozilla() {
	MOZILLA_HOME=$1

	if [ -f $MOZILLA_HOME/libgtkembedmoz.so ]; then
	
		LD_LIBRARY_PATH=$MOZILLA_HOME:$LD_LIBRARY_PATH
		export LD_LIBRARY_PATH

		exec "$dist_bin/liferea-bin" $params
	fi
}

look_for_mozilla() {

	results=`ls -1d $1 2>/dev/null | sort -n`
	while [ -n "$results" ]
	do
		path=`echo "$results" | tail -n 1`
		results=`echo "$results" | grep -v $path`
		path=`dirname $path` 	# strip comm.jar
		path=`dirname $path`	# strip chrome
		run_with_mozilla $path
		
	done
}

if [ -n "$MOZILLA_FIVE_HOME" ]; then
	run_with_mozilla $MOZILLA_FIVE_HOME
fi

look_for_mozilla "/usr/lib/mozilla-1.7.13"
look_for_mozilla "/usr/lib/mozilla*/chrome/comm.jar"
look_for_mozilla "/usr/local/mozilla*/chrome/comm.jar"
look_for_mozilla "/opt/mozilla/chrome/comm.jar"
look_for_mozilla "/opt/mozilla/lib/chrome/comm.jar"
look_for_mozilla "/opt/gnome/lib/mozilla/chrome/comm.jar"
look_for_mozilla "/usr/lib/firefox*/chrome/comm.jar"
look_for_mozilla "/usr/local/firefox*/chrome/comm.jar"
look_for_mozilla "/opt/firefox/chrome/comm.jar"
look_for_mozilla "/opt/firefox/lib/chrome/comm.jar"
# for FreeBSD 6:
look_for_mozilla "/usr/X11R6/lib/mozilla/chrome.jar"

echo 'Neither Mozilla nor Firefox is available...'
exec "$dist_bin/liferea-bin" $params
