#<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
#<HTML><HEAD>
#<META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
#<BODY><XMP>
#!c:/perl/bin/perl.exe

# favorites.pl -- Turn an IE "Favorites" hierarchy into a web page
#
# Produce a flattened representation of an IE "Favorites" folder hierarchy.
# Useful for periodic archiving, or for sharing your favorites with others.
#
# Usage:
#	C:> perl favorites.pl > favorites.html
#
# Developed on Win95 using ActiveState perl (http://www.activestate.com/)
#
# Dave W. Smith <dws@postcognitive.com>, 10/6/1998
# http://www.postcognitive.com/tools
# modified by Margaret Fisk <margaretfisk@attbi.com> to support offline processing
# modified 2/2/04 to generate only the body text for buildwebpage.pl
#...my ISP does not support cgi

require 5.003;
#use strict;
use File::Find;
use Time::localtime; 
use Time::Local;
use LWP::Simple;


#getting the date stamp
$tm = localtime; 
($DAY, $MONTH, $YEAR) = ($tm->mday, $tm->mon, $tm->year);
$MONTH=$MONTH+1;
$YEAR=$YEAR-100;
#print "Date is: $MONTH - $DAY - 0$YEAR\n";

#my $page_title = "Favorites";
my $file_urls_ok = 1;   # 1 to generate links for "file://" URLs

# The default favorites folder can be overriden on the command line
my $favorites = $ARGV[0];	# <--- change for NT
my $outfile = $ARGV[1];
my $page_title =$ARGV[2];
my $comment = $ARGV[3];
my $root = shift @ARGV || $favorites;
my @path=split(/\\/,$outfile);
my $url = $path[$#path];

my %seen = ();
my $elide = 0;

find(\&record, $root);
open (APPEND,">$outfile") or die "could not open output file $outfile - $!";
print APPEND "<!--generated with linktest.pl and favorites.pl-->\n";
print APPEND "\t\t\t\t\t\t<table border='0' >\n";
print APPEND "\t\t\t\t\t\t\t<tr>\n";
print APPEND "\t\t\t\t\t\t\t\t\<td colspan=2 valign='center'>$comment<br>Updated on $MONTH - $DAY - 0$YEAR<\/td><\/tr>";

#html_header();
#print $url;
emit_links($url,1);
emit_links($url);
#html_footer();
print APPEND "\t\t\t\t\t<\/table>\n";

# ------------------------------------------------------------------------

# Record each folder and URL in the favorites hierarchy.

sub record {
    my $dir = $File::Find::dir;

    if ( $_ eq "." ) {
        # remember how many chars to trim from directory names
        $elide = length($dir) + 1;
        return;
    }

    if ( -d $_ ) {
        my $path = $dir . "/" . $_;
        $seen{$path} = () if not defined $seen{$path}
    }
    else {
        push @{$seen{$dir}}, $_ if m/\.url$/i;
    }
}


#sub html_header {
#    print <<EOH;
#<html>
#<head><title>$page_title</title>
#</head>
#<body bgcolor=#ffffff>
#<font size="+2"><b>$page_title</b></font>
#EOH
#}


# Emit a flattened representation of the hierarchy

sub emit_links ()
{
my $url=$_[0];
my $type=$_[1];

if($type eq "1")
  {
    my ($dir, $file);
	print "root links";
    foreach $dir ( sort keys %seen ) {
        print "creating links";
	  my @folders=split(/\\/,$dir);
	  my $root_folder = $folders[$#folders];
        my $folder = substr($dir, $elide);
 
        print APPEND "\t\t\t\t<tr>\n";
        print APPEND "\t\t\t\t\t<td width='15' align='left'>&nbsp;<\/td><td align='left'><a href='\#$folder'  class='navlink'>", $folder, "<\/a><br>\n" if $folder ne "";
        print APPEND "\t\t\t\t\t<td colspan='2' align='left'><span class='sectionheader'>", $root_folder, "<\/span><br>\n" if $folder eq "";
	next if not defined $seen{$dir};
		print APPEND "\t\t\t\t\t\t<table><tr><td width='5'><\/td><td>\n";
      print APPEND "\t\t\t\t\t\t<\/td><\/tr><\/table>\n\t\t\t\t<\/td><\/tr>\n";
    }

  }
else
  {
    my ($dir, $file);
	print "root links";
    foreach $dir ( sort keys %seen ) {
        print "creating links";
	  my @folders=split(/\\/,$dir);
	  my $root_folder = $folders[$#folders];
        my $folder = substr($dir, $elide);
        print APPEND "\t\t\t\t<tr>\n";
        print APPEND "\t\t\t\t\t<td width='15' align='left'>&nbsp;<\/td><td align='left'><a name='$folder'><b>", $folder, "<\/b><\/a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='#top'>Back to Top<\/a><br>\n" if $folder ne "";
        print APPEND "\t\t\t\t\t<td colspan='2' align='left'><span class='sectionheader'>", $root_folder, "<\/span><br>\n" if $folder eq "";

	next if not defined $seen{$dir};
		print APPEND "\t\t\t\t\t\t<table><tr><td width='5'><\/td><td>\n";
        foreach $file ( sort @{$seen{$dir}} ) {

            open(IN, "<$dir/$file") || do { warn "$0: $dir/$file: $!"; next };
            undef $/;
            my $stuff = <IN>;
            close(IN);

            my ($url) = $stuff =~ m/url=(.*)/i;
            $url =~ tr|\\|/|;

            my $title = $file;
	    $title = substr($title, 0, -4) if $title =~ m/.url$/i;
#		print APPEND '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		if ($title=~m/badlink/ig)
			{}
		else
			{
	            if ( $url =~ m/^file:/i ) {
      	          print APPEND "\t\t\t\t\t\t\t".'<a href="', $url, '" class="navlink" target="_blank"><i>', $title, "</i></a><br>\n" if $file_urls_ok;
            	} else {
	                print APPEND "\t\t\t\t\t\t\t".'<a href="', $url, '" class="navlink" target="_blank">', $title, "</a><br>\n";
			}
            }
        }
      print APPEND "\t\t\t\t\t\t<\/td><\/tr><\/table>\n\t\t\t\t<\/td><\/tr>\n";
    }
  }
}


#sub html_footer {
 #   print <<EOF;
#</body>
#</html>
#EOF
#}
#</XMP></BODY></HTML>
