#!/bin/bash
#
# "@(#)dwww:$Id: dwww 511 2009-01-10 23:59:42Z robert $"
#
# 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

[ "$DWWW_USEHTTPS" = "yes" ] && prot="https" || prot="http"

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