#!/usr/bin/perl -w
use strict ;

=head1 sched_job_display

A complete network system schedule solution

=head2 DESCRIPTION

This program take a job file an check if we can detect error.

=head2 USAGE

    Usage : sched_job_display [-v] -j status-job 
     --help      : print this help
     --verbose
     --job job   : execute job

=head2 INSTALLATION

Complete installation details are in the README and README.conf files included
with the software. It works with library available on CPAN.

=head2 AUTHOR

(C) 2004-2005 Eric Bollengier

You may reach me through the contact info at eric@eb.homelinux.org

=head2 LICENSE

    sched_validate_job, part of the network scheduling system (Sched)
    Copyright (C) 2004-2005 Eric Bollengier
        All rights reserved.

    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

=cut

use XML::Mini::Document ;
use POSIX qw/strftime/ ;
use MIME::Base64 qw/decode_base64/ ;
use Sched ;

my $VERSION = '$Id: sched_job_display,v 1.1 2005/04/05 19:59:33 mcgregor Exp $' ;
$ENV{PATH} = '/bin:/usr/bin' ;

################################################################
# traitement de la ligne de commande

sub HELP_MESSAGE
{
    print "Usage : $0 [-v] -j job_file
    --help      : print this help
    --verbose
    --job job   : execute job
" ;
    exit (1) ;
}


################################################################
################	Variables	        ################

my $file_job = '';		# fichier xml du job
my $verbose ;

use Getopt::Long ;

GetOptions("job=s"    => \$file_job,
	   "verbose"  => \$verbose,
	   "help"     => \&HELP_MESSAGE) ;

if (!-r $file_job) {
    print "E : job file is not readable $!\n" ;
    HELP_MESSAGE() ;
}

Sched::init_test() ;

my $doc = new XML::Mini::Document() ;
$doc->parse($file_job) ;

$doc = $doc->getRoot()->getElement('job') ;

sub display_task
{
    my $root = shift ;
    
    for my $t (@{ $root->getAllChildren('task') }) {
	print "\n------------------------------------" ;
	print "\nid      = " . $t->attribute('id') ;

	print "\nhost    = " . $t->attribute('host') 
	    if ($verbose && $t->attribute('host'));

	print "\nstatus  = " . sprintf("%d", $t->attribute('status')) ;



	if ($verbose) {
	    print "\ninfo    = " . $t->attribute('info') 
		if ($t->attribute('info')) ;
	    
	    print "\ncmdline = " . decode_base64($t->attribute('cmdline')) ;
	    print "\nI/O     = " 
		. $t->attribute('stdin') 
		. " 1> " . $t->attribute('stdout') 
		. " 2> " . $t->attribute('stderr') ;

	}



	display_task($t) ;
    }
}

print "\n------------------------------------" ;
print "\n-------- JOB STATUS ----------------" ;
print "\n------------------------------------" ;
print "\nstatus = " . $doc->attribute('status') ;
print "\nstate  = " . $doc->attribute('state') ;
print "\nbegin  = " . POSIX::strftime('%F %T', localtime($doc->attribute('start_date'))) ; 
print "\nduration  = " . ($doc->attribute('sync_date') - $doc->attribute('start_date')) . "s"; 
print "\nmaxtime   = " . $doc->attribute('maxtime') . "s" if ($verbose) ;
print "\n------------------------------------\n" ;

display_task($doc) ;

print "\n------------------------------------\n" ;

exit 0 ;
# EOF
