#!/usr/bin/perl
# ~AsmodeuS~ (2002)
# asm@asmodeus.com.ua

require '../libexec/config.pl';
&parse_arguments(@ARGV);
if ($user) { user($user);  }
else { &help; }

#show user statistic

sub user ()
{
 my $user = shift;

if ($FORM{period} == 1) {
   $WHERE = "WHERE date_format(login, '%Y-%m-%d')=curdate() ";
 }
elsif ($FORM{period} == 2) {
   $WHERE = "WHERE TO_DAYS(curdate()) - TO_DAYS(login) = 1 ";
 }
elsif ($FORM{period} == 3) {
   $WHERE = "WHERE TO_DAYS(curdate()) - TO_DAYS(login) < 7 ";
 }
elsif ($FORM{period} == 4) {
   $WHERE = "WHERE date_format(login, '%Y-%m')=date_format(curdate(), '%Y-%m') ";
 }

 $q = $db -> prepare("SELECT  
  sum(if(date_format(login, '%Y-%m-%d')=curdate(), sent, 0)) / 1024, 
  sum(if(date_format(login, '%Y-%m-%d')=curdate(), recv, 0)) / 1024, 
  SEC_TO_TIME(sum(if(date_format(login, '%Y-%m-%d')=curdate(), duration, 0))),

  sum(if(TO_DAYS(curdate()) - TO_DAYS(login) = 1, sent, 0)) / 1024,
  sum(if(TO_DAYS(curdate()) - TO_DAYS(login) = 1, recv, 0)) / 1024,
  SEC_TO_TIME(sum(if(TO_DAYS(curdate()) - TO_DAYS(login) = 1, duration, 0))),

  sum(if(TO_DAYS(curdate()) - TO_DAYS(login) < 7, sent, 0)) / 1024,
  sum(if(TO_DAYS(curdate()) - TO_DAYS(login) < 7, recv, 0)) / 1024,
  SEC_TO_TIME(sum(if(TO_DAYS(curdate()) - TO_DAYS(login) < 7, duration, 0))),

  sum(if(date_format(login, '%Y-%m')=date_format(curdate(), '%Y-%m'), sent, 0)) / 1024,
  sum(if(date_format(login, '%Y-%m')=date_format(curdate(), '%Y-%m'), recv, 0)) / 1024,
  SEC_TO_TIME(sum(if(date_format(login, '%Y-%m')=date_format(curdate(), '%Y-%m'), duration, 0))),
  
  sum(sent) / 1024, sum(recv) / 1024, SEC_TO_TIME(sum(duration))  FROM log WHERE id='$user';")
   || die $db->strerr;
  $q -> execute ();
#
#  if ($q->rows == 0) {
#      print "User $user not exist";
#      return 0;	
#    }
# 
  ($today_sent, $today_recv, $today_duration, $yesterday_sent, $yesterday_recv, $yesterday_duration,
  $week_sent, $week_recv, $week_duration, $month_sent, $month_recv, $month_duration, $all_sent, $all_recv, $all_duration) = $q -> fetchrow();
  $today_sum = $today_sent + $today_recv;
  $yesterday_sum = $yesterday_sent + $yesterday_recv;
  $week_sum = $week_sent + $week_recv;
  $month_sum = $month_sent + $month_recv;
  $all_sum = $all_sent + $all_recv;
  
print "User:\t$user
 Period    Duration          Sent        recv         Sum
 Today     $today_duration\t $today_sent\t $today_recv\t $today_sum
 Yesterday $yesterday_duration\t $yesterday_sent\t $yesterday_recv\t $yesterday_sum
 7 Days    $week_duration\t $week_sent\t $week_recv\t $week_sum
 Month     $month_duration\t $month_sent\t $month_recv\t $month_sum
 All       $all_duration\t $all_sent\t $all_recv\t $all_sum
";
$q -> finish ();

}

sub parse_arguments {
    local(@argv) = @_;
    while ($_ = $argv[0], /^-/) {
      shift @argv;
        last if /^--$/;
        if    (/^-?(u)$/)   { $user = $argv[0]; shift @argv }
        elsif (/^-?(d)$/)   { $detail = $argv[0]; shift @argv }
        elsif (/^-?(h)$/)   { help(); }
        elsif (/^-?(p)$/)   { $period = $argv[0]; shift @argv }
    }
    #&help if $#argv < 0;
}

sub help ()
{
 print "
 Users statistic
 Copyright (C) 2002 ~AsmodeuS~

 -h help
 -u user name
 -p [t,y,w,m,a] period
 -d [YYYY-MM-DD] details\n";
}
