#!/bin/sh
# Launch SliTaz default Web browser.
#

. /lib/libtaz.sh
export TEXTDOMAIN='slitaz-tools' #i18n

. /etc/slitaz/applications.conf

USER_CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/slitaz/applications.conf"
[ -f  $USER_CONFIG ] && . $USER_CONFIG

EXEC="exec setsid $BROWSER"

[ "$(which $BROWSER)" ] || case "$1" in
file://*)
	exec xterm -e "sh -c \"sed 's/<[^>]*>//g;s/&lt;/</g;s/&gt;/>/g;s/&amp;/\\&/g' < ${1#file://} | uniq | less\""
esac

# No browser installed (a desktop built from a minimal flavor): say so
# and offer the package manager instead of failing silently.
if [ ! "$(which $BROWSER)" ]; then
	msg="$(_ 'No web browser is installed.')"
	hint="$(_ 'Find one with: spk-search browser')
$(_ 'Install it with: spk --add NAME')"
	if [ -z "$DISPLAY" ]; then
		echo "$msg"
		echo "$hint"
	elif which yad >/dev/null && which spk-gui >/dev/null; then
		yad --title="$(_ 'Web browser')" --window-icon=web-browser \
			--image=web-browser --center --width=360 \
			--text="$msg
$(_ 'Open the package manager to install one?')" \
			--button="$(_ 'Cancel'):1" \
			--button="$(_ 'Install a browser'):0" &&
		exec spk-gui browser
	elif which yad >/dev/null; then
		yad --title="$(_ 'Web browser')" --window-icon=web-browser \
			--image=web-browser --center --width=360 \
			--text="$msg
$hint" --button="OK:0"
	else
		xmessage -center "$msg
$hint"
	fi
	exit 1
fi

if [ -z "$XAUTHORITY" ]; then
	# console session
	EXECT="$BROWSER"
else
	# Xorg session
	case $BROWSER in
		links) EXECT="$EXEC -g" ;;
		*)     EXECT="terminal -e $BROWSER" ;;
	esac
fi

case $BROWSER in
	*links*|retawq|lynx) $EXECT    $@ ;;
	w3m)                 $EXECT -N $@ ;;
	qupzil*)             $EXEC -ow $@ ;;
	*)                   $EXEC     $@ ;;
esac

exit 0
