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

=head1 sched_builder

A complete network scheduling system solution

=head2 DESCRIPTION

This program can edit a job file.

=head2 USAGE

    Usage : sched_builder [-h] -j job
     --help      : print this help
     --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.
 - XML::Mini
 - Gnome2
 - Gnome2::Canvas
 - Gtk2::GladeXML

=head2 AUTHOR

(C) 2004-2005 Eric Bollengier

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

=head2 LICENSE

    sched_builder, 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 SchedUI ;
use SchedUI::JobPane ;
use SchedUI::JobItem ;
use SchedUI::TaskItem ;
use Sched::NS ;
use XML::Mini ;

use Gnome2 ;
use Gtk2 ;

# parcours de l'arbre pour afficher tous les task

sub display_subtask
{
    my ($task, $parent_item) = @_ ;

    my $group =  Sched::NS::aa('=group=') ;	

    for my $t (@{ $task->getAllChildren('task') }) {
	my $ti = new SchedUI::TaskItem(job_uid => $parent_item->{job_uid},
				       xml => $t) ;

	$ti->display($group) ;

	my $l = Sched::NS::aa('=link-xml=') ;
	$l->add_line_between_obj($parent_item, $ti) ;

	display_subtask($t, $ti) ;
    }
}

sub display_comment
{
    my ($task) = @_ ;

    my $group =  Sched::NS::aa('=group=') ;	

    for my $t (@{ $task->getAllChildren('comment') }) {
	my $ci = new SchedUI::CommentItem(xml => $t) ;

	$ci->display($group) ;
    }
}
sub display_link
{
    my ($task) = @_ ;

    for my $t (@{ $task->getAllChildren('task') }) {
	my $l = Sched::NS::get_obj($t->attribute('id')) ;

	$l->update_link() ;

	display_link($t) ;
    }
}

sub HELP_MESSAGE
{

    print "Usage : $0 [-h] [-j job.xml]
    --job jobfile : file to load
" ;
    exit (1) ;
}

my $uid = Sched::NS::get_uid() ;

my $file_job = "<job id='$uid'/>" ;

use Getopt::Long ;
GetOptions("job=s"    => \$file_job,
	   "help"     => \&HELP_MESSAGE) ;

$XML::Mini::AutoSetParent = 1 ;
my $xml = new XML::Mini::Document() ;
$xml->parse($file_job) ;
$xml = $xml->getRoot()->getElement('job') if ($xml) ;

die "E : impossible de charger $file_job" if (!$xml) ;

Gnome2::Program->init ('sched_builder', '1.0');

my $j = new SchedUI::JobItem(xml => $xml) ;
Sched::NS::set_jobid($j->uid) ;

my $p = new SchedUI::JobPane() ;
$j->display($p->{canvas}->root) ;

if (-f $file_job) {
    Sched::NS::register_id('=file-job=', $file_job) ;
    $p->on_zoom_show_all() ;
} else {
    Sched::NS::register_id('=file-job=', '/dev/stdout') ;
}

display_subtask($xml, $j) ;
display_link($xml) ;
display_comment($xml) ;

Gtk2->main() ;

END {
    print $xml->toString() 
	if ($xml) ;
}

# EOF
