#!/usr/local/bin/perl -s
#
# list history database of ~/.netscape/history.db with Berkeley UNIX DB
#
# Copyright (C) 1999 Taiji Yamada

%month = ('Jan','1', 'Feb','2', 'Mar','3', 'Apr','4', 'May','5', 'Jun','6', 'Jul','7', 'Aug','8', 'Sep','9', 'Oct','10', 'Nov','11', 'Dec','12');

sub timestamp {
  local($time) = @_;
  $time = localtime($time);
  local($mon,$mday,$time,$year) = $time
      =~ /^\S+\s+(\S+)\s+(\d+)\s+(\d+:\d+:\d+)\s+(\d+)$/;
  sprintf("%4d/%02d/%02d %s", $year,$month{$mon},$mday,$time);
}

$history = $ENV{"HOME"}."/.netscape/history.db";

use Fcntl;
use DB_File;
tie %history, DB_File, $history, O_RDONLY;

foreach $key (keys %history) {
  next if ($key !~ /^(http|ftp|file):[ -~]+\0$/i);
  ($last,$first,$count,$expire,$title) = unpack("LLLLa*", $history{$key});
  $url = $key;
  if ($l || $f || $e || $c || $a) {
    $last = timestamp($last);
    $first = timestamp($first);
    $expire = timestamp($expire);
  }
  $url =~ s/\0$//;
  $title =~ s/\0$//;
  next if (!defined($T) && $title eq "");
  print "$url\t$title";
  print "\t$last" if ($l || $a);
  print "\t$first" if ($f || $a);
  print "\t$expire" if ($e || $a);
  print "\t$count" if ($c || $a);
  print "\n";
}
