#!/usr/local/bin/perl5 -w
#
# $Source: /home/cur/djb1/develop/logtools/RCS/build-sums,v $
#
# $Id: build-sums,v 1.13 1995/03/07 12:03:44 djb1 Exp $
#
# Build and store summaries for the given log files, for various summary
# types.
#
# USAGE: build-sums [config options] [--dryrun] [log files...]
#
# (C) Copyright 1994,1995 Dave Beckett <D.J.Beckett@ukc.ac.uk>
# University of Kent at Canterbury
#
# This program is free software; you may distribute under the terms of
# either the GNU General Public License or the Artistic License, as
# specified in the README file.
#

require 'option_utils.pl';
require 'log_utils.pl';
require 'file_utils.pl';
require 'date_utils.pl';


sub MENTION {
  &MENTION;
  $dryrun=1;
  $verbose=1;
  $illegalday=1;
}


%sum_type_map = (
  'count', 'Byte and Access Counts:sumc:sum-counts',
  'institution' , 'Institutions:inst:sum-names --where --institution',
  'site' , 'Sites:site:sum-names --where',
  'email' , 'Email addresses:user:sum-names --who',
  'user' , 'Email addresses:user:sum-names --who',
  'country' , 'Countries:country:sum-names --where --country',
  'files' , 'Files Transferred:name:sum-names --what ',
);


sub unique_list {
  local(@list)=@_;
  local(@newlist)=();
  local(%seen)=();

  foreach $entry (@list) {
    push(@newlist,$entry) if !$seen{$entry};
    $seen{$entry}=1;
  }
  return @newlist;
}


sub do_system {
  local($outfile,$exec)=@_;
  local($rc)=system($exec)>>8;
  if ($rc) {
    unlink($outfile) if $outfile;
    die "$prog_name: system('$exec') returned $rc\n";
  }
}


sub depends {
  local($from, $to)=@_;
  return 1 if !-r $to;
  return -M _ > -M $from;  
}


sub cache_counts {
  local($dryrun,$sum_type,@filenames)=@_;
  local(@dates)=@filenames;

  if (!defined($sum_type_map{$sum_type})) {
    warn "$prog_name: Cannot process unknown summary type '$sum_type'\n";
    return;
  }
  local($sum_type_msg, $sum_suffix,$sum_program)=split(/:/,$sum_type_map{$sum_type});


  warn "$prog_name: Summarizing $sum_type_msg\n" if $verbose>0;

  $sums_path=~s,/$,,;

  if ($output_file_format =~ /daily/) {
    grep($_=&name2date($_),@dates);
  } elsif ($output_file_format =~ /monthly/) {
    grep($_=substr(&name2date($_),0,7),@dates);
  } else {
    die "$prog_name: Cannot (yet) build summaries of year logs\n";
  }

  @dates=sort &unique_list(@dates);

  warn "$prog_name: Seen dates: @dates\n";

  if (length($dates[0]) eq 10) { # IE: /^\d\d\d\d-\d\d-\d\d$/
    warn "$prog_name: Summarizing days: @dates\n";
    local($monthsseen)=();
    foreach $date (@dates) {
      local($year,$month,$day)=split(/-/,$date);
      local($logfn)=&get_uncomp_filename(&get_output_filename($year,$month,$day,$illegaltime));
      local($sumfn)="$sums_path/$date.$sum_suffix";
      if (&depends($logfn, $sumfn)) {
	local($exec)="$sum_program $logfn > $sumfn";
	if ($dryrun) {
	  warn "$prog_name: Would run '$exec'\n";
	} else {
	  &do_system($sumfn, $exec);
	}
	$monthsseen{"$year-$month"}=1;
      }
    }
    @dates=sort keys %monthsseen;
  }

  return if !@dates;

  if (length($dates[0]) eq 7) { # IE: /^\d\d\d\d-\d\d$/
    warn "$prog_name: Summarizing months: @dates\n";
    local($yearsseen)=();
    foreach $yearmonth (@dates) {
      local($year,$month)=split(/-/,$yearmonth);
      local($suminfnsglob)="$sums_path/$year-$month-??.$sum_suffix";
      local($sumfn)="$sums_path/$yearmonth.$sum_suffix";
      local($exec)='';
      if ($output_file_format =~ /monthly/) {
	local($logfn)=&get_uncomp_filename(&get_output_filename($year,$month,$illegalday,$illegaltime));
	if (&depends($logfn, $sumfn)) {
	  $exec="$sum_program $logfn > $sumfn";
	}
      } else {
	$exec="$sum_program $suminfnsglob > $sumfn";
      }
      if ($exec) {
	if ($dryrun) {
	  warn "$prog_name: Would run '$exec'\n";
	} else {
	  &do_system($sumfn, $exec);
	}
	$yearsseen{$year}=1;
      }
    }
    @dates=sort keys %yearsseen;
  }

  return if !@dates;

  if (length($dates[0]) eq 4) { # IE: /^\d\d\d\d$/
    warn "$prog_name: Summarizing years: @dates\n";
    foreach $year (@dates) {
      local($suminfnsglob)="$sums_path/$year-??.$sum_suffix";
      local($sumfn)="$sums_path/$year.$sum_suffix";
      local($exec)="$sum_program $suminfnsglob > $sumfn";
      if ($dryrun) {
	warn "$prog_name: Would run '$exec'\n";
      } else {
	&do_system($sumfn, $exec);
      }
    }
    @dates=("-");
  }

  return if !@dates;

  if ($dates[0] eq '-') {
    warn "$prog_name: Summarizing total\n";
    local($suminfnsglob)="$sums_path/????.$sum_suffix";
    local($sumfn)="$sums_path/total.$sum_suffix";
    local($exec)="$sum_program $suminfnsglob > $sumfn";
    if ($dryrun) {
      warn "$prog_name: Would run '$exec'\n";
    } else {
      &do_system($sumfn, $exec);
    }
  }

}


# MAIN PROGRAM
#
#
#
#

&set_prog_name;

# Parse options
&handle_config_options(*ARGV);
@filelist=&handle_filelist_options(*ARGV);
$usage=&handle_local_flag_options(*ARGV, "dryrun");
$usage=&remove_remaining_options(*ARGV) if !$usage;
@filelist=&read_default_filelist if !@ARGV && !@filelist;
$usage=1 if !$usage && !@ARGV && !@filelist;

die <<"EOT" if $usage;
USAGE: $prog_name [config options] [--dryrun] [log files...]
EOT

# Any remaining arguments are log files
@filelist=(@filelist,@ARGV);

&print_config_info if $verbose>2;

&init_output_filenames;

if (!@summary_types) {
  warn "$prog_name: No summary types defined in \@summary_types\n";
  exit 0;
}

foreach $sum_type (@summary_types) {
  &cache_counts($dryrun,$sum_type,@filelist);
}

exit 0;

# Local Variables:
# mode: perl
# End:
