#!/bin/perl
#
# show history database of ~/.netscape/history.db
#
# Copyright (C) 1999 Taiji Yamada

$history_list = "./history-db-list";

if ($ENV{"SERVER_PROTOCOL"}) {
  print "Content-type: text/html\n\n";
}

$argc = 0;
foreach $argv (@ARGV) {
  $argv =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C",hex($1))/eg;
  $argv =~ s/\\(.)/$1/g;
  $ARGV[$argc++] = $argv;
}

$filter_fgrep = "";
$filter_grep = "";
$filter_egrep = "";
$filter_misc = "";
require 'getopts.pl';
&Getopts('f:g:e:s:');
$filter_fgrep = "|/bin/env LANG=ja /bin/fgrep '$opt_f'" if ($opt_f);
$filter_grep = "|/bin/grep '$opt_g'" if ($opt_g);
$filter_egrep = "|/bin/egrep '$opt_e'" if ($opt_e);
$filter_misc = "|/bin/sort $opt_s" if ($opt_s);

$filters = "$filter_fgrep$filter_grep$filter_egrep$filter_misc";

if ($usr = $ENV{"SCRIPT_NAME"}) {
  $usr =~ s/\/~([^\/]+).*/\1/g;
  $HOME = "/home/$usr";
} else {
  $HOME = $ENV{"HOME"};
}

print <<EOF;
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=euc-jp">
<TITLE>SHOW HISTORY</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000">
<H1>SHOW HISTORY</H1>
<TABLE COLS=1>
EOF

if ($ARGV[0] eq '-') {
  $in = 'STDIN';
} else {
  $in = 'history';
  open($in, "/bin/env HOME=$HOME $history_list$filters|");
}
while (<$in>) {
  ($url,$title) = /([^\t]*)\t([^\t]*)/;
  if ($title !~ /SHOW HISTORY/) {
    print "<TR><TD NOWRAP><A HREF=\"$url\">$title</A> - $url</TR>\n";
  }
}
close($in) if ($in ne 'STDIN');

print <<EOF;
</TABLE>
</BODY>
</HTML>
EOF
