#!/usr/local/bin/perl

##########################################################################
#                                                                        #
#  This script was written by S.Nazin (http://xray.sai.msu.su/~leng/)    #
#  Sternberg Astronomical Institute, Moscow University.                  #
#                                                                        #
##########################################################################

use CGI qw(:all);
use GD;

##########################################################################
#                                                                        #
#                     USER  DEFINED  VARIABLES                           #
#                                                                        #
##########################################################################

$titleFont    = gdLargeFont;
$textFont     = gdMediumBoldFont;
$smallFont    = gdSmallFont;
$tinyFont     = gdTinyFont;
$topmargin    = 3;
@geometry     = ( 560, 420 );
$graphLmargin = 70;
$graphRmargin = 85;
$graphTmargin = 10;
$graphBmargin = 50;
$XTics        = 8;
$YTics        = 10;
$TicLI        = 0;
$TicLO        = 5;
$maxSize      = 0;


##########################################################################
#                                                                        #
#            PRECALCULATED VARIABLES  (DON'T CHANGE THEM)                #
#                                                                        #
##########################################################################

$topLineY = $topmargin*3+$titleFont->height;
$graphL   = $graphLmargin;
$graphR   = $geometry[0]-$graphRmargin;
$graphT   = $topLineY+$graphTmargin;
$graphB   = $geometry[1]-$graphBmargin;
$graphW   = $graphR - $graphL;
$graphH   = $graphB - $graphT;

%Monthes = ( 
  'Jan' => 0,
  'Feb' => 31,
  'Mar' => 59,
  'Apr' => 90,
  'May' => 120,
  'Jun' => 151,
  'Jul' => 181,
  'Aug' => 212,
  'Sep' => 243,
  'Oct' => 273,
  'Nov' => 304,
  'Dec' => 334,
);

%DoNotPlot = (
  'population' => 1,
  'iauc'       => 1,
);

@show = param('s');

if( scalar @show ){
   $DoNotPlot{'alex'}      = 1;
   $DoNotPlot{'alexeyev'}  = 1;
   $DoNotPlot{'andy'}      = 1;
   $DoNotPlot{'belokuro'}  = 1;
   $DoNotPlot{'binary'}    = 1;
   $DoNotPlot{'darja'}     = 1;
   $DoNotPlot{'galja'}     = 1;
   $DoNotPlot{'ivan'}      = 1;
   $DoNotPlot{'jana'}      = 1;
   $DoNotPlot{'karpov'}    = 1;
   $DoNotPlot{'ketsaris'}  = 1;
   $DoNotPlot{'leng'}      = 1;
   $DoNotPlot{'lesha'}     = 1;
   $DoNotPlot{'lipunov'}   = 1;
   $DoNotPlot{'lisett'}    = 1;
   $DoNotPlot{'moulin'}    = 1;
   $DoNotPlot{'mystery'}   = 1;
   $DoNotPlot{'petrov'}    = 1;
   $DoNotPlot{'popova'}    = 1;
   $DoNotPlot{'ps'}        = 1;
   $DoNotPlot{'raguzova'}  = 1;
   $DoNotPlot{'sasha'}     = 1;
   $DoNotPlot{'sazhin'}    = 1;
   $DoNotPlot{'shakura'}   = 1;
   $DoNotPlot{'shura'}     = 1;
   $DoNotPlot{'vpetrov'}   = 1;

   foreach (@show){
     $DoNotPlot{$_} = 0;
   }
}

##########################################################################
#                                                                        #
##########################################################################

    $im = new GD::Image(@geometry);

    $white  = $im->colorAllocate(255, 255, 255);
    $yellow = $im->colorAllocate(255,255,208);
    $black  = $im->colorAllocate(0,0,0);

    @colors = ( 
        $im->colorAllocate(224,0,0),
        $im->colorAllocate(0,224,0),
        $im->colorAllocate(0,0,224),
        $im->colorAllocate(224,0,224),
        $im->colorAllocate(112,0,0),
        $im->colorAllocate(0,112,0),
        $im->colorAllocate(112,112,0),
        $im->colorAllocate(224,112,0),
        $im->colorAllocate(112,0,224),
        $im->colorAllocate(112,112,112),
    );

    $im->fill(1,1,$yellow);

    $str = &GetDU();
    $imax = scalar( @TimeStamps ) - 1;
    $RealMax  = &FindMax();

    $maxSize  = $RealMax unless( $maxSize );
    $SizeStep = $maxSize/$YTics/1000;
    $TimeStep = ($TimeStamps[$imax]-$TimeStamps[1])/$XTics;

    $im->string($titleFont,($geometry[0]-40*$titleFont->width)/2,
                $topmargin,"Last updated at: $str",$black);
    $im->line( 4,$topLineY, $geometry[0]-4,$topLineY, $black);

    $im->rectangle( $graphL, $graphT, $graphR, $graphB, $black );

    for( $i=0; $i<=$XTics; $i++ ){
       $im->line( $graphL+$i*$graphW/$XTics, $graphB-$TicLI,
                  $graphL+$i*$graphW/$XTics, $graphB+$TicLO, $black );
       $curTime = $TimeStamps[1]+$i*$TimeStep;
       $date = int( $curTime/24/3600 );
       $time = int(($curTime-24*3600*$date)/60);
       $h = int($time/60);
       $m = $time - 60*$h;
       $date = $date - 365*int($date/365);
       foreach (sort {$Monthes{$b} <=> $Monthes{$a}} keys %Monthes){
          $month = $_;
          last if( $date > $Monthes{$_} );
       }
       $date = $date - $Monthes{$month};

       $im->string($smallFont,
                   $graphL+$i*$graphW/$XTics-3*$smallFont->width,
                   $graphB+$TicLO+5,
                   "$date $month", $black);
       $im->string($tinyFont,
                   $graphL+$i*$graphW/$XTics-2.5*$tinyFont->width,
                   $graphB+$TicLO+5+$smallFont->height+2,
                   sprintf("%02d:%02d",$h,$m), $black);
    }
    for( $i=0; $i<=$YTics; $i++ ){
       $im->line( $graphL-$TicLO, $graphB-$i*$graphH/$YTics,
                  $graphL+$TicLI, $graphB-$i*$graphH/$YTics, $black );
       $im->dashedLine( $graphL, $graphB-$i*$graphH/$YTics,
                  $graphR, $graphB-$i*$graphH/$YTics, $black );
       $im->string($textFont,$graphL-$TicLO-8*$textFont->width,
                             $graphB-$i*$graphH/$YTics-$textFont->height/2,
                             sprintf( "%4d Mb", $i*$SizeStep ), $black);
    }

    foreach $user (sort {${$b}[$imax] <=> ${$a}[$imax]} keys %Users){
       next if( $DoNotPlot{$user} );
       $color = shift @colors;
       push @colors, $color;
       &PlotUser( $user, $color );
    }

    print  "Content-type: image/gif\nPragma: no-cache\n\n";
    print $im->gif;

########################################################################

sub GetDU{
  my( $date_str, $time );
  my $i = 0;

  open( FIN, "<diskusage" );
  while(<FIN>){
     next if( /^-/ );
     if( /^(\d+)\s+\/.1\/home\/(\w+)\s*$/ ){
        ${$2}[$i] = $1;
        $Users{$2} = 1;
     }elsif( /^\w{3}\s+(\w{3})\s+(\d+)\s+(\d+):(\d+):(\d+)\s+\S+\s+(\d+)\s*$/ ){
        $date_str = $_;
        $time = $5+60*($4+60*($3+24*($2+$Monthes{$1}+365*($6-1997))));
        $i++;
        $TimeStamps[$i] = $time;
     }else{
     }
  }
  close( FIN );
  return( $date_str );
}

sub FindMax {
   my $max = 0;
   my $user, $i;

   foreach $user (keys %Users){
      next if( $DoNotPlot{$user} );
      for( $i = 1; $i<=$imax; $i++ ){
         $max = ${$user}[$i] if( $max < ${$user}[$i] );
      }
   }
   $max  = (int($max/50000)+1)*50000;
   return $max;
}

sub PlotUser {
    my( $user, $color ) = @_;

    my $prevX = 0; 
    my $prevY = $graphH*${$user}[1]/$maxSize;
    $prevY    = $graphH-1 if( $prevY >= $graphH );

    for( $i = 2; $i<=$imax; $i++ ){
       $newX = $graphW*($i-1)/($imax-1);
       $newY = $graphH*${$user}[$i]/$maxSize;
       $newY = $graphH-1 if( $newY >= $graphH );
       $im->line( $graphL+$prevX, $graphB-$prevY, 
                  $graphL+$newX,  $graphB-$newY, $color );
       $prevX = $newX;
       $prevY = $newY;
    }
    $userNo++;
    $im->string($textFont,$graphR+($graphRmargin-8*$textFont->width)/2,
                $graphT+($userNo-1)*$textFont->height,$user,$color);
}

__END__
