#!/usr/local/bin/perl
#
# this script will check the history-hash.txt
# file in /afs/w3.org/pub/WWW/History/
# and decide wether to regenerate the gdbm file
#
# - Renaud
#
# $Id: build-hash.pl,v 1.5 1999/06/11 17:38:59 renaudb Exp $
#

use GDBM_File;
use Fcntl;
$source = "/afs/w3.org/pub/WWW/History/history-hash.txt";
if($#ARGV != 0){
    print "usage:\n\t$0 output.gdbm\n";
    exit 0;
}

($target)=@ARGV;
if((stat($target))[9] < (stat($source))[9]){
    open(TXT, "$source");
    tie(%DB, 'GDBM_File',$target.".new", O_RDWR|O_CREAT,0644) || die "doh $!";
    reset %DB;
    my $line=0;
    my $rules=0;
    while(<TXT>){
	$var=$_;
	$line++;
	next if /^#/;
	next if /^\s*$/;
	if($var =~ /^(\/[^\t]*)\t+(.*)/){
	    my $orig=$1; my $dest=$2;
	    $rules++;
	    if($DB{$orig}){
		print $orig," is a duplicate entry: line $line\n";
	    } else {
		$DB{$orig}=$dest;
	    }
	} else {
	    print "Parse error line $line:\n\t$var\n";
	} 
    }
    untie(%DB);
    close(TXT);
    #print "$line lines processed, $rules parsed\n";
    rename $target, $target.".old";
    rename $target.".new",$target;
} else {
    #print "Nothing to update\n";
}


