#!/bin/bash
#
#
# This script attempts to be the ultimate url opening script for UZBL. It
# hopes to do this in the following way. It presents all surfraw bookmarks
# and elvi in a dmenu, and uses surfraw to obtain a valid URL. If the first
# word entered by the user (and thus returned by dmenu) contains a colon (:)
# or a period (.), it assumes that the user entered a valid URL and just
# immediately prints that, bypassing surfraw.
#
# As such, the script itself is stupid, and the awesomeness comes from dmenu
# and surfraw.
#
# Example UZBL configuration:
#
# To open any url in the current UZBL instance:
# bind    o         = sh 'uri=`$XDG_DATA_HOME/uzbl/scripts/uzbl_load_url_from_surfraw` && echo "uri $uri" > $4'
# To open any url in a new UZBL instance:
# bind    t         = sh 'uri=`$XDG_DATA_HOME/uzbl/scripts/uzbl_load_url_from_surfraw` && uzbl --uri $uri'
#
#
# October 2009, Sumant Oemrawsingh
#
# TODO: Do we also want history? Titles of webpages and urls? Tags for bookmarks?

# Default search engine. If we really are at a loss, use this
DEFAULT_SEARCH=google

# The location of the surfraw bookmark file
if [ -n "$XDG_CONFIG_HOME" -a -e "$XDG_CONFIG_HOME/surfraw/bookmarks" ]; then
    BOOKMARKS="$XDG_CONFIG_HOME/surfraw/bookmarks"
elif [ -e "$HOME/.config/surfraw/bookmarks" ]; then
    BOOKMARKS="$HOME/.config/surfraw/bookmarks"
else
    BOOKMARKS="$HOME/.surfraw.bookmarks"
fi

# Colors for dmenu
COLORS=" -nb #303030 -nf khaki -sb #CCFFAA -sf #303030"

# Prompt for dmenu
PROMPT="Open"

# From load_url_from_bookmarks.sh: Use dmenu with vertical patch if available
if dmenu --help 2>&1 | grep -q '\[-rs\] \[-ni\] \[-nl\] \[-xs\]'
then
	DMENU="dmenu -i -xs -rs -l 10" # vertical patch
else
	DMENU="dmenu -i"
fi

# Use surfraw to search for the words
function search()
{
  # Does surfraw know what to do with it?
  url=`surfraw -print $@`

  # If not, then use the default search engine
  if [ $? -ne 0 ]
  then
    url=`surfraw -print $DEFAULT_SEARCH $@`
  fi

  echo $url

}

# We assume that this is a URL
function goto()
{
  case "$1" in
    *:*) echo $1 ;;
    *) echo "http://$1" ;;
  esac
}

# Use dmenu to navigate through possible choices
function present_menu()
{
  elvi=`surfraw -elvi | grep -v -e ' ELVI:' -e '^W' | mawk '{print $1}'`
  if [ -r "$BOOKMARKS" ]
  then
    elvi="$elvi $(mawk '{print $1}' "$BOOKMARKS" 2>/dev/null)"
  fi

  echo "$elvi" | tr ' ' '\n' | sort | $DMENU -p "$PROMPT" $COLORS
}

present_menu | \
  ( read car cdr
    test -z "$car" && exit 1
    ( test -z "$cdr" && echo $car | fgrep -c '.
:' > /dev/null && goto $car ) || search "$car $cdr" )
