#!/opt/perl/bin/perl
$vernum = "1.0 - 04 MAR 99" ;           # Code version and modify date
#
# Compare the features of real interest between two Netscape bookmark files.
# (Compare the current bookmark file with the one named as an argument.)
# (It has been used with Netscape 3.x and 4.x, but may not work with
# future versions.)
#
# This code copyright 1999 by
# D. W. Eaton, Artronic Development, Phoenix, AZ -- dwe@arde.com
#
# This software is made freely available under the provisions of the Perl
# "Artistic" license:  http://language.perl.com/misc/Artistic.html
#
# This code is not supported and is not warranteed to perform any particular
# function. Contact dwe@arde.com for aditional information.
# If you find bugs or make enhancements, it would be appreciated if you
# sent them on to the author at dwe@arde.com.
#
require "ctime.pl" ;
#
$true = 1;  # truth values
$false = 0;
# ---------------------------------------------------
# Configurable values:
$debugme = $false;            # Set to TRUE to just re-name old file
$noleadspace = $true;         # Set to TRUE to ignore leading whitespace
$currentfile = "$ENV{'HOME'}/.netscape/bookmarks.html"; # Current bookmark file
# ---- end normal configuration items --------------------
# Initialize:
$errmsg = '' ;

# --------------------------- logic ----------------------------------
# Get any command-line arguments that may have been passed (1):
$numargs = scalar (@ARGV) ;
if ($numargs == 1)
{
 $thisfile = shift (@ARGV) ;
}
else
{
 print "Error: should have 1 argument, found $numargs\n";
 exit (1);
}
# Got all agruments now ....
#
# Get time of run
$submitdate = time ;
$submitdate = ctime($submitdate) ;
$submitdate =~ s/\n//g ;
#
# ---------------- read file -----------------
#
$oldcontent = &getmycontent ("$currentfile");
$newcontent = &getmycontent ("$thisfile");

if (! $oldcontent)
{
 $errmsg .= "No significant content found in '$oldcontent'\n";
}
if (! $newcontent)
{
 $errmsg .= "No significant content found in '$newcontent'\n";
}

# If there were any error messages, display them all and quit.
if ($errmsg)
{
 &err_msg ("$errmsg\n") ;
 print "Aborting bookmark compare\n";
 exit (1) ;
}
else
{
 # --------both files read - save for compare---------
 $oldtmp = "/tmp/bookmrk.old";
 $newtmp = "/tmp/bookmrk.new";
 $errmsg .= &savemycontent ("$oldcontent\n",$oldtmp);
 $errmsg .= &savemycontent ("$newcontent\n",$newtmp);

 # If there were any error messages, display them all and quit.
 if ($errmsg)
 {
  &err_msg ("$errmsg\n") ;
  &cleanup;
  print "Unable to do bookmark compare\n";
  exit (1) ;
 }
 else
 {
  # Compare the two files
  print "Compare of:\n\<'$currentfile'\n\>'$thisfile'\n";
  system ("diff $oldtmp $newtmp");
 }
 &cleanup;
} 
#
exit (0) ;
### END.

# --------------------------- subroutines ----------------------------
#
# Tell user that an error has occurred
sub err_msg
{
   local ($msg) = @_ ;

   print "Error(s) found by $0 version $vernum:\n";
   print "$msg\n" ;
}
#-------------------------------
# Get selected content from the specified file
#  $thecontent = &getmycontent ("the/file/path");
# Passes back errors appended to the global $errmgs variable
sub getmycontent
{
 my ($filepath) = @_ ;

 if (! -f $filepath)
 {
  $errmsg .= " Unable to find file '$filepath'\n";
 }
 elsif (open (MYFILE, "<$filepath"))
 {
   $lineno = 0;
   $content = ""; # Initialize to no content
   while (defined ($line = <MYFILE>))
   {
    $lineno++;
    if ($lineno == 1)
    {
     # See if this is a proper bookmark file
     if (! $line =~ /^\<\!DOCTYPE/)
     {
      $errmsg .= " File '$filepath' was not a bookmark file\n";
      return (0);
     }
    }
    # Look for stuff we don't care to compare:
    if ($line =~ / ADD_DATE=\"\d+\"/)
    {
     # Delete that stuff
     $line =~ s/ FOLDED ADD_DATE=/ ADD_DATE=/g;
     $line =~ s/ ADD_DATE=\"\d+\"//g;
    }
    if ($line =~ /LAST_VISIT=\"\d+\"/)
    {
     # Delete that stuff
     $line =~ s/LAST_VISIT=\"\d+\"//g;
    }
    if ($line =~ /LAST_MODIFIED=\"\d+\"/)
    {
     # Delete that stuff
     $line =~ s/LAST_MODIFIED=\"\d+\"//g;
    }
    if ($noleadspace)
    {
     $line =~ s/\s+//;
    }
    # Pass through what's left:
    $content .= "$line";
   }
   close (MYFILE);
 }
 else
 {
   $errmsg .= " (can't open file '$filepath')" ;
 }
 return ($content);
}
#-------------------------------
# save identified content to the specified file
#  $errormessages = &savemycontent ("the content","the/file/path");
# (return is null if no errors)
sub savemycontent
{
 my ($thecontent,$filepath) = @_ ;
 my ($myerrmsg);

 $errmsg = "";
 if (open (MYFILE, ">$filepath"))
 {
   print MYFILE "$thecontent";
   close (MYFILE);
 }
 else
 {
   $myerrmsg .= " (can't open file '$filepath')" ;
 }
 return ($myerrmsg);
}
#---------------------
# Cleanup before exit
sub cleanup
{
  if ($debugme)
  {
   print "See files '$oldtmp' and '$newtmp'\n";
  }
  else
  {
   # Remove the temp files
   if ($oldtmp && -f $oldtmp)
   {
    unlink ($oldtmp);
   }
   if ($newtmp && -f $newtmp)
   {
    unlink ($newtmp);
   }
  }
}
#---------------------
