#!@{PERL_PATH} @{WFLAG}
#
# $Id: req-status,v 1.10 2002/03/19 18:54:03 jwise Exp $
#
# remy@ccs.neu.edu
# 28 November 1994
#
# Copyright (C) 1994 by Remy Evard
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.

#
# A copy of the license may be found in docs/license of the source
# distribution.
#

#
# This script was a 1am to 3:30am hack.   Read at your own risk.
#

# reqstat -update   Traverse the whole directory of requests, and then
#                   update the text file in the etc/ directory containing
#                   the stats.
# reqstat           Prints the file of info, which looks like:
#  Active: #        The number of active requests.
#  High: #          Number of high prio.
#  Normal: #        Number of normal prio.
#  Low: #           Number of low prio.
#  Day: #           The number of new requests in 24 hours.
#  ResDay: #        The number of requests resolved in 24 hours.
#  NewResDay:       The number of new requests resolved in 24 hours.
#  Week: #          The number of new requests in 7 days.
#  ResWeek: #       The number of requests resolved in 7 days.
#  NewResWeek: #    The number of new requests resolved in 7 days.
#
# reqstat -active  Prints the active number.
# reqstat -high    etc.
use strict;
no strict "vars";

push (@INC, "@{CODE_LIB_DIR}");

require "ctime.pl";
require "req-config.pl";
require "req-common.pl";

umask(@{RUN_UMASK});
$ENV{"PATH"} = "/bin";

if( -f "$stats_file" ) {
  $last_update = -M "$stats_file";
} else {
  $last_update = 0;
}

$time=time();
$day_seconds = 60 * 60 * 24;
$week_seconds = $day_seconds * 7;

&parse_args();

if($mode eq "update") {
  &update();
  exit(0);
}

($active, $high, $normal, $low, $day, $nrday, $rday, $week, $nrweek, $rweek) 
  = &read_stats();

($active, $high, $normal, $low, $day, $nrday, $rday, $week, $nrweek, $rweek) 
  = &recent_active($active, $high, $normal, $low, $day, $nrday, $rday, $week, $nrweek, $rweek);


#if($mode eq "active") {
#  print "$active\n";
#  exit(0);
#}
#elsif($mode eq "high") {
#  print "$high\n";
#  exit(0);
#}
#elsif($mode eq "normal") {
#  print "$normal\n";
#  exit(0);
#}
#elsif($mode eq "low") {
#  print "$low\n";
#  exit(0);
#}

($active, $high, $normal, $low, $day, $nrday, $rday, $week, $nrweek, $rweek) 
  = &recent_resolved($active, $high, $normal, 
    $low, $day, $nrday, $rday, $week, $nrweek, $rweek);

if($mode eq "day") {
  print "$day\n";
}
elsif($mode eq "nrday") {
  print "$nrday\n";
}
elsif($mode eq "rday") {
  print "$rday\n";
}
elsif($mode eq "week") {
  print "$week\n";
}
elsif($mode eq "nrweek") {
  print "$nrweek\n";
}
elsif($mode eq "rweek") {
  print "$rweek\n";
}
else {
  print "Active: $active\n";
  print "High: $high\n";
  print "Normal: $normal\n";
  print "Low: $low\n";
  print "Day: $day\n";
  print "ResDay: $rday\n";
  print "NewResDay: $nrday\n";
  print "Week: $week\n";
  print "ResWeek: $rweek\n";
  print "NewResWeek: $nrweek\n";
}
  

exit(0);


# ===========================================================================
# Subs
# ===========================================================================

sub recent_active {
  local($active, $high, $normal, $low, $day, $nrday, $rday, $week, 
     $nrweek, $rweek) = @_;
  local(@allfiles);
  local($actday, $actweek);

  #
  # Read about the files that have been active since we did an update.
  #
  @allfiles = &read_new_files($last_update, $active_dir);
  ($h, $n, $l, $d, $w, $ad, $aw) = &scan_files($active_dir, @allfiles);
  
  $active += ($#allfiles + 1);
  $high += $h;
  $normal += $n;
  $low += $l;
  $day += $d;
  $week += $w;

  ($active, $high, $normal, $low, $day, $nrday, $rday, $week, $nrweek, $rweek);
}

sub recent_resolved {
  local($active, $high, $normal, $low, $day, $nrday, $rday, $week, 
     $nrweek, $rweek) = @_;
  local(@allfiles);
  local($actday, $actweek);
  local($h, $n, $l, $d, $w, $ad, $aw);

  #
  # Read about the resolved files that have been active since we did an update.
  #
  @allfiles = &read_new_files($last_update, $resolved_dir);
   ($h, $n, $l, $d, $w, $ad, $aw) = &scan_files($resolved_dir, @allfiles);

  $day += $d;
  $week += $w;
  $nrday += $ad;
  $nrweek += $aw;
  $rday += $d;
  $rweek += $w;

  ($active, $high, $normal, $low, $day, $nrday, $rday, $week, 
    $nrweek, $rweek);
}


sub update {
  #
  # Grunge through all the files out there and update the stats file.
  #
  local(@allfiles);
  local($active, $high, $normal, $low, $day, $week, $actday, $actweek);
  local($a, $h, $n, $l, $d, $ad, $w, $aw);

  @allfiles = &read_files($active_dir);
  $active = $#allfiles+1;
  ($high, $normal, $low, $day, $week, $actday, $actweek) = 
     &scan_files($active_dir, @allfiles);

  @allfiles = &read_files($resolved_dir);
  ($h, $n, $l, $d, $w, $ad, $aw) = &scan_files($resolved_dir, @allfiles);
  
  &dump_stats($active, $high, $normal, $low, $day + $d, $d, $ad,
              $week + $w, $w, $aw);
}


sub dump_stats {
  local($active, $high, $normal, $low, $day, $nrday, $rday, $week,
        $nrweek, $rweek) = @_;

  open(STATS, ">$stats_file") || die "Can't open $stats_file";
  print STATS "Active: $active\n";
  print STATS "High: $high\n";
  print STATS "Normal: $normal\n";
  print STATS "Low: $low\n";
  print STATS "Day: $day\n";
  print STATS "ResDay: $rday\n";
  print STATS "NewResDay: $nrday\n";
  print STATS "Week: $week\n";
  print STATS "ResWeek: $rweek\n";
  print STATS "NewResWeek: $nrweek\n";
  close(STATS);
}

sub read_stats {
  local($active, $high, $normal, $low, $day, $nrday, $rday, $week,
        $nrweek, $rweek) = (0,0,0,0,0,0,0,0,0,0);

  open(STATS, "<$stats_file") || die "Can't open $stats_file";
  while(<STATS>) {
    if(/^Active: (\d+)/) {
      $active = $1;
    }
    if(/^High: (\d+)/) {
      $high = $1;
    }
    if(/^Normal: (\d+)/) {
      $normal = $1;
    }
    if(/^Low: (\d+)/) {
      $low = $1;
    }
    if(/^Day: (\d+)/) {
      $day = $1;
    }
    if(/^ResDay: (\d+)/) {
      $rday = $1;
    }
    if(/^NewResDay: (\d+)/) {
      $nrday = $1;
    }
    if(/^Week: (\d+)/) {
      $week = $1;
    }
    if(/^ResWeek: (\d+)/) {
      $rweek = $1;
    }
    if(/^NewResWeek: (\d+)/) {
      $nrweek = $1;
    }
  }
  close(STATS);

  ($active, $high, $normal, $low, $day, $nrday, $rday, $week, 
  $nrweek, $rweek);
}


sub scan_files {
  #
  #  Scan all the files on the input, getting their priority and their
  #  age.  Then return the totals for categories.
  #   
  local($dir, @files) = @_;
  local($prio, $date, $active);
  local($high, $normal, $low, $day, $week, $age, $act, $actday, $actweek) 
     = (0,0,0,0,0,0,0,0,0);

  file:
  foreach $f (@files) {
    &read_file("$dir/$f");

   $merged = 0;
    while($merged = &get_header("x-request-merged")) {
        next file;
    }

    $prio = &get_header("x-request-priority");
    print ("$prio\n");

    $date = &get_header("x-request-date");
    $active = &get_header("x-request-updated");

    if($prio eq "high") {
      $high++;
    } elsif ($prio eq "low") {
      $low++;
    } else {
      $normal++;
    }

    $age = $time - &getseconds($date);
    $day++ if($age < $day_seconds);
    $week++ if($age < $week_seconds);

    $act = $time - &getseconds($active);
    $actday++ if($act < $day_seconds);
    $actweek++ if($act < $week_seconds);
  }

  ($high, $normal, $low, $day, $week, $actday, $actweek);
}


sub read_new_files {
  local($t, $dir) = @_;
  local(@allfiles, $f);

  opendir(DIR, $dir);
  @allfiles = grep(((-M "$dir/$_") < $t), readdir(DIR));
  @allfiles = grep(/^\d+$/, @allfiles);
  closedir(DIR);

  @allfiles;
}


sub newer_than {
  local($t, $dir, @files) = @_;
  local(@new);

  return @files if(!$t);
  
  foreach $f (@files) {
    if( (-M "$dir/$f") < $t) {
      push(@new, $f);
    }
  }
  @new;
}
  

sub parse_args {
  if($ARGV[0] && ($ARGV[0] =~ /^-(.*)$/)) {
    $mode = $1;
  } else {
    $mode = "none";
  }
}


sub get_file_names {
  # Given a request number, return the name of the associated active file
  # and resolved file.  
  # Don't do any checking for existence, but should probably make sure
  # the files are in reasonable places for security reasons.
  #
  local($num) = @_;
  ("$active_dir/$num", "$resolved_dir/$num");
}
  

sub resolved_request {
  local($file) = @_;
  (-f $file);
}


sub active_request {
  local($file) = @_;
  (-f $file);
}


sub untaint {
  local($foo) = @_;
  $foo =~ /^(.*)$/;
  $1;
}
