#! /usr/local/bin/perl 

'di';
'ig00';


$ENV{'PATH'} = '.:/www/bin:/www/sbin:/usr/local/bin:' .
  '/usr/bin/X11:/usr/local/etc:' .
  '/usr/local/emacs/etc:/proj/bin:/usr/new:/usr/ucb:/bin:' .
  '/usr/bin:/usr/etc:/etc:/usr/games:/usr/hosts';

($MYNAME     = $0) =~ s|.*/||o;

# $RCSHEADER = '$Header: /users/pilgrim_staff/srivasta/bin/graph-log.pl,v 1.2 1995/06/08 23:02:15 srivasta Exp srivasta $'; #';

  
'$Revision: 1.2 $'  =~ /^\$\w+:\s+([.1234567890]+)\s+\$$/;	#';
$VERSION = $1;

require 5.001;

# Synopsis:
$Usage = <<"END_OF_USAGE";
 $MYNAME $VERSION
usage: $MYNAME [options] [filename]
  [--help]                    This message
  [--type=per_hour|per_day|per_month|date|month|year] Type of graph needed
  [--otype=count|bytes|both]  Type of output needed
  [--prefix=<file-prefix>]    Output file  prefix
  [--title=<Title of plot>]   A general title for all plots
  [--[no]total]               [do not] output totals
  [--[no]ftp]                 [do not] output ftp data
  [--[no]gopher]              [do not] output gopher data
  [--[no]http]                [do not] output http  data
END_OF_USAGE
  ;
# >> BEGIN parse options >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #

$opt_help = 0;			# shuts off a warning
$opt_total = 1;
$opt_ftp = 1;
$opt_gopher = 1;
$opt_http = 1;
$opt_type = 'per_hour';
$opt_otype = 'both';
$Prefix = 'graph-out';

use Getopt::Long;
$result = GetOptions ('help', 'type=s', 'otype=s', 'prefix=s',
		      'title=s', 'total!', 'ftp!', 'gopher!','http!');
		      

# << END parse options <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #

# >> BEGIN validate options >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #

if (!$result) {
  die "Error Parsing options:\n$Usage";
}
if ($opt_help) {
  die "$Usage";
}
if ($opt_type) {
  die "Error in type[$opt_type]:\n$Usage" unless 
    $opt_type =~ m/^(?:per_hour|per_day|per_month|date|month|year)$/o;
}
if ($opt_otype) {
  die "Error in output type[$opt_otype]:\n$Usage"  unless 
    $opt_otype =~ m/^(?:count|bytes|both)$/o;
} 

if ($opt_otype eq 'count') {
  $Out_count = 1;
  $Out_bytes = 0;
} elsif ($opt_otype  eq 'bytes') {
  $Out_bytes = 1;
  $Out_count = 0;
} else {
  $Out_count = 1;
  $Out_bytes = 1;
}
if ($opt_prefix) {
  $Prefix = $opt_prefix;
} 

$Output_all_cnt = $Prefix . "-all-cnt.dat";
$Output_ftp_cnt = $Prefix . "-ftp-cnt.dat";
$Output_gph_cnt = $Prefix . "-gph-cnt.dat";
$Output_www_cnt = $Prefix . "-www-cnt.dat";


$Output_all_byt = $Prefix . "-all-byt.dat";
$Output_ftp_byt = $Prefix . "-ftp-byt.dat";
$Output_gph_byt = $Prefix . "-gph-byt.dat";
$Output_www_byt = $Prefix . "-www-byt.dat";

$Output_gnuplot = $Prefix . ".gnu";

# << END validate options <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #

# >> BEGIN parse arguments >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #

# << END parse arguments <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #

# >> BEGIN validate arguments >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #

# << END validate arguments <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #

# >> BEGIN initialization >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #
%Daysinmonth = (
		1 => 31,
		2 => 28,
		3 => 31,
		4 => 30,
		5 => 31,
		6 => 30,
		7 => 31,
		8 => 31,
		9 => 30,
		10 => 31,
		11 => 30,
		12 => 31
	       );
%LeapDaysinmonth = (
		1 => 31,
		2 => 29,
		3 => 31,
		4 => 30,
		5 => 31,
		6 => 30,
		7 => 31,
		8 => 31,
		9 => 30,
		10 => 31,
		11 => 30,
		12 => 31
	       );

%Ticlevels = (
	      .0277777777777778 => "10",
	      .0555555555555556 => "20",
	      .0833333333333333 => "Jan",
	      .111111111111111 => "10",
	      .138888888888889 => "20",
	      .166666666666667 => "Feb",
	      .194444444444444 => "10",
	      .222222222222222 => "20",
	      .25 => "Mar",
	      .277777777777778 => "10",
	      .305555555555556 => "20",
	      .333333333333333 => "Apr",
	      .361111111111111 => "10",
	      .388888888888889 => "20",
	      .416666666666667 => "May",
	      .444444444444444 => "10",
	      .472222222222222 => "20",
	      .5 => "Jun",
	      .527777777777778 => "10",
	      .555555555555556 => "20",
	      .583333333333333 => "Jul",
	      .611111111111111 => "10",
	      .638888888888889 => "20",
	      .666666666666667 => "Aug",
	      .694444444444444 => "10",
	      .722222222222222 => "20",
	      .75 => "Sep",
	      .777777777777778 => "10",
	      .805555555555556 => "20",
	      .833333333333333 => "Oct",
	      .861111111111111 => "10",
	      .888888888888889 => "20",
	      .916666666666667 => "Nov",
	      .944444444444444 => "10",
	      .972222222222222 => "20",
	     );

%TiclevelsMonth = (
	      .0833333333333333 => "Jan",
	      .166666666666667 => "Feb",
	      .25 => "Mar",
	      .333333333333333 => "Apr",
	      .416666666666667 => "May",
	      .5 => "Jun",
	      .583333333333333 => "Jul",
	      .666666666666667 => "Aug",
	      .75 => "Sep",
	      .833333333333333 => "Oct",
	      .916666666666667 => "Nov",
	      );


## >> BEGIN subroutine: tmpname >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ##

# Subroutine:
#     tmpname
#
# Description:
#       Like the tmpname(2) call, returns the name of a unique file.
# Usage:
#     sname (arg)
#
# Return:
#     filename guaranteed to be unique
#
# Dependencies:
#
# Bugs:
#

sub tmpnam
{
    local ($file) = "$$000000";
    while (stat("/tmp/$file")) { $file++; }
    "/tmp/$file";
}

## << END subroutine: tmpname <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ##

# << END initialization <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #

# >> BEGIN verify prerequisites >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #

# << END verify prerequisites <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #

# >> BEGIN main >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #


# << END main <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #
undef %YearsSeen;

while (<>) {
  chop;

  if (/^fields/o || /^scheme/o) { # whther it is a pretty format or not
    ($linefmt = $_ ) =~ s/-/_/og;
    @linefmt = split (' ', $linefmt);
    while ($tmp = shift @linefmt) {
      $arr .= '$' . $tmp . ', '; #';
      $mtch .= '(\S+)\s*';
      
    }
    chop($arr);
    chop($arr);
    chop($mtch);
    chop($mtch);
    chop($mtch);
    $evalstr = "($arr) = m/^$mtch/o;";
#    print STDERR "[$evalstr]\n";
    next;
  }
  next unless m/^\s*$opt_type/;
  eval "$evalstr";
  warn "$@" if $@;
#  eval "print STDERR \"$_\\n\";";
#  eval "print STDERR \"$arr\\n\";";
  

  if ($opt_type eq 'per_hour') {
    if ($value =~ /:/) {
      $value =~ s/://o;		# a bug in log format?
      $value = sprintf ("%02d", $value);
    }
  }

  if ($opt_type eq 'date') {
    ($year,$month,$date) = $value =~ m|(\d+)-(\d+)-(\d+)|o;

    if (($year == (4 * int ($year / 4.0))) &&
        ($year != (400 * int ($year / 400.0)))) {
      $value = $month + $date / $LeapDaysinmonth{int($month)};
    }
    else {
      $value = $month + $date / $Daysinmonth{int($month)};
    }
    $value = $year + ($value)/12.0;
    $YearsSeen{$year} =1;
  }

  if ($opt_type eq 'month') {
    ($year,$month) = $value =~ m|(\d+)-(\d+)|o;
    $value = $year + ($month)/12.0;
    $YearsSeen{$year} =1;
  }

  $Data{"$scheme"}{"$value"}{"total_accesses"} += $total_accesses
    if exists $Data{"$scheme"}{"$value"}{"total_accesses"};
  $Data{"$scheme"}{"$value"}{"total_accesses"} = $total_accesses
    unless exists $Data{"$scheme"}{"$value"}{"total_accesses"};
    

  $Data{"$scheme"}{"$value"}{"total_bytes"} += $total_bytes
    if exists $Data{"$scheme"}{"$value"}{"total_bytes"};
  $Data{"$scheme"}{"$value"}{"total_bytes"} = $total_bytes
    unless exists $Data{"$scheme"}{"$value"}{"total_bytes"};

  $Data{"$scheme"}{"$value"}{"ftp_accesses"} += $ftp_accesses
    if exists $Data{"$scheme"}{"$value"}{"ftp_accesses"};
  $Data{"$scheme"}{"$value"}{"ftp_accesses"} = $ftp_accesses
    unless exists $Data{"$scheme"}{"$value"}{"ftp_accesses"};
    

  $Data{"$scheme"}{"$value"}{"ftp_bytes"} += $ftp_bytes
    if exists $Data{"$scheme"}{"$value"}{"ftp_bytes"};
  $Data{"$scheme"}{"$value"}{"ftp_bytes"} = $ftp_bytes
    unless exists $Data{"$scheme"}{"$value"}{"ftp_bytes"};
 

  $Data{"$scheme"}{"$value"}{"gopher_accesses"} += $gopher_accesses
    if exists $Data{"$scheme"}{"$value"}{"gopher_accesses"};
  $Data{"$scheme"}{"$value"}{"gopher_accesses"} = $gopher_accesses
    unless exists $Data{"$scheme"}{"$value"}{"gopher_accesses"};
    

  $Data{"$scheme"}{"$value"}{"gopher_bytes"} += $gopher_bytes
    if exists $Data{"$scheme"}{"$value"}{"gopher_bytes"};
  $Data{"$scheme"}{"$value"}{"gopher_bytes"} = $gopher_bytes
    unless exists $Data{"$scheme"}{"$value"}{"gopher_bytes"};
 
  
  $Data{"$scheme"}{"$value"}{"http_accesses"} += $http_accesses
    if exists $Data{"$scheme"}{"$value"}{"http_accesses"};
  $Data{"$scheme"}{"$value"}{"http_accesses"} = $http_accesses
    unless exists $Data{"$scheme"}{"$value"}{"http_accesses"};
    

  $Data{"$scheme"}{"$value"}{"http_bytes"} += $http_bytes
    if exists $Data{"$scheme"}{"$value"}{"http_bytes"};
  $Data{"$scheme"}{"$value"}{"http_bytes"} = $http_bytes
    unless exists $Data{"$scheme"}{"$value"}{"http_bytes"};
 
}

if ($Out_count) {
  if ($opt_total) {
    open ($Output_all_cnt, ">$Output_all_cnt") || 
      die "Could not open $Output_all_cnt:$!";
  }
  if ($opt_ftp) {
    open ($Output_ftp_cnt, ">$Output_ftp_cnt") || 
      die "Could not open $Output_ftp_cnt:$!";
  }
  if ($opt_gopher) {
    open ($Output_gph_cnt, ">$Output_gph_cnt") || 
      die "Could not open $Output_gph_cnt:$!";
  }
  if ($opt_http) {
    open ($Output_www_cnt, ">$Output_www_cnt") || 
      die "Could not open $Output_www_cnt:$!";
  }

  foreach $value (sort keys %{$Data{"$scheme"}}) {
    if ($opt_total) {
      printf $Output_all_cnt "% 15.11f\t %12d\n",  $value, 
      $Data{"$scheme"}{"$value"}{"total_accesses"};
    } 
    if ($opt_ftp) {
      printf $Output_ftp_cnt "% 15.11f\t %12d\n",  $value, 
      $Data{"$scheme"}{"$value"}{"ftp_accesses"};
    }
    if ($opt_gopher) {
      printf $Output_gph_cnt "% 15.11f\t %12d\n",  $value, 
      $Data{"$scheme"}{"$value"}{"gopher_accesses"};
    }
    if ($opt_http) {
      printf $Output_www_cnt "% 15.11f\t %12d\n",  $value, 
      $Data{"$scheme"}{"$value"}{"http_accesses"};
    }
  }

  if ($opt_total) {
    close ($Output_all_cnt) || 
      die "Could not close $Output_all_cnt:$!";
  }
  if ($opt_ftp) {
    close ($Output_ftp_cnt) || 
      die "Could not close $Output_ftp_cnt:$!";
  }
  if ($opt_gopher) {
    close ($Output_gph_cnt) || 
      die "Could not close $Output_gph_cnt:$!";
  }
  if ($opt_http) {
    close ($Output_www_cnt) || 
      die "Could not close $Output_www_cnt:$!";
  }
}


if ($Out_bytes) {
  if ($opt_total) {
    open ($Output_all_byt, ">$Output_all_byt") || 
      die "Could not open $Output_all_byt:$!";
  }
  if ($opt_ftp) {
    open ($Output_ftp_byt, ">$Output_ftp_byt") || 
      die "Could not open $Output_ftp_byt:$!";
  }
  if ($opt_gopher) {
    open ($Output_gph_byt, ">$Output_gph_byt") || 
      die "Could not open $Output_gph_byt:$!";
  }
  if ($opt_http) {
    open ($Output_www_byt, ">$Output_www_byt") || 
      die "Could not open $Output_www_byt:$!";
  }

  foreach $value (sort keys %{$Data{"$scheme"}}) {
    if ($opt_total) {
      printf $Output_all_byt "% 15.11f\t %12d\n",  $value, 
      $Data{"$scheme"}{"$value"}{"total_bytes"};
    } 
    if ($opt_ftp) {
      printf $Output_ftp_byt "% 15.11f\t %12d\n",  $value, 
      $Data{"$scheme"}{"$value"}{"ftp_bytes"};
    }
    if ($opt_gopher) {
      printf $Output_gph_byt "% 15.11f\t %12d\n",  $value, 
      $Data{"$scheme"}{"$value"}{"gopher_bytes"};
    }
    if ($opt_http) {
      printf $Output_www_byt "% 15.11f\t %12d\n",  $value, 
      $Data{"$scheme"}{"$value"}{"http_bytes"};
    }
  }



  if ($opt_total) {
    close ($Output_all_byt) || 
      die "Could not close $Output_all_byt:$!";
  }
  if ($opt_ftp) {
    close ($Output_ftp_byt) || 
      die "Could not close $Output_ftp_byt:$!";
  }
  if ($opt_gopher) {
    close ($Output_gph_byt) || 
      die "Could not close $Output_gph_byt:$!";
  }
  if ($opt_http) {
    close ($Output_www_byt) || 
      die "Could not close $Output_www_byt:$!";
  }
}

open ($Output_gnuplot, ">$Output_gnuplot") ||
  die "Could not open $Output_gnuplot:$!";
print $Output_gnuplot <<EOTOP
set xtics 0,1
set border
set tics out
set key below title 'Legend' box 
set grid
set logscale y
set term pbm color


EOTOP
  ;

if ($opt_type eq 'date') {
  print $Output_gnuplot "set xtics (";
  
  foreach $year (sort keys %YearsSeen) {
    print $Output_gnuplot  "\"$year\" $year,";
    foreach $tic (sort keys %Ticlevels) {
      print $Output_gnuplot  " \"$Ticlevels{$tic}\" ", 
            $year + $tic,", ";
    }
  }
  print $Output_gnuplot "\"\" ", $year + 1, ")\n";
  print $Output_gnuplot "set xlabel 'Month'\n";
  print $Output_gnuplot "set mxtics\n";
}
  
if ($opt_type eq 'month') {
  print $Output_gnuplot "set xtics (";
  
  foreach $year (sort keys %YearsSeen) {
    print $Output_gnuplot  "\"$year\" $year,";
    foreach $tic (sort keys %TiclevelsMonth) {
      print $Output_gnuplot  " \"$TiclevelsMonth{$tic}\" ", 
            $year + $tic, ", ";
    }
  }
  print $Output_gnuplot "\"\" ", $year + 1, ")\n";
  print $Output_gnuplot "set xlabel 'Month'\n";
  print $Output_gnuplot "set mxtics 30\n";
}

if ($opt_type eq 'per_hour') {
  print $Output_gnuplot <<EOTICS
set xlabel 'Hour'
EOTICS
  ;
}

if ($opt_type eq 'per_day') {
  print $Output_gnuplot <<EOTICS
set xlabel 'Date'
EOTICS
  ;
}
if ($opt_type eq 'per_month') {
  print $Output_gnuplot <<EOTICS
set xtics ("Jan" 1, "Feb" 2, "Mar" 3, "Apr" 4, "May" 5, "Jun" 6, "Jul" 7, "Aug" 8, "Sep" 9, "Oct" 10, "Nov" 11, "Dec" 12) 
set mxtics 30
set xlabel 'Month'
EOTICS
  ;
}

if ($opt_type eq 'year') {
  print $Output_gnuplot <<EOTICS
set xlabel 'Year'
set mxtics 12
EOTICS
  ;
}

if ($Out_count) {
  if ($opt_title) {
    $Title = $opt_title . "Number of Accesses";
  }
  else {
     $Title = "Number of Accesses";
  }
  print $Output_gnuplot "set title \"$Title\"\n";
  print $Output_gnuplot "set output \"$Prefix-cnt.ppm\"\n";
  $Plot = 'plot ';
  
  if ($opt_total) {
    $Plot .= "\"$Output_all_cnt\"" .
      " using 1:2 title 'Total Access' with lines,\\\n";
  }
  if ($opt_ftp) {
    $Plot .= "\"$Output_ftp_cnt\"" .
      " using 1:2 title 'FTP Access' with lines,\\\n";
  }
  if ($opt_gopher) {
    $Plot .= "\"$Output_gph_cnt\"" .
      " using 1:2 title 'Gopher Access' with lines,\\\n";
  }
  if ($opt_http) {
    $Plot .= "\"$Output_www_cnt\"" .
      " using 1:2 title 'HTTP Access' with lines,\\\n";
  }
  chop ($Plot);			# newline
  chop ($Plot);			# backslash
  chop ($Plot);			# comma
  $Plot .= "\n\n";
  print $Output_gnuplot $Plot;
}

if ($Out_bytes) {
  if ($opt_title) {
    $Title = $opt_title . "Bytes Transferred";
  }
  else {
     $Title = "Bytes Transferred";
  }
  print $Output_gnuplot "set title \"$Title\"\n";
  print $Output_gnuplot "set output \"$Prefix-byt.ppm\"\n";
  $Plot = 'plot ';
  
  if ($opt_total) {
    $Plot .= "\"$Output_all_byt\"" .
      "using 1:2 title 'Total Bytes' with lines,\\\n";
  }
  if ($opt_ftp) {
    $Plot .= "\"$Output_ftp_byt\"" .
      "using 1:2 title 'FTP Bytes' with lines,\\\n";
  }
  if ($opt_gopher) {
    $Plot .= "\"$Output_gph_byt\"" .
      "using 1:2 title 'Gopher Bytes' with lines,\\\n";
  }
  if ($opt_http) {
    $Plot .= "\"$Output_www_byt\"" .
      "using 1:2 title 'HTTP Bytes' with lines,\\\n";
  }
  chop ($Plot);			# newline
  chop ($Plot);			# backslash
  chop ($Plot);			# comma
  $Plot .= "\n\n";
  print $Output_gnuplot $Plot;
}


close ($Output_gnuplot) ||
  die "Could not close $Output_gnuplot:$!";
__END__
########################################################

	# These next few lines are legal in both Perl and Nroff.

.00;		# finish .ig
 
'di		\" finish diversion--previous line must be blank
.nr nl 0-1	\" fake up transition to first page again
.nr % 0		\" start at page 1
'; __END__	#### From here on it's a standard manual page ####
.TH ~((file-name) :up) 1 "~(day) ~(mon) ~(date) ~(year)" "Project Pilgrim, University of Massachusetts at Amherst"
.SH NAME
~(file-name) \- 
.SH SYNOPSIS
.B ~(file-name) [options] [files]
.SH DESCRIPTION
.SH OPTIONS
.SH ENVIRONMENT
.SH EXAMPLES
.SH FILES
.SH AUTHOR
~(user-name) ~(eval user-mail-address)
.SH SEE ALSO
.SH DIAGNOSTICS
.SH BUGS









