#!/usr/bin/perl -w

#
# bmgrep -- grep Netscape bookmarks for a regexp pattern
# Kirrily 'Skud' Robert <skud@netizen.com.au>
#
# Version 1.0 1999-09-12
#
# Changelog
# 1999-09-12	Initial version
# 
# Licensing
#
# This script may be modified and redistributed under the same terms
# as Perl itself.
#

use strict;
use Netscape::Bookmarks;
use Getopt::Std;

use vars qw(@names @urls @matches $opt_t $opt_i $opt_f $opt_h);

$opt_t = '';		# to escape "unitialised variable" warnings..
getopts('t:if:h');

help() if $opt_h;

$, = "\n";

my $pattern = $ARGV[0];

my $filename = $opt_f || "$ENV{HOME}/.netscape/bookmarks.html";

my @bookmarks = bookmarks_lol($filename);

print "Found " . scalar(@bookmarks) . " bookmarks.\n";

foreach my $bm (@bookmarks) {
	push(@urls, $bm->[0]);
	push(@names, "$bm->[1] ($bm->[0])");
}


if ($opt_t eq 'name') {
	print match(@names);
} elsif ($opt_t eq 'url') {
	print match(@urls);
} else {
	print match(@urls, @names);
}

print "\n";

sub match {
	if ($opt_i) {
		return grep /$pattern/i, @_;
	} else {
		return grep /$pattern/, @_;
	}
}

sub help {
	print qq(
bmgrep -- grep Netscape bookmarks file

Usage:	bmgrep [options] [pattern]

Options:
t	Type (either url or name, defaults to using both)
i	Case insensitive
f	Bookmarks file to use (defaults to ~/.netscape/bookmarks.html)
h	Displays this help message

Patterns are perl regexps.
);
	exit;
}
