#!/usr/local/bin/perl

######################################################################
# Genererer gif basert paa statistikksidene.
# Henter snitt "Bytes Sent" og "Requests" fra maanedsoversikten (.html)
#
# Eirik Rogne (eirik.rogne@hiof.no) - 300497
# Sist oppdatert: 020597
# Aller sist oppdatert (og forandret krafig): 200498 - knut a mork
######################################################################

$basedir            = "/www/almashriq/htdocs/base/statistics";
$plotdir            = "/home/almash/admin/statistics/";

$plot_bytes_file    = "$plotdir"."/gnuplot_bytes.dat";
$plot_requests_file = "$plotdir"."/gnuplot_requests.dat";
$tmp_file           = "$plotdir"."temp.dat";

$merged_script      = "$plotdir"."merged.gp";
$merged_gif         = "$plotdir"."averages.gif";

#--------------------------------------------------------------

# Finner forrige maaned

$month = (Dec, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov)[(localtime)[4]];
$aar = `date +%y`;
chop($aar);
if( $forrige_maaned eq "Dec")
{
    $aar = $aar - 1;
}
$date = $month . $aar;

#--------------------------------------------------------------

# Skriver inn siste maaned, og lengde paa grafen

open (ORG_MERGED, $merged_script);
open (NEW_MERGED, ">$tmp_file");

while(<ORG_MERGED>)
{
    $line = $_;

    if ($line =~ /^(set xtics border mirror.*) ([0-9]*)\)/)
    {
	chop($line);
	$number = $2 + 1;
	print NEW_MERGED "$1 $2 \",$date\" $number\)\n";
    }
    elsif ($line =~ /^(set terminal gif interlace small size) ([0-9]*)/)
    {
	$number = $2 + 100;
	print NEW_MERGED "$1 $number" . ",480\n";
    }
    else
    {
	print NEW_MERGED $line;
    }
}
close (NEW_MERGED);
close (ORG_MERGED);
system("mv $tmp_file $merged_script");

#------------------------------------------------------------------

# Kjoerer gnuplot

system("/usr/bin/gnuplot $merged_script");
