#!perl
# Favourites2Bookmarks                                (c)2001 by JAY Software
#
# WinXX Favourites directory .url files into Netscape bookmarks.html file
# convertor.
# 
# Usage:
# [perl] Favourites2Bookmarks.pl [Path]
#

if ( $#ARGV < 0 ) {
	$favouritesDir = "$ENV{SYSTEMROOT}\\Profiles\\$ENV{USERNAME}\\Favorites";
} else {
	$favouritesDir = $ARGV[0];
}

#print "$#argv $favouritesDir\n";
	
$header  = "<!DOCTYPE NETSCAPE-Bookmark-file-1>\n";
$header .= "<!-- This is an automatically generated file.\n";
$header .= "It will be read and overwritten.\n";
$header .= "Do Not Edit! -->\n";
$header .= "<TITLE>Bookmarks for $ENV{USERNAME}</TITLE>\n";
$header .= "<H1>Bookmarks for $ENV{USERNAME}</H1>\n\n<DL><p>";

$footer = "\n<DD>\n</DL><p>";

print $header;

Favourites2Bookmarks( $favouritesDir, "\n" );
print $footer;

sub Favourites2Bookmarks
{
my( $currpath, $indent ) = @_;
my( @dir ) = ();
my( $pathname ) = ();

    $indent .= "    ";

	if ( opendir( DH, $currpath ) ) {
		@dir = readdir DH;
		closedir DH;

		foreach $filename ( @dir ) {
			if ( ( $filename ne "." ) && 
				 ( $filename ne ".." ) ) {
				$pathname = "$currpath\\$filename";
	
				if ( -d $pathname ) {
				    print $indent . "<DT><H3>$filename</H3>";
				    print $indent . "<DL><p>";
				    print $folder;
					Favourites2Bookmarks( $pathname, $indent );
					print $indent . "</DL><p>";
				} else {
					open FH, $pathname;
					@lines = <FH>;
					close FH;
					$lines = join "", @lines;
					#print $lines;
					$lines =~ m/\[InternetShortcut\]\nURL=([^\n]+)/;
					$linkURL = $1;
					$filename =~ s/\..*$//;
			        print $indent . "<DT><A HREF=\"$linkURL\">$filename</A>";
				}
			}
		}	# foreach

	}	# opendir
}
