eval 'LANG=C exec perl -w -S $0 ${1+"$@"}'
    if $running_under_some_shell;
$running_under_some_shell = 0;

######################################################################
# 
# Copyright  Freescale Semiconductor, Inc. 2004-2006. All rights reserved.
#
# Stuart Hughes, stuarth@freescale.com,  20th April 2005
# 
# This file is part of LTIB.
#
# LTIB 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.
#
# LTIB 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 LTIB; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

# autobuild ltib bsps and mail the results
#
# To use this:
#  1/ Create a directory that will hold your ltib projects
#  2/ In that directory unpack and configure ltib a number of times
#     once for each BSP you want to test
#  3/ Copy this script into the top level directory containing the 
#     ltib BSPs
#  4/ Edit the values in the section: EDIT THESE
#  2/ Add an entry in your crontab to run the script, for instance,
#     I run a build at 10 minutes past 9pm Mon-Fri:
#     10 21 * * 1-5 (cd /home/seh/ltib_bsps ; ./autobuild_ltib 2>&1 )
#
#
######################################################################
use MIME::Lite;
use Cwd 'abs_path';
use Getopt::Std;

####### EDIT THESE ######
# who to email
$to   = 'Ltib-users@sourceforge.freescale.net';
# who to CC (comma separated list)
$cc   = '';
# who it came from
$from = 'autobuilder@auslxpb02.mtwk.freescale.net';
####### END OF EDIT THESE ######

$opt_d = ".";
$opt_P = "";
$opt_h = 0;
$opt_f = 0;
$opt_c = 0;

$usage = <<TXT;

Usage: autobuild_ltib [ -d <dir> -f -h -P profile ]
  where:
    -d <dir>  : base ltib directory containing bsps (default is $opt_d)
    -f        : full force build
    -c        : continue on error
    -P <pf>   : profile
    -h        : help
TXT

getopts("d:P:hfc") or die $usage;
die "$opt_d is not a directory" unless -d $opt_d;
die $usage if $opt_h;

$ltib_cmd  = "./ltib -b ";
$ltib_cmd .= "--profile $opt_P " if $opt_P;
$ltib_cmd .= "-f " if $opt_f;
$ltib_cmd .= "-C " if $opt_c;

($day, $month, $year) = (gmtime)[3,4,5];
$date = sprintf("%04d%02d%02d", $year+1900, $month+1, $day);

$msg = MIME::Lite->new(
             From     => $from,
             To       => $to,
             Cc       => $cc,
             Subject  =>"Build results for $date",
             Type     =>'multipart/mixed',
                      );
foreach $dir ( glob("$opt_d/*") ) {
    next unless -d $dir;
    #$apath = abs_path($dir);
    $log  = "Processing directory $dir\n";
    $log .= "$ltib_cmd \n";
    system_nb(<<TXT);
cd $dir
rm -rf rpm/BUILD/*
cvs -q up -dP &> /dev/null
rm -f build.log  build.log.end.txt
$ltib_cmd &> build.log 
tail -30 build.log > build.log.end.txt
cd -
TXT
}

# build failure summary
system_nb(<<TXT);
./mk_pkg_results > build_res.html
scp build_res.html auslxsc01:/opt/freescale/var/www/html/ltib/
TXT

$msg->attach(Type     =>'TEXT',
             Data     => "Here are todays build results\n",
             );

$msg->attach(Type     =>'text/html',
             Path     => "build_res.html",
             Filename => "build_res.html",
             Disposition => 'attachment',
             );

$msg->send("sendmail");
exit 0;

# Normally system will block SIGINT and SIGQUIT
# We don't want to do this, or we can't properly CNTRL-C
sub system_nb
{
    my (@cmd) = @_;
    if(my $pid = fork) {
        waitpid($pid, 0);
        return $?;
    } else {
        die "cannot fork: $!\n" unless defined $pid;
        exec(@cmd) or die "exec: @cmd: $!";
    }
}

