#!/bin/bash
#
# "@(#)dwww:$Id: dwww,v 1.12 2003-07-27 16:17:32 robert Exp $"
#
# based on dpkg-www

#
# Initialization
#
. /usr/share/dwww/functions.sh || exit 1
dwww_initialize


www_browser=""
if [ -n "$DISPLAY" ] ; then
	www_browser="$DWWW_X11_BROWSER"
else
	www_browser="$DWWW_BROWSER"
fi

# $BROWSER, if set, overrides our config values
if [ -n "$BROWSER" ] ; then
	# let sensible-browser handle $BROWSER properly
	www_browser=sensible-browser
fi
	

for i in "$www_browser" sensible-browser ; do
	if test -n "$i" && command -v "$i" > /dev/null 2>&1; then
		www_browser="$i"
		break
	fi
done


if [ -z "$www_browser" ] ; then
	echo "$0: no useful browser found" 2>&1
fi

if [ -z "$DWWW_SERVERNAME" ]
then
	DWWW_SERVERNAME=localhost
fi

if [ -n "$DWWW_SERVERPORT" ]
then
	DWWW_SERVERNAME="$DWWW_SERVERNAME:$DWWW_SERVERPORT"
fi

if [ -z "$1" ]
then
	exec "$www_browser" "http://$DWWW_SERVERNAME/dwww/"
else
	uri="`urlencode $1`"
	exec "$www_browser" "http://$DWWW_SERVERNAME/cgi-bin/dwww?search=$uri"
fi
