#! /usr/local/env perl

use warnings;
use strict;


my $CONFFILE = '/etc/rsbackup/rsbackup_server.conf';
my $TEMPLATEDIR = '/etc/rsbackup/modules/';
# some variables we need from the configuration file
# If they are not listed here, they are part of the sub getConfig
# and freed when we return from that.
my $SENDMAIL;
my $FROM;
my $LOCAL_BACKUP_PATH;
my %user;

my $OVERAGE_TO;
my $OVERAGE_ERROR_REPORT;
my $OVERAGE_ALLOCATED_OVER;
my $LOW_ERROR_REPORT;
my $OVERAGE_REPORT_REPLY_TO;


sub getConfig {
   open CONFIG,"<$CONFFILE" or die "could not open backup server configuration $CONFFILE: $!\n";
   my $conf = join( "", <CONFIG> );
   eval( $conf );
   close CONFIG;
}

sub getNumber {
   my $value = shift;
   $value =~ m/(\d+)/;
   return $1;
}

sub sendReport {
   my ($percentPaidFor, $username, $paidFor, $allocated, $used, $free, $percent, $userEmail ) = @_;
   # figure out which template we need, and load it
   my $template = $TEMPLATEDIR;
   if ( $percentPaidFor >= 1.0 ) {
      $template .= 'usageReport.critical.template';
   } elsif ( $percentPaidFor >= $OVERAGE_ERROR_REPORT ) {
      $template .= 'usageReport.hiuse.template';
   } elsif ( $percentPaidFor <= $LOW_ERROR_REPORT ) {
      $template .= 'usageReport.lowuse.template';
   } else {
      $template .= 'usageReport.template';
   }
   open TEMPLATE, "<$template" or die "Could not open $template template file: $!";
   $template = join ('', <TEMPLATE>);
   close TEMPLATE;
   
   # clean up some of the variables so we get pretty output
   $allocated = sprintf( '%d', $allocated );
   $used = sprintf( '%d', $used );
   $percentPaidFor = sprintf( '%d', $percentPaidFor * 100 );
   $percent = sprintf( '%d', $percent * 100 );
   $free =  sprintf( '%d', $free ); 

   # substitute values in the template
   $template =~ s/<percentPaidFor>/$percentPaidFor/gi;
   $template =~ s/<username>/$username/gi;
   $template =~ s/<paidFor>/$paidFor/gi;
   $template =~ s/<allocated>/$allocated/gi;
   $template =~ s/<used>/$used/gi;
   $template =~ s/<free>/$free/gi;
   $template =~ s/<percent>/$percent/gi;
   $template =~ s/<userEmail>/$userEmail/gi;

   # send the report
   open SENDMAIL,"|$SENDMAIL $userEmail" or die "Could not open sendmail $SENDMAIL: $!";
   print SENDMAIL $template;
   close SENDMAIL;
   
   return;
}

&getConfig();

my @dirSizes = `df -BM | grep $LOCAL_BACKUP_PATH`; # Get all backup mounts in megabytes
chomp @dirSizes;
my $output;
print "Percent\tUser\tPaid For\tAllocated\tUsed\tFree\tPercent Total\n";
foreach my $dir ( @dirSizes ) {
   my ( $device,$allocated,$used,$free, $percent, $mount ) = split( /\s+/, $dir );
   $allocated = &getNumber( $allocated ) / 1024;
   $used = &getNumber( $used ) / 1024;
   $free = &getNumber( $free ) / 1024;
   $percent = &getNumber( $percent ) / 100;
   $mount =~ m'/([^\/]+)$';
   $username = $1;
   my $paidFor = $user{$username}{'AllocatedSize'};
   my $userEmail = $user{$username}{'Contact'};
   my $percentPaidFor = $used/$paidFor;
   &sendReport( $percentPaidFor, $username, $paidFor, $allocated, $used, $free, $percent, $userEmail );
   $output .= "$percentPaidFor\t$username\t$paidFor\t$allocated\t$used\t$free\t$percent\t$userEmail\n";
 }
 
 print $output;
 
 1;
