#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }

use MIME::Lite;
use Mail::Sendmail;
use Date::Calc qw(:all);

#-------------------------------------------------------------------------------

our $debug      = $ARGV[0] ;
our $username   = $ARGV[1] ;

# perl /usr/lib/cgi-bin/scripts/cron/email_sql_backup.pl 1 rory

#------------------------------------------------------------------------------------------

&config_set_paths;
&config_set_vars ;

&today;
&read_bz2_backups($mysqlbakpath,'bz2');
&sort_files;

exit ;

#-------------------------------------------------------------------------------

sub read_bz2_backups {
   
	my ($path,$ext) = @_;

	opendir BP_DIR,"$path" or die "Cannot open $path: $!\n";
		my @backups = grep { /\.$ext$/ } readdir BP_DIR;
	closedir BP_DIR;

	for (@backups) {
		my ($db_bk_name,$db_bk_date,$db_bk_time,$db_bk_ext) = split(/\./);
		
		$db_bk_date =~ s/\-//g;
		
		push @bkfiles, "$db_bk_date$db_bk_time|$_" ;
	}

} #-------------------------------------------------------------------------------

sub sort_files {

	my @sorted_bkfiles = sort {$b <=> $a} @bkfiles ; 

	my ($sortprefix,$file) = split(/\|/, shift @sorted_bkfiles);

	if ($file) {
		&send_email($file);
	}

} #-------------------------------------------------------------------------------

sub send_email {
	
	my ($attachname) = @_ ;

	my $to  = ($debug) ? $email_it_1 : $email_accounts ;
	my $bcc = ($debug) ? '' : $email_it_1 ;

	&common_send_smtp_mail('',$to,'',$bcc,"$useropts{acronym} MySQL Backup : $attachname",'Please see DB backup attached...',"<tr><td>&nbsp;$attachname&nbsp;</td></tr>",'html',"<thead><tr><th>&nbsp;MySQL latest Backup&nbsp;</th></tr></thead>",$mysqlbakpath,$attachname,'','','') ;

	# $msg = MIME::Lite -> new(
		# From    => "$useropts{acronym} <info\@aisport.africa>",
		# To      => $to,
		# Subject => "$useropts{acronym} MySQL Backup : $attachname",
		# Type    => 'multipart/mixed'
	# );

	# # my $boundary = "====" . time() . "====";

	# my $email_body = <<END_OF_BODY;
	# <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	# <html>
	# <head>
	# <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
	# <title></title>

	# <style type="text/css">
	# body { background-color:#ffffff; color:#222222; }
	# tbody tr td { font-size: 11px; }
	# .table th{background-color:#fff !important}.btn>.caret,.dropup>.btn>.caret{border-top-color:#000 !important}.label{border:1px solid #000}.table{border-collapse:collapse !important}.table-bordered th,.table-bordered td{border:1px solid #ddd !important}
	# .table-striped>tbody>tr:nth-child(odd)>td,.table-striped>tbody>tr:nth-child(odd)>th{background-color:#EEEEEE; color:#222222; }
	# .table-striped>tbody>tr:nth-child(even)>td,.table-striped>tbody>tr:nth-child(even)>th{ background-color:#A4A2A2; color:#ffffff; }
	# .table-striped>tbody>tr:nth-child(odd)>td a:not(.btn) { text-decoration: none; color: #375A7F; font-weight: bold; }
	# .table-striped>tbody>tr:nth-child(even)>td a:not(.btn) { text-decoration: none; color: #BCCEE2; font-weight: bold; }
	# .table>thead>tr>th, .table>tbody>tr>th, .table>tfoot>tr>th, .table>thead>tr>td, .table>tbody>tr>td, .table>tfoot>tr>td { height: 8px; vertical-align: middle; }
	# </style>

	# </head>
	# <body style="background-color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 13px">

	# <div align="center">

	# <img alt="ITV" src="https://$useropts{web}/img/$useropts{logo}" border="0"></a>
	# <br><br>

	# <table id="ff-table" class="table table-striped table-bordered bootstrap-datatable datatable responsive">    
		# <thead><tr><th>&nbsp;MySQL latest Backup&nbsp;</th></tr></thead>
		# <tbody>		
			# <tr><td>&nbsp;$attachname&nbsp;</td></tr>
		# </tbody>
	# </table>

	# </p>
	# </div>
	# </body>
	# </html>
# END_OF_BODY

	# $msg -> attach(
		# Type     => 'text/html',
		# Data     => $email_body
	# );

	# $msg -> attach (
		# Type        => 'AUTO',
		# Path        => "$mysqlbakpath/$attachname",
		# Filename    => "$attachname",
		# Disposition => 'attachment'
	# ) or die "Error adding $path : $attach $!\n";
		
	# $msg -> send ('smtp','localhost', Debug=>0 );

} #-------------------------------------------------------------------------------

use common ;
use today ;
use uploader ;
use cfg_paths ;

1;

