#!/bin/sh
#

if [ ${#} -ne 1 ] || [ "${1}" = "-H" ]; then
   echo "usage: ${0##*/} URL"
   echo "Start Gnome-configured web browser for URL"
   exit 1
fi

if [ -x /usr/bin/xdg-open ]; then
   exec /usr/bin/xdg-open "${1}"
fi

if [ -x /usr/bin/gconftool-2 ]; then
   GCONFTOOL=/usr/bin/gconftool-2
elif [ -x /opt/gnome/bin/gconftool-2 ]; then
   GCONFTOOL=/opt/gnome/bin/gconftool-2
else
   echo "${0##*/}: unable to find gconftool-2" >&2
   exit 1
fi

if "${GCONFTOOL}" -v > /dev/null; then
   :
else
   echo "${0##*/}: unable to execute ${GCONFTOOL}" >&2
   exit 1
fi

function gconfget {
   "${GCONFTOOL}" --get "${1}" 2> /dev/null
}

CMD=$(gconfget /desktop/gnome/url-handlers/http/command)
NT=$(gconfget /desktop/gnome/url-handlers/http/needs_terminal)
if [ -z "${NT}" ]; then
   NT=$(gconfget /desktop/gnome/url-handlers/http/need-terminal)
fi
EN=$(gconfget /desktop/gnome/url-handlers/http/enabled)
if [ "${EN}" != "true" ]; then
   CMD=
   NT=
fi

if [ -z "${CMD}" ]; then
   CMD=$(gconfget /desktop/gnome/url-handlers/unknown/command)
   NT=$(gconfget /desktop/gnome/url-handlers/unknown/needs_terminal)
   if [ -z "${NT}" ]; then
      NT=$(gconfget /desktop/gnome/url-handlers/unknown/need-terminal)
   fi
   EN=$(gconfget /desktop/gnome/url-handlers/unknown/enabled)
   if [ "${EN}" != "true" ]; then
      CMD=
      NT=
   fi
fi

if [ -z "${CMD}" ] && [ -x /bin/firefox ]; then
   CMD="/bin/firefox %s"
   NT=false
fi

if [ -z "${CMD}" ]; then
   CMD="mozilla %s"
   NT=false
fi

if [ "${CMD%\"%s\"*}" != "${CMD}" ]; then
   # The use of eval and argsify below is designed to protect any special
   # characters (e.g. &) in a URL.  If the CMD also has quotes to protect
   # those characters, it will introduce too many quotes.  So remove them
   # here and rely on the code below.
   CMD="${CMD%\"%s\"*}%s${CMD##*\"%s\"}"
fi

function argsify {
   pre=
   while [ "${#}" -gt 0 ]; do
      if [ "${1%\'*}" != "${1}" ]; then
         echo -n "${pre}\""
         echo -n "${1}" | sed ';s/\\/\\\\\\\\/g;s/\(["$`]\)/\\\1/g'
         echo -n "\""
      elif [ -z "${1}" ] || [ "${1%[][\\\";$&#*\(\)\|<>\{\}~\` 
]*}" != "${1}" ]; then
         echo -n "${pre}'${1}'"
      else
         echo -n "${pre}${1}"
      fi

      pre=" "
      shift
   done

   if [ -n "${pre}" ]; then
      echo
   fi
}

shopt -s execfail

if [ "${NT}" = true ]; then
   TERMINAL_EXEC=$(gconfget /desktop/gnome/applications/terminal/exec)
   TERMINAL_EXEC_ARG=$(gconfget /desktop/gnome/applications/terminal/exec_arg)
   exec "${TERMINAL_EXEC}" "${TERMINAL_EXEC_ARG}" \
                           $(eval printf "\"${CMD}\"" "$(argsify "${1}")")
   exec 2>&1 # bash trashes fd 2 on failed exec; resurrect it
   xmessage -buttons Close "NUU: webbrowser: failed to execute command:
    ${TERMINAL_EXEC} ${TERMINAL_EXEC_ARG} $(eval printf "\"${CMD}\"" "$(argsify "${1}")")
Check root's GNOME Control Center settings under Preferred Applications."
else
   eval exec $(printf "${CMD}\n" "$(argsify ${1})")
   exec 2>&1 # bash trashes fd 2 on failed exec; resurrect it
   xmessage -buttons Close "NUU: webbrowser: failed to execute command:
    $(printf "${CMD}\n" "$(argsify ${1})")
Check root's GNOME Control Center settings under Preferred Applications."
fi
