#!/bin/sh -

grep="${GREP-grep}"

# Note: with Firefox 3 you need to set browser.bookmarks.autoExportHTML true
#	in about:config to get bookmarks.html
bookmark="${BOOKMARK-$HOME/etc/bookmarks.html}"
htmlview="${HTMLVIEW-webrowse -s}"

beghtml='<dl>'
endhtml='</dl>'

# grepbm - convenience kludge to grep through browser bookmarks
# Steve Kinzler, steve@kinzler.com, Aug 98
# https://kinzler.com/me/home.html#web

title=`echo "$0 $*" | sed 's/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g'`

view=; bad=

case "$1" in
-b)	view=t; shift;;
-h)	bad=t;  shift;;
esac

case "$bad" in
?*)	cat << EOF 1>&2
usage: $0 [ -b ] [ $grep options ] regexp
	-b	view the output in a web browser ($htmlview)
EOF
	exit 1;;
esac

case "$view" in
'')	$grep "$@" "$bookmark" | grep -v '^<!--'; exit $?;;
esac

(cat <<EOF
<html><head>
<title>$title</title>
</head><body>
$beghtml
EOF

$grep "$@" "$bookmark" | grep -v '^<!--'

cat <<EOF) | $htmlview
$endhtml
</body></html>
EOF
