#!/usr/bin/perl
#use Class::Date;
#use Date::Simple(':all');
#use POSIX;
# qw(strftime);
our @EXPORT = qw(parseToken sendAlertMail);

sub sendAlertMail
    ###########################
    # Author:Arun V Yakkundi
    # Created Date: Apr 30 2009
    # Modified Date: Apr 30 2009
    #Input: An array email credentils & array structure 
    #    : $arrMailCredentials['mailFrom'] = <From email id>
    #    : $arrMailCredentials['mailTo'] = <to email id>,<to email id>,<to email id>
    #    : $arrMailCredentials['mailReply'] = <email id>
    #    : $arrMailCredentials['mailCc'][0] = <cc email id>
    #    : $arrMailCredentials['mailCc'][1] = <cc email id>
    #    : $arrMailCredentials['mailBcc'][0] = <bcc email id>
    #    : $arrMailCredentials['mailBcc'][1] = <bcc email id>
    #    : $arrMailCredentials['mailSubject'] = 'Mail subject'
    #    : $arrMailCredentials['mailHtmlBody'] = 'Mail Body'
    #    : $arrMailCredentials['mailPlainBody'] = 'Mail Body'
    #    : $arrMailCredentials['mailAttachment'][0] = 'File Path'
    #    : $arrMailCredentials['mailAttachment'][1] = 'File Path'
    #Output: TRUE Always
    #Desc: Sends the mail to respective ids & return the only one status
    #ToDo: Sending the mail attachements
    ###########################
    #
    # How to use it
	#   my %arrMailCredentials = (
	#   'mailFrom', 'arun.yakkundi@implesys.com',
	#   'mailTo', 'arun.yakkundi@impelsys.com',
	#   'mailReply', 'arun.yakkundi@impelsys.com',
	#   'mailCc', 'arunvyakkundi@yahoo.com,arunvyakkundi@gmail.com',
    #	'mailBcc', 'arunvyakkundi@rediffmail.com',
    #	'mailSubject','This is a test mail from perl',
    #	'mailHtmlBody', 'Hi,<br>how are you <br>Cheers<br>Arun',
    #	'mailPlainBody', 'Do',
    #	'mailAttachment', 'None'
    #	);
    # Re-initilizing the variable for referencing
    #	$arrMailCredentials{'mailHtmlBody'} = "Hi <b>Arun</b><br>This is a template mail sent from jupiter server<br><b>Thanks & Regards<br>Arun</b>";
	#
    # sendAlertMail(%arrMailCredentials);
    #
    ##########################
{
    my (%arrMailCredentials) = @_;
    my $mailFrom = '';
    my $mailReplyTo = '';
    my $mailTo = '';
    my $mailCc = '';
    my $mailBcc = '';
    my $mailSubject = '';
    my $mailHtmlBody = '';
    my $isMailValidated = 1; # if all mailing data is available then 1 if any nacessary data is missing then 0
    my $sendMail ="/usr/sbin/sendmail -t"; 
    my $mailContentType = "Content-type: text/html\n\n";

    while(($key, $value) = each (%arrMailCredentials)) {
        if($key eq "mailFrom") {
            if($value ne "") {
                $mailFrom = $value;
            } 
            else {
                $isMailValidated = 0;
            }
        }
        elsif($key eq "mailReplyTo") {
            if($value ne "") {
                $mailReplyTo = $value;
            }
        }
        elsif($key eq "mailTo") { 
            if($value ne "") {
                $mailTo = $value;
            }
            else {
                $isMailValidated = 0;
            }
        }
        elsif($key eq "mailSubject") {
            if($value ne "") {
                $mailSubject = $value;
            }
            else {
                $isMailValidated = 0;
            }
        }
        elsif($key eq "mailHtmlBody") {
            if($value ne "") {
                $mailHtmlBody = $value;
            }
            else {
                $isMailValidated = 0;
            }
        }
        elsif($key eq "mailCc") {
           $mailCc = $value;
        }
        elsif($key eq "mailBcc") {
           $mailBcc = $value;
        }
    } # End of while loop

    # If all nacessary details are available then only send the mail
    if($isMailValidated eq 1) {
        if($mailReplyTo eq "") {
          $mailReplyTo = $mailFrom
        }
        open(SENDMAIL, "|$sendMail") or die "Cannot open $sendMail: $!";
        print SENDMAIL "Reply-to: " . $mailReplyTo."\n";
        print SENDMAIL "From: " . $mailFrom."\n";
        if($mailCc ne "") {
            print SENDMAIL "Cc: " . $mailCc . "\n";
        }
        if($mailBcc ne "") {
            print SENDMAIL "Bcc: " . $mailBcc . "\n";
        }
        print SENDMAIL "Subject: " . $mailSubject."\n";
        print SENDMAIL "To: " . $mailTo."\n";
        print SENDMAIL "". $mailContentType."\n";
        print SENDMAIL "" . $mailHtmlBody."\n";
        close(SENDMAIL);
    }
    else {
		print "Failed to send the mail\n";
    }
    return (1);
}

sub parseToken
    ###########################
    # Author:Arun V Yakkundi
    # Created Date: May 05 2009
    # Modified Date: May 05 2009
    # Input: A string(may contain the token) and a token to be evaluated 
	# Output: A String with token replaced by the value
	#
	#################
	#
	# How to use the function
    # @arrCronTokens[0] = '{REPORT_ERROR_MESSAGE}';
    # @arrCronTokens[1] = '{DATE_TIME_US_LONG}';
    # @arrCronTokens[2] = '{DATE_TIME_US_MEDIUM}';
    # @arrCronTokens[3] = '{DATE_TIME_US_SHORT}';
    # @arrCronTokens[4] = '{DATE_US_SHORT}';
    # @arrCronTokens[5] = '{IPC_CRON_FILE_PATH}';
    # @arrCronTokens[6] = '{IPC_CRON_FILE_NAME}';
    #
    # $hostMessage = "This is string generated on {DATE_TIME_US_LONG} in the file {IPC_CRON_FILE_NAME}";
    # $hostErrorMessage = "Err: DB error";
    # $str = parseToken($hostMessage,$hostErrorMessage, @arrCronTokens);
    # 
{
	my ($stringToParse, $errorMessage, @arrCronTokens) = @_; 
	my $parsedString = $stringToParse;
	my $errorString = $errorMessage;
    	my $tmpString = "";
	foreach $token  (@arrCronTokens) {
		if($token eq "{REPORT_ERROR_MESSAGE}") {
			$parsedString =~ s/$token/$errorString/;
		}
		elsif($token eq "{DATE_TIME_US_LONG}") {
			$tmpString = strftime("%a %b %d, %Y %H:%M:%S", gmtime);
			$parsedString =~ s/$token/$tmpString/;
		}
		elsif($token eq "{DATE_TIME_US_MEDIUM}") {
			$tmpString = strftime("%b %d, %Y %H:%M:%S", gmtime);
			$parsedString =~ s/$token/$tmpString/;
		}
		elsif($token eq "{DATE_TIME_US_SHORT}") { 
			$tmpString = strftime("%Y-%m-%d %H:c%M:%S", gmtime);
			$parsedString =~ s/$token/$tmpString/;
		}
		elsif($token eq "{DATE_US_SHORT}") {
			$tmpString = strftime("%Y-%m-%d", gmtime);
			$parsedString =~ s/$token/$tmpString/;
		}
		elsif($token eq "{IPC_CRON_FILE_NAME}") { 
			$tmpString = __FILE__;
			$parsedString =~ s/$token/$tmpString/;
		}
			elsif($token eq "{IPC_CRON_FILE_PATH}") { 
			$tmpString = $ENV{PWD};
			$parsedString =~ s/$token/$tmpString/;
		} 
	} # end of for loop
	return ($parsedString);
}
=beg 
    @arrCronTokens[0] = '{REPORT_ERROR_MESSAGE}';
    @arrCronTokens[1] = '{DATE_TIME_US_LONG}';
    @arrCronTokens[2] = '{DATE_TIME_US_MEDIUM}';
    @arrCronTokens[3] = '{DATE_TIME_US_SHORT}';
    @arrCronTokens[4] = '{DATE_US_SHORT}';
    @arrCronTokens[5] = '{IPC_CRON_FILE_PATH}';
    @arrCronTokens[6] = '{IPC_CRON_FILE_NAME}';
    $hostMessage = "{REPORT_ERROR_MESSAGE} This is string generated on {DATE_TIME_US_LONG} in the file {IPC_CRON_FILE_NAME}";
    $hostErrorMessage = "Err: DB error";
    $str =  parseToken($hostMessage,$hostErrorMessage,@arrCronTokens);
    print "\nProcessed :$str\n";
=cut
1;
