#!/bin/bash
#
# Invoke whatever HTML viewer is installed...
# Usage:
#	htmlview [--remote|--local] URL
#	--remote: We need support for remote (http/ftp) URLs
#	--local: This is a local (doc) html file
#	If neither --remote nor --local is given, it will
#	be determined automatically.
#
# List the tools in order of preference.
#
# written by Bernhard Rosenkraenzer <bero@redhat.com>
# (c) 2000-2001 Red Hat, Inc.
#
# This script is in the public domain.

if [ "$1" == "--remote" ]; then
	REMOTE=1
	shift
elif [ "$1" == "--local" ]; then
	REMOTE=0
	shift
else
	if echo $1 |egrep -q "^(http://|ftp://|www\.|ftp\.)" 2>/dev/null; then
		REMOTE=1
	else
		REMOTE=0
	fi
fi

TERMS_KDE="/usr/bin/konsole /usr/bin/kvt"
TERMS_GNOME="/usr/bin/gnome-terminal"
TERMS_GENERIC="/usr/bin/rxvt /usr/X11R6/bin/xterm /usr/bin/Eterm"
if [ $REMOTE == 1 ]; then
	TTYTOOLS="/usr/bin/links /usr/bin/lynx /usr/bin/w3m"
	X11TOOLS_KDE="/usr/bin/konqueror /usr/bin/kfmbrowser"
	X11TOOLS_GNOME="/usr/bin/galeon /usr/bin/mozilla"
	X11TOOLS_GENERIC="/usr/bin/mozilla /usr/bin/netscape"
else
	TTYTOOLS="/usr/bin/links /usr/bin/lynx /usr/bin/w3m /usr/bin/less /bin/more /bin/cat"
	X11TOOLS_KDE="/usr/bin/khelpcenter /usr/bin/konqueror /usr/bin/khcclient /usr/bin/kdehelp /usr/bin/kfmbrowser"
	X11TOOLS_GNOME="/usr/bin/gnome-help-browser"
	X11TOOLS_GENERIC="/usr/bin/mozilla /usr/bin/netscape"
fi

if [ "x`/sbin/pidof gnome-session`" != "x" ]; then
	X11TOOLS="$X11TOOLS_GNOME $X11TOOLS_KDE $X11TOOLS_GENERIC"
	TERMS="$TERMS_GNOME $TERMS_KDE $TERMS_GENERIC"
else
	X11TOOLS="$X11TOOLS_KDE $X11TOOLS_GNOME $X11TOOLS_GENERIC"
	TERMS="$TERMS_KDE $TERMS_GNOME $TERMS_GENERIC"
fi

if test "x$DISPLAY" = x; then
	for i in $TTYTOOLS; do
		if [ -x $i ]; then
			exec $i $*
		fi
	done
else
	for i in $X11TOOLS; do
		[ -x $i ] && exec $i $*
	done
	for i in $TERMS; do
		if [ -x $i ]; then
			CONSOLE="$i -e"
			break
		fi
	done
	for i in $TTYTOOLS; do
		[ -x $i ] && exec $CONSOLE $i $*
	done
fi

