#!/usr/bin/perl -w
#
# checkmake.pl Script
#
#
#    ========== licence begin  GPL
#    Copyright (C) 2001 SAP AG
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2
#    of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#    ========== licence end
#
use strict;

#define vars
my ($Prot, $ErrFound, @Lines, @Msg);

foreach my $Prot ( @ARGV ) {
    
    # read prot
    open (IN, "<$Prot") || exit -1; # error
    @Lines = <IN>;
    close IN;

    # check for errors
    $ErrFound = grep { /END:\s*MAKE\s*TARGET:\s*\S*\s*RESULT:\s*ERROR/i } @Lines;
    push @Msg, @Lines if ( $ErrFound );
}

# error found
if ( @Msg ) {
    # send mail
    &SendErrorMail(@Msg) if ( @Msg ); 
    # exit with error code
    exit -1;
}

# no error
exit 0;

#########################################################################

sub SendErrorMail {
    use Mail::Sendmail;

    # define local vars
    my ($msg, $receiver, %mail, $hostname);

    
    # get hostname
    $hostname =  `hostname`;
    chomp $hostname;
    
    # generate message
    $msg  = "make problem on $hostname($ENV{OWN})\n\n";
    $msg .= join '', @_;
    
    $receiver .= 'joerg.mensing@sap.com,';
    
    %mail = ( To      => "$receiver",
                 From    => 'automail.unixmake@sap.com',
                 Message => "$msg",
                 Smtp    => 'mail.sap-ag.de',
                 Subject => "make problem on $hostname");

    if ( ! sendmail(%mail) ) {
        print "FATAL: sendmail error $Mail::Sendmail::error\n";
    }
}
