#!/usr/local/bin/perl5.6.0


######################################################
# Tue May 13 18:14:24 BST 2003
# dn1 fix-it script for missing exoseq_readinfo reads
######################################################
use strict;
#use sql_engine;
#use SDTable;
use Data::Dumper;
use Getopt::Long;
use Mail::Mailer;


###############
# Mail message
###############
my $mail_message;

my $mode;
my ($help);
GetOptions (
	    "mode=s"     => \$mode,      # The category required
#	    "d=s"     => \$department,             # The department required (overrides category)
#	    "s=s"     => \$sumtrim,                # If specified, trims the summary down to the desired length
#	    "notable" => \$notable,
	    "h"       => \$help,
	    
	    );


#################
# -h (help) info
#################
if ($help){
    die "No help is available...\n";
}

&add_2_mail("---------------------------------------------------------");
&add_2_mail("Running from cron on genesis (monitor_pool.pl)");
&add_2_mail("\n\tRemember to monitor the HelpDesk PSG pool !\n\tCurrent state of play...");
&add_2_mail("---------------------------------------------------------");



my $report = '/usr/local/badger/lib/Reports/Programs/HelpDesk/get_pending.pl -d 1 -s 55';
open(FH, "$report 2>&1 |");
while (<FH>) {
    chomp $_;
    &add_2_mail("$_\n");
}



sub add_2_mail {
    my ($new) = @_;
    my $to_print = $new . "\n"; 
#    warn "$to_print";

    $mail_message = $mail_message . "$new\n";
}



sub send_mail {
    my ($message) = @_;
    
    my $email = "bug";
    my $subject = "HelpDesk PSG pool";
    my ($mailer);

    eval 
    {
	my $mailer = Mail::Mailer->new("mail");
	
	$mailer->open({ From    => "HelpDesk",
			To      => "$email",
			Subject => "$subject"}) ||  die "Can't open: $!\n";
    
	print $mailer "$message";
	
	close($mailer);
    };

    if ($@){
	print STDERR "Error sending mail; $@\n\nFrom monitor_pool.pl\nTo $email\nSubject $subject\n\n$message\n";
    }
    
}



###########
## Tidy up
###########
END {
    
    &send_mail($mail_message);

};


