#!/usr/local/bin/perl5 
#
# 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 'stat.pl';

require 'log_consts.pl';
require 'log_utils.pl';
require 'date_utils.pl';
require 'option_utils.pl';
require 'status_utils.pl';


sub MENTION {
  &MENTION;
  $firstdate=1;
  $scan_types=1;
  $lastdate=1;
}

# Make sure this is consistent with build sums
%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 --usemaps --what ',
);




sub get_period_filenames {
  local($date,$startdate,$enddate)=@_;
  local($nowtime)=time;
  local($today)=&time2timestrs($nowtime);
  local($count,@all_file_names);
  
  return (0,$today) if $date eq 'today';
  @all_file_names=();
  
  $usage=0;
  if ($date) {
    $startdate='';
    $enddate=$today;
  } else {
    push (@all_file_names, $startdate);
    while ($startdate ne $enddate) {
      $startdate = &next_date($startdate);
      push (@all_file_names, $startdate);
    }
    push (@all_file_names, $enddate);
    return (0,@all_file_names);
  }
 
  if ($date eq 'yesterday') {
    $startdate=(&time2timestrs($nowtime-$secondsinday))[0];
    return (0, $startdate);
  } elsif ($date eq 'last-week') {
    $startdate=(&time2timestrs($nowtime-$secondsinday*7))[0];
    push (@all_file_names, $startdate);
    $count = 0;
    while ($startdate ne $enddate && $count < 8) {
      $startdate = &next_date($startdate);
      push (@all_file_names, $startdate);
      $count++;
    }
    push (@all_file_names, $enddate);
  } elsif ($date eq 'last-month') {
    $startdate=(&time2timestrs($nowtime-$secondsinday*30))[0];
    push (@all_file_names, $startdate);
    $count = 0;
    while ($startdate ne $enddate && $count < 31) {
      $startdate = &next_date($startdate);
      push (@all_file_names, $startdate);
      $count++;
    }
    push (@all_file_names, $enddate);
  } elsif ($date eq 'this-month') {
    $startdate=$today; $startdate=~ s/\-..$//;
    push (@all_file_names, $startdate);
  } elsif ($date eq 'this-year') {
    $startdate=$today; $startdate=~ s/\-..\-..$//;
    push (@all_file_names, $startdate);
  } elsif ($date eq 'last-year') {
    ($year,$month,$day) = split('-',(&time2timestrs($nowtime))[0]);
    $endmonth = sprintf("%04d-%02d",$year,$month); 
    $year--;
    $month++;
    $startdate = sprintf("%04d-%02d",$year,$month); 
    push (@all_file_names,$startdate);
    $count=0;
    while ($startdate ne $endmonth && $count < 13) {
      $month++;
      if ($month eq "13") {
	$month="01";
	$year++;
      }
      $startdate = sprintf("%04d-%02d",$year,$month); 
      push (@all_file_names,$startdate);
      $count++;
    }
    push (@all_file_names,$endmonth);
  } elsif ($date =~ 'last (\d+) days') {
    $number=$1;
    $startdate=(&time2timestrs($nowtime-$secondsinday*$number))[0];
    push (@all_file_names, $startdate);
    $count = 0;
    while ($startdate ne $enddate && $count < $number) {
      $startdate = &next_date($startdate);
      push (@all_file_names, $startdate);
      $count++;
    }
    push (@all_file_names, $enddate);
  } else {
     warn "$prog_name: Illegal date period '$date'\n";
     $usage=1;
  }
  
  return ($usage,@all_file_names);
}

sub parse_local_options {
  local(*args)=@_;
  local($usage)=0;
  local($startdate,$enddate,$period);

  $startdate=''; $enddate=''; $period='';
  

  local(@newargs)=();
  while(defined($_=shift(@args))) {
    if(/^--from=(.+)$/) {
      $startdate=&check_date($1);
    } elsif (/^--to=(.+)$/) {
      $enddate=&check_date($1);
    } elsif (/^--period=(.+)$/) {
      $period=$1;
    } else {
      push(@newargs, $_);
      last if /^--$/ || !/^--/;
    }
  }
  @args=(@newargs, @args);
  ($usage, @Retval)=&get_period_filenames($period,$startdate,$enddate);
  return ($usage, @Retval);
}

sub output_loglist {
  local(@dates) = @_;
  

}
# MAIN PROGRAM
#
#
#
#


&set_prog_name;

# Parse options
&handle_config_options(*ARGV);
($usage,@dates)=&parse_local_options(*ARGV);
warn "Options: @dates)\n" if $verbose>3;
$usage=&remove_remaining_options(*ARGV) if !$usage;
##@filelist=&read_default_filelist if !@ARGV && !@filelist;
$usage=1 if !$usage && !@ARGV ; ## && !@filelist;
$prefix = shift(@ARGV);
$usage=1 if @ARGV ;

die <<"EOT" if $usage;
USAGE: $prog_name [config options] [--from=start date] [--to=end date]
       [--period=date period]  output-loglist
EOT

# Any remaining arguments are files to write output in

### &init_output_filenames;
&print_config_info if $verbose>2;

if ($#dates < 0) {
  die "Could not resolve dates\n";
}
if (!@summary_types) {
  warn "$prog_name: No summary types defined in \@summary_types\n";
  exit 0;
}
$sums_path_local = $sums_path;
$sums_path_local =~ s|/$||o;

foreach $sum_type (@summary_types) {
  local($sum_type_msg,$sum_suffix,$sum_program)=
    split(/:/,$sum_type_map{$sum_type});
  local($outfilename)="$prefix.$sum_suffix";
  local(@newdates)=();
  local($date);
  
  open (LOGLIST, ">$outfilename") ||
    die "could not open $outfilename:$!";
  
  @newdates = map {"$sums_path_local/" . $_ . ".$sum_suffix";} @dates;
  foreach $date (@newdates) {
    print LOGLIST "$date\n";
  }
  close(LOGLIST);
}

warn "The output file prefix is $prefix \n" if $verbose>3;
warn "The output dates are @dates\n" if $verbose>3;
exit 0;
