#!/usr/bin/perl 

###
### sort-netscape-bookmarks.pl  
###
### Usage: sort-netscape-bookmarks.pl [bookmark-files]
###
### Just type this in any directory to alphabetically sort your 
### .netscape-bookmarks.html file. If you provide a list of additional
### bookmark files on the command line, the script will merge these
### into your main bookmark file, keeping only unique bookmarks.
###
### Version 2   / 10-Dec-95 
### Version 2.1 / 12-Apr-96 (compatibility with Netscape 2)
###
### Mark Gerstein 
### http://hyper.stanford.edu/~mbg / mbg@hyper.stanford.edu
###

local($bookmarks) = "$ENV{HOME}/.netscape/bookmarks.html" ; # For Netscape 2 and later
if (!(-s $bookmarks)) {
    $bookmarks = "$ENV{HOME}/.netscape-bookmarks.html" ; # For Netscape Versions Earlier than 2
}

local($date) = `date` ;
chop $date ;

local($prog_title) = "sort-netscape-bookmarks.pl" ;
local($prog_url) = "http://hyper.stanford.edu/~mbg/sort-netscape-bookmarks.html" ;
local($prog_link) = 
    "    <DT><A HREF=\"http://hyper.stanford.edu/~mbg/sort-netscape-bookmarks.html\" ADD_DATE=\"816828556\" LAST_VISIT=\"816828548\">sort-netscape-bookmarks.pl</A>" ;
local($num) = 1;
$list{"$prog_title --- \"$prog_url\""} = $prog_link ;

print STDERR "Reading $bookmarks\n";
open (IN,$bookmarks) || die "Can't open $bookmarks ";

$head = "" ;
$_ = <IN> ; $head .= $_ ; # <!DOCTYPE NETSCAPE-Bookmark-file-1>
$_ = <IN> ; $head .= $_ ; # <!-- This is an automatically generated file.
$_ = <IN> ; $head .= $_ ; #     It will be read and overwritten.
$_ = <IN> ; $head .= $_ ; #     Do Not Edit! -->   !!!!!!!!!! SORRRY 
$_ = <IN> ; $head .= $_ ; # <TITLE>Mark Gerstein's Bookmarks</TITLE>
$_ = <IN> ; $head .= $_ ; # <H1>Mark Gerstein's Bookmarks</H1>
$_ = <IN> ; $head .= $_ ; # <DL><p>

while (<IN>) {
    s/^[ \t]+//g ; 
    if (/<DT><A HREF\=/) {
	$num++ ;
	$_ =~ /<DT><A HREF=([^ ]+) ADD_DATE/; 
	$url = $1 ;
	$_ =~ /ADD_DATE=\"(\d+)\" / ; # "
	$add = $1 ;
	$_ =~ /=\"(\d+)\">([^<]+)<\/A>/ ; # """
	$last = $1 ;
	$title = $2;
	# print OUT "$url $add $visit $title\n" ;
	$sort_key = "$title --- $url" ;
	$list{$sort_key} = $_ ;
    }
}				# 

foreach $a (@ARGV) {
    print STDERR "Merging $a into bookmarks file.\n";
    open (IN,$a) || die "Can't open $a "; 
    while (<IN>) {
	s/^[ \t]+//g ; 
	if (/<DT><A HREF\=/) {
	    $num++ ;
	    $_ =~ /<DT><A HREF=([^ ]+) ADD_DATE/; 
	    $url = $1 ;
	    $_ =~ /ADD_DATE=\"(\d+)\" / ; # "
	    $add = $1 ;
	    $_ =~ /LAST_VISIT=\"(\d+)\">([^<]+)<\/A>/ ; # """
	    $visit = $1 ;
	    $title = $2;
	    $sort_key = "$title --- $url" ;
	    $list{$sort_key} = $_ ;
	}
    }				# 
}

#<DT><A HREF="http://www.well.com/" ADD_DATE="802308377" LAST_VISIT="814767386">The WELL</A>

@keys = keys %list ;
$num_unique = $#keys + 1;
print STDERR "There were a total of $num bookmarks in the input files.\n";
print STDERR "Writing $num_unique unique bookmarks to $bookmarks.tmp\n";
open (OUT,">$bookmarks.tmp") || die "Can't open $bookmarks.tmp " ;

#    <DT><H3 ADD_DATE="816827195">On $date using <A HREF=$prog_url>$prog_title</A></H3>
print OUT <<EOF;
    $head 
    <DT><H3 ADD_DATE="816827195">Sorted alphabetically ($num_unique bookmarks)</H3>
    <DL><p>
    </DL><p>
    <DT><H3 ADD_DATE="816827195">On $date using $prog_title</H3>
    <DL><p>
    </DL><p>
EOF

foreach $_ (sort keys %list) {
    print OUT $list{$_} ;
}

print OUT <<EOF;
</DL><p>
<HR>
    <DT><H3 ADD_DATE="816826796">New bookmarks, not sorted</H3>
    <DL><p>
    </DL><p>
</DL><p>
EOF

print STDERR "$bookmarks --> $bookmarks.bak\n" ;
system "cp $bookmarks $bookmarks.bak" ;
print STDERR "$bookmarks.tmp --> $bookmarks\n" ;
system "mv $bookmarks.tmp $bookmarks" ;





