#!/bin/bash
# Steven Maresca
# lightyear4 @t gmail
#
# checks to see if firefox is running.
# if so, loads url/file in new window.
# if not, loads firefox with url/file.
# 
# made to as a workaround to that 
# annoying profile manager issue
# when multiple programs load an url
#
# Current version of this script (and others) always available
# at http://www.fugitivethought.com/projects/shell-scripts/
#
# start firefoxcheck script

if [ $# -eq 0 ]; then

	echo "--------------------------------------------------" 1>&2
	echo "This script is for dumb programs that dont check  " 1>&2
	echo " for an already running firefox thread. Checks to " 1>&2
	echo " see if firefox is already running. If so, loads  " 1>&2
	echo " url/file in new window or tab (specified via     " 1>&2
	echo " command line arguments, see below). If not       " 1>&2
	echo " running, spawns new process with url/file. Gets  " 1>&2
	echo " around the issue of the profile manager          " 1>&2
	echo " complaining.                                     " 1>&2
	echo "--------------------------------------------------" 1>&2
	echo "Usage:                                            " 1>&2
	echo " $> firefoxcheck new-{tab|window} \"URL\"         " 1>&2
	echo "--------------------------------------------------" 1>&2
	exit 0
fi

if [ "$2" = "" ]; then
	firefox -remote "openurl($1,new-window)" && exit 0
	firefox $1
fi

case "$1" in
	new-tab)
		firefox -remote "openurl($2,new-tab)" && exit 0
		
		# if we get this far, spawn firefox
		# (its not already running)
		firefox $2 &
	;;
	new-window)
		firefox -remote "openurl($2,new-window)" && exit 0
	
		# if we get this far, spawn firefox
		# cuz it aint runnin
		firefox $2 &
	;;
esac

# end script
