| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use oEdtk::Main; | ||
| 7 | use oEdtk::Config qw(config_read); | ||
| 8 | use oEdtk::DBAdmin qw(db_connect); | ||
| 9 | use oEdtk::Outmngr qw(omgr_check_acquit omgr_track_folds omgr_track_report); | ||
| 10 | use oEdtk::Messenger qw(oe_send_mail); | ||
| 11 | |||
| 12 | if ( @ARGV > 1 or $ARGV[0] =~ /-h/i ) { | ||
| 13 | die "Usage: $0 [history]\n\n\thistory : number of history days, default is 10 days \n"; | ||
| 14 | } | ||
| 15 | |||
| 16 | my $cfg = config_read( 'EDTK_DB', 'EDTK_STATS', 'MAIL' ); | ||
| 17 | my $dbh = db_connect( $cfg, 'EDTK_DBI_STATS', { AutoCommit => 1, RaiseError => 1 } ); | ||
| 18 | |||
| 19 | my $rows = omgr_check_acquit( $dbh, $ARGV[0] ); | ||
| 20 | $rows = omgr_track_folds( $dbh, $ARGV[0] ); | ||
| 21 | my $fmt = shift(@$rows); | ||
| 22 | my $head = shift(@$rows); | ||
| 23 | |||
| 24 | if ( @$rows == 0 ) { | ||
| 25 | warn "INFO: no acquittement\n"; | ||
| 26 | exit 1; | ||
| 27 | } | ||
| 28 | |||
| 29 | my %hMail; | ||
| 30 | foreach my $row (@$rows) { | ||
| 31 | $hMail{ @$row[$#$row] } .= sprintf( $$fmt . "\n", @$row ); | ||
| 32 | } | ||
| 33 | |||
| 34 | foreach my $mail ( keys %hMail ) { | ||
| 35 | my $mailfile = $cfg->{'EDTK_MAIL_ACQ'}; | ||
| 36 | open( my $fh, '<', $mailfile ) or die "Cannot open \"$mailfile\": $!\n"; | ||
| 37 | my @body = <$fh>; | ||
| 38 | close($fh); | ||
| 39 | |||
| 40 | push( @body, "\n\n\n" ); | ||
| 41 | push( @body, sprintf( $$fmt, @$head ), "\n" ); | ||
| 42 | push( @body, $hMail{$mail}, "\n" ); | ||
| 43 | |||
| 44 | my ( $sec, $min, $hour, $day, $month, $year ) = localtime(); | ||
| 45 | my $date = sprintf( "%02d/%02d/%d", $day, $month + 1, $year + 1900 ); | ||
| 46 | my $time = sprintf( "%02d:%02d:%02d", $hour, $min, $sec ); | ||
| 47 | |||
| 48 | my $subject = $cfg->{'EDTK_TYPE_ENV'}; | ||
| 49 | $subject .= " - "; | ||
| 50 | $subject .= $cfg->{'EDTK_MAIL_SUBJ'}; | ||
| 51 | $subject =~ s/%date/$date/; | ||
| 52 | $subject =~ s/%time/$time/; | ||
| 53 | |||
| 54 | oe_send_mail( $cfg->{$mail}, $subject, @body ); | ||
| 55 | } | ||
| 56 | |||
| 57 | $rows = omgr_track_report($dbh); | ||
| 58 | $fmt = shift(@$rows); | ||
| 59 | $head = shift(@$rows); | ||
| 60 | if ( @$rows == 0 ) { | ||
| 61 | warn "INFO: nothing to report\n"; | ||
| 62 | exit 1; | ||
| 63 | } | ||
| 64 | |||
| 65 | my %hMail2; | ||
| 66 | foreach my $row (@$rows) { | ||
| 67 | $hMail2{ @$row[6] } .= sprintf( $$fmt . "\n", @$row ); | ||
| 68 | } | ||
| 69 | |||
| 70 | foreach my $mail ( keys %hMail2 ) { | ||
| 71 | my $mailfile = $cfg->{'EDTK_MAIL_RPT'}; | ||
| 72 | open( my $fh, '<', $mailfile ) or die "Cannot open \"$mailfile\": $!\n"; | ||
| 73 | my @body = <$fh>; | ||
| 74 | close($fh); | ||
| 75 | |||
| 76 | push( @body, "\n\n\n" ); | ||
| 77 | push( @body, sprintf( $$fmt, @$head ), "\n" ); | ||
| 78 | push( @body, $hMail2{$mail}, "\n" ); | ||
| 79 | |||
| 80 | my ( $sec, $min, $hour, $day, $month, $year ) = localtime(); | ||
| 81 | my $date = sprintf( "%02d/%02d/%d", $day, $month + 1, $year + 1900 ); | ||
| 82 | my $time = sprintf( "%02d:%02d:%02d", $hour, $min, $sec ); | ||
| 83 | |||
| 84 | my $subject = $cfg->{'EDTK_TYPE_ENV'}; | ||
| 85 | $subject .= " - "; | ||
| 86 | $subject .= $cfg->{'EDTK_MAIL_SUBJ'}; | ||
| 87 | $subject =~ s/%date/$date/; | ||
| 88 | $subject =~ s/%time/$time/; | ||
| 89 | |||
| 90 | oe_send_mail( $cfg->{"EDTK_MAIL_$mail"}, $subject, @body ); | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; |