#!/bin/sh
# http://www.macosxhints.com/article.php?story=20040623231530448
#echo firegoogle \"$*\"
#
# this following line makes ' ' into '+'
#echo "$1"
__active=0
__text=0
_SCRIPT_LOCATION=~/bin/
_SCRIPT_NAME=firegoo
#h google script (called firegoo) by duus v 1.0
#h  <google> any string will google "any string"
#
#
#
# help or version information:
if [ "$1" = "h" -o  "$1" = "help" -o  "$1" = "v" -o  "$1" = "version" ]
	then
	grep "^#h" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more
	exit 1;
fi
#
#
if [ "$1" = "a" -o "$1" = "activate" -o  "$1" = "show" -o  "$1" = "s" ]
	then
#h  <a>ctivate or <s>how brings firefox to the front.
	__active=1
	shift
fi
if [ "$1" = "t" -o "$1" = "text" ]
	then
#h  <t>ext runs it in lynx
	__text=1
	shift
fi

#
# construct string.
__searchst=`echo $* | perl -ple "s/ /+/g"`
echo -n "google the following: ${__searchst}"
_URL="http://www.google.com/search?hl=en&q=${__searchst}&btnG=Google+Search"
#
if [ ${__text} -eq 1 ]
	then
	echo "lynx google search (${__searchst}): ${_URL}"
	lynx ${_URL}
	echo "complete: lynx google search (${__searchst}): ${_URL}"
elif [ ${__active} -eq 1 ]
	then
	echo " then bring firefox to the front"
	echo | osascript <<EOF
	tell application "Firefox"
		Activate
	  	Get URL "http://www.google.com/search?hl=en&q=${__searchst}&btnG=Google+Search"
	end tell
EOF
#
	else
	echo " (will be brought up in the background in firefox)"
	echo | osascript <<EOF
	tell application "Firefox"
	  	Get URL "http://www.google.com/search?hl=en&q=${__searchst}&btnG=Google+Search"
	end tell
EOF
#
fi
#
#h   <h>elp,<v>ersion display this.


