##############################################################################
# Perl Script to convert a directory tree of URL files to an html file
# Purpose: create an html file image of Windows Favorites
# readable by browsers other than Microsoft Internet Explorer.
# Note: Information about last access time is trashed. Who cares, anyway...
#
# Written by David Binard, 12-Jul-97
# mailto:binard@california.com
#
# History:
# 12-Jul-97     Original
# 10-Nov-97	Added a while loop to make sure we disregard the last line
#		added by IE4.0 in the URL file format about last access time.
##############################################################################

my $rlvl = 0; # recursion level

# Get command-line args.
$Input = $ARGV[0];
shift(@ARGV);
$Output = $ARGV[0];

# Do we have enough args?
if ($Input eq "" | $Output eq "")
{
        Usage();
}

shift(@ARGV);
$title = $ARGV[0];
if ($title eq "")
{
        $title = "$Input";
}

$header = "<!DOCTYPE NETSCAPE-Bookmark-file-1>";
$header = "$header\n<!-- This is an automatically generated file.";
$header = "$header\nIt will be read and overwritten.";
$header = "$header\nDo Not Edit. Hack hack hack...! -->";
$header = "$header\n<TITLE>$title</TITLE>";
$header = "$header\n<H1>$title</H1>\n\n";

# Open the files
open(OUT,">$Output") || die("Unable to create $Output\n");

print OUT $header;
ProcessDir ($Input);

close(OUT);

# Usage banner
# Called if user does not provide adequate command-line options
sub Usage
{
        print "\n$0: converts a directory tree to a Netscape bookmark html file.";
        print "\nUsage: ";
        print "perl $0 <directory> <html file> [\"title\"]\n";
	die("\n");
}

sub ProcessDir {
   my @filenames;
   my @files;
   my @dirs;
   my $url = "";
   my $urltitle = "";
   my $indent = "";
   $rlvl++;
   for (2 .. $rlvl) { $indent="$indent   "; }
   chdir("@_") || die("Can't chdir to @_: $!\n");
   opendir(DIR,"@_") || die ("Can't open @_: $!\n");
   @filenames = sort(readdir(DIR));
   foreach (@filenames) {
      if ($_ ne "." && $_ ne "..") {
         if (-d $_) {
            @dirs = (@dirs, $_);
         }
         else {
            @files = (@files, $_);
         }
      }
   }
   print (OUT "$indent<DL><p>\n");
   foreach (@dirs) {
      print (OUT "$indent<DT><H3 ADD_DATE=\"0\">$_</H3>\n");
      ProcessDir("@_\\$_");
   }
   $indent = "";
   $rlvl++;
   for (2 .. $rlvl) { $indent="$indent   "; }
   foreach (@files) {
      if (/\.url$/ || /\.URL$/) {
         $urltitle = substr($_,0,length($_)-4);
         open(URL, "@_\\$_") || warn("Unable to open $_\n");
         while (<URL>) {
            if (/^URL=/ || /^url=/) {
               $url = "$_";
            }
         }
         chomp($url);
         $url = substr($url,4);
         print (OUT "$indent<DT><A HREF=\"$url\" ADD_DATE=\"0\" LAST_VISIT=\"0\" LAST_MODIFIED=\"0\">$urltitle</A>\n");
         close(URL);
      }
      else {
         print (OUT "$indent<DT><A HREF=\"file://@_\\$_\" ADD_DATE=\"0\" LAST_VISIT=\"0\" LAST_MODIFIED=\"0\">$_</A>\n");
      }
   }
   $indent = "";
   $rlvl--;
   for (2 .. $rlvl) { $indent="$indent   "; }
   print (OUT "$indent</DL><p>\n");
   closedir(DIR);
   $rlvl--;
}
