#!/usr/bin/perl
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell





## 
## Convert a firefox bookmarks.html file into a elinks native bookmark file
## USAGE:
##	bookmarkconv.pl --firefox-to-elinks||--elinks-to-firefox [--i=infile] [--o=outfile]
## 
## 
##	--firefox-to-elinks	convert from firefox to elinks bookmarks
##	--elinks-to-firefox	convert from elinks to firefox bookmarks
##	--i=infile		select alternative input bookmark file
##	--o=outfile		selecto alternative output bookmark file
## 
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
## 
## firefox bookmark format
## 
## 
## <DL> enter folder
## <DT> entry - URL(<A HREF>), folder(<H3>)
## </DL> exit folder
## 
## 
## 
## elinks bookmark format
## 
## folder \t \t N \t F - N is level of folder root N=0, folder in folder N=1
## bookmark name \t URL \t M - M is level from root M=N+1, root bm M=1, in 1 folder M=2, 2; M=2
## 



use strict;
use warnings;
use Getopt::Long;
use File::Copy;


#sub firefox-to-elinks($);




## find standard elinks/firefox bookmarks to use if
## nothing is specified
my $inputfile = `find $ENV{HOME}/.mozilla/firefox -name 'bookmarks.html'`;
chomp($inputfile);
my $outputfile = "$ENV{HOME}/.elinks/bookmarks";
my $elinks2firefox;
my $firefox2elinks;

my $argret=GetOptions(
		      "i=s" => \$inputfile,
		      "o=s" => \$outputfile,
		      "elinks-to-firefox!" => \$elinks2firefox,
		      "firefox-to-elinks!" => \$firefox2elinks,
		     );


if(defined($firefox2elinks)) {

    print "\nConverting firefox bookmarks: \"$inputfile\"\n".
      "to elinks standard bookmarks: \"$outputfile\"\n";

    my $inputfile = `find $ENV{HOME}/.mozilla/firefox -name 'bookmarks.html'`;
    chomp($inputfile);
    my $outputfile = "$ENV{HOME}/.elinks/bookmarks";

} elsif(defined($elinks2firefox)) {

    print "\nConverting elinks bookmarks: \"$inputfile\"\n".
      "to firefox standard bookmarks: \"$outputfile\"\n";

    my $inputfile = "$ENV{HOME}/.elinks/bookmarks";
    my $outputfile = `find $ENV{HOME}/.mozilla/firefox -name 'bookmarks.html'`;
    chomp($inputfile);

} else {

    die "USAGE:\n" .
	"\tbookmarkconv.pl --firefox-to-elinks||--elinks-to-firefox " .
	  "[--i=infile] [--o=outfile]\n";
}




my $start_of_bm = 1;
my $folder_counter = 0;
my $last_dirlevel=0;
my @line;



## copy current elinks.conf file if it exists
if(-e $outputfile) {
    print "Copying \"$outputfile\" to \"$outputfile.orig\"\n";
    copy($outputfile, "$outputfile.orig")
      or die "Cannot copy $outputfile\n";
}



## open firefox bm to read and elinks bm to write to
open(IN, "$inputfile") or die "cannot open $inputfile: $!";
open(OUT, ">$outputfile") or die "cannot open $outputfile: $!";


## write the HTML header for firefox bookmarks
if(defined($elinks2firefox)) {

    print OUT q|
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
     It will be read and overwritten.
     DO NOT EDIT! -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1 LAST_MODIFIED="1205490430">Bookmarks</H1>

<DL><p>
    <DT><H3 LAST_MODIFIED="1143203504" PERSONAL_TOOLBAR_FOLDER="true" ID="rdf:#$FvPhC3">Bookmarks Toolbar Folder</H3>
<DD>Add bookmarks to this folder to see them displayed on the Bookmarks Toolbar
    <DL><p>
    </DL><p>
    <HR>
|;

}

while(<IN>) {

    if(defined($firefox2elinks)) {

	&firefox2elinks($_);

    } elsif(defined($elinks2firefox)) {

	&elinks2firefox($_);

    }

}


## close folder definitions depending on how 
## many levels the last folder was deep
if(defined($elinks2firefox)) {

    for(my $i=0; $i <= $last_dirlevel; $i++) {
	print OUT "</DL><p>\n";
    }
}

## close opened files
close(IN);
close(OUT);


print "Conversion complete\n\n";



# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # -


sub firefox2elinks($) {

    if(/<HR>/)   { $start_of_bm = 0; }

    ## don't start parsing until past the horizontal rule
    if(!$start_of_bm) {

	## count folders that are moved into and out of
	if(/<DL>/)   { $folder_counter++; }
	if(/<\/DL>/) { $folder_counter--; }



	## match folder title
	if (/<H[0-9]/) {
	    s/.*?<H[0-9].*?>(.*?)<//;
	    print OUT "$1\t\t${folder_counter}\tF\n";
	}


	## match bookmark url
	if (/<A HREF/) {
	    s/.*?<A HREF="(.*?)".*?>(.*?)<//;
	    print OUT "$2\t$1\t${folder_counter}\n";
	}
    }


}




# - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # -


sub elinks2firefox($) {

    ## split line to insert into html format
    @line = split("\t+", $_);


    ## match folder title
    if($line[2] =~ m/F/) {

 	if($line[1] < $last_dirlevel ) { print OUT "</DL><p>\n"; }
	print OUT "<DT><H3>$line[0]</HR>\n" .
			"<DL><p>\n";

	$last_dirlevel = $line[1];
    } else {

	## if the line is not a separator
	if($#line > 2) {
	    if($line[2] < $last_dirlevel ) { print OUT "</DL><p>\n"; }

	} else {
	    print OUT "<HR>\n";
	}

	if($#line > 2) {
	    print OUT "<DT><A HREF=\"$line[1]\">$line[0]</A>\n";

	    ## remember current level in directory
	    if($#line > 2) { $last_dirlevel = $line[2]; }
	}
    }

}
