#!/bin/sh
# Wrapper around xulrunner/firefox to start the xulrunner application conkeror
# Written by Axel Beckert <abe@deuxchevaux.org> for the Debian Project

# Find an appropriate xulrunner binary
XULRUNNER=''

# *sigh* No more xulrunner since iceweasel 30
if [ -x /usr/bin/iceweasel ]; then
    XULRUNNER="/usr/bin/iceweasel -app"
fi


if [ -z "$XULRUNNER" ]; then
    # Due to http://bugs.debian.org/683157 we have to skip xulrunner-14.0
    for version in `LC_ALL=C seq 30 -1 15` `LC_ALL=C seq 13 -1 5` 2 1.9.2 1.9.1; do
        for subversion in $version $version.0; do
            XRTMP=`which xulrunner-$subversion`
            if [ -n "$XRTMP" -a -x "$XRTMP" ]; then
	        XULRUNNER=$XRTMP
	        break 2
            fi
        done
    done
fi

# Try firefox if no xulrunner can be found
if [ -z "$XULRUNNER" ]; then
    FFTMP=`which firefox`
    if [ -n "$FFTMP" -a -x "$FFTMP" ]; then
	echo "xulrunner not found, trying firefox instead." 1>&2
	XULRUNNER="$FFTMP -app"
    fi
fi

if [ -z "$XULRUNNER" ]; then
    echo "xulrunner version 1.9.1 (or iceweasel/firefox version 3.5) or newer required, but not found. Bailing out." 1>&2
    exit 1;
fi

if [ "$*" = "--help" ]; then
    errormsg="`exec $XULRUNNER /usr/share/conkeror/application.ini \"$@\" 2>&1`"
    echo "$errormsg" | sed -e "s:/[^ ]*/xulrunner-bin:$0:; /-\(width\|height\)/d" 1>&2
else
    exec $XULRUNNER /usr/share/conkeror/application.ini "$@"
fi
