#!/bin/sh

tag=bm	# vi tag for the bookmarks file to edit, starting at its add point

pre='	<DT>'	# for <!DOCTYPE NETSCAPE-Bookmark-file-1>
post=

# vibm - convenience kludge to edit a bookmarks file inserting the given URL
# Steve Kinzler, steve@kinzler.com, Feb 15
# https://kinzler.com/me/home.html#web

url="$1"
case "$url" in
'')	if test -t 0
	then	url="`xbuf | head -1`"
	else	read url
	fi;;
esac
qurl="\"$url\""

# see http://is.gd/BaBSVy
title=`wget -qO- "$url" |
       perl -l -0777 -ne 'print $1 if /<title.*?>\s*(.*?)\s*<\/title/is'`

${VISUAL-${EDITOR-vi}} -t "$tag" -c ":a
$pre<A HREF=$qurl>$title</A>$post
." < /dev/tty
