#!/usr/bin/perl

# Ce script permet la construction des images ROM/feed de
# - openzaurus-354X     : la branche stable
# - openzaurus-unstable : la branche de developpement
# - angstrom            : le future d'openzaurus
# - zaurusfr            : distrib personnalisee
# - poky                : image pour qemu (voir http://projects.o-hand.com/poky )

use strict;
use warnings;

use File::Basename;
use Cwd 'abs_path';
use lib abs_path( dirname($0) ) . "/suza";
use lib abs_path( dirname($0) )."/modules";
use OpenEmbeded::Builder;
use Getopt::Long;
 
my $VERSION="0.4";

# Lecture de la configuration
    my $fileName = dirname($0) . "/oe.cfg";
    my $settings = new Settings::Simple($fileName);
    if (!defined $settings) {
        exit(1);
    }    

# construction du logger
    Logging::LoggerManager::Instance->configure($settings);
    my $logger = Logging::LoggerManager::Instance->logger(__PACKAGE__);

# welcome
    $logger->section("OpenEmbedded Builder $VERSION");

# construction du builder
    my $builder = new OpenEmbeded::Builder($settings);

# Les options en ligne de commande
    my %options=();
    GetOptions(
                "help!"=>\$options{help},
                "machine=s"=>\$options{machine},
                "image=s"=>\$options{image},
                "user=s"=>\$options{user},
                "stuff=s"=>\$options{stuff});
            
    # re-paramétrage de l'objet builder
    if ( $options{machine} ) {
        $builder->machine($options{machine});
    } 
    
    if ( $options{user} ) {
        $builder->user($options{user});
    } 
    
    if ( $options{stuff} ) {
        $builder->stuffPath($options{stuff});
    } 
    
    if ( $options{image} ) {
        $builder->stuffPath($options{image});
    } 

    # aide
    if ( $options{help} ) {
        usage($builder);
        exit(0);
    }
                    
    # lecture de l'action
    if (!@ARGV) {
       $logger->severe("Error! No action defined");
       usage($builder);
       exit(1);
    }

    $builder->start(@ARGV);
    

# Utilisation du script
sub usage {
    my $builder=shift;
    my %actions=$builder->actions;
    $logger->section("Usage");
    $logger->info("   $0 [--stuff stuffPath] [--machine MACHINE] [--image IMAGE] [--user USER] ACTION [ACTION_PARAMETERS]");
    $logger->section("Possible actions");
    foreach (keys %actions) {
        $logger->info("   ".$_." ".$actions{$_});
    }
}
