#!/bin/sh

DEFAULTBROWSER="lynx \$URL"

BASENAME="`basename $0`"

USAGE="${BASENAME}: incorrect usage.  Please run \"${BASENAME} -h\" for help"

HELP="\
\tusage: ${BASENAME} [-n | -w | -b <browser> | -h ] -x | <URL to open>\n\
\t-n\tOpen up in Netscape\n\
\t-w\tOpen up in Netscape in a new window\n\
\t-b\tOpen up in <browser>\n\
\t-h\tDisplay this help information\n\
\t-f\tOpen up the URL as a local file\n\
\t-x\tOpen up the URL specified in the X cut buffer\n\
\n\
\tThe default browser is set to ${DEFAULTBROWSER}.  It can be changed\n\
\tby editing the DEFAULTBROWSER variable in the \"open\" shell script.\n\
"

BROWSER=${DEFAULTBROWSER}
OPENLOCALFILE="no"

set -- `getopt nwhb:d:fx $*`

if [ $? != 0 ]
then
	echo -e $USAGE >&2
	exit 2
fi

for i in $*
do
	case $i in
		-n)
			shift
			BROWSER="netscape -remote openURL\(\$URL\)"
		;;

		-w)
			shift
			BROWSER="netscape -remote openURL\(\$URL,new-window\)"
		;;

		-b)
			shift
			BROWSER="$1 \$URL"
			shift
		;;

		-d)
			shift
			DISPLAY="$1"
			shift
		;;

		-f)
			shift
			OPENLOCALFILE="yes"
		;;

		-x)
			shift
			XCUTBUFFER="yes"
		;;

		-h)
			shift
			echo -e $HELP >&2
			exit 0
		;;

		--)
			shift
			break
		;;
	esac
done

if [ x"$XCUTBUFFER" == x"yes" ]
then
	URL="`xcb -p 0`"
else
	if [ $# -ne 1 ]
	then
		echo "`basename $0`: incorrect number of arguments" >&2
		echo $USAGE >&2
		exit 1
	fi

	URL=$1
fi

if [ x"$OPENLOCALFILE" = x"yes" ]; then
	if echo "$URL" | grep '^/' > /dev/null
	then
		URL="file://localhost$URL"
	else
		URL="file://localhost`pwd`/$URL"
	fi
fi

export DISPLAY
eval exec $BROWSER
