#! /usr/bin/perl -w

# vim:syntax=perl

use strict;

use Locale::TextDomain qw/lire/;

use Lire::Logger;
use Lire::Config;
use Lire::Config::SpecParser;
use Lire::DlfSchema;
use Lire::ReportSpec;
use Lire::FilterSpec;
use Lire::ReportConfig;
use Lire::XMLSpecI18N;

use Getopt::Long;

sub usage {
    lr_err( @_,
            __(
"Usage: lr_spec2pot [--cfgspecdir <dir>]  [--defaultsdir <dir>]
                    [--reportsdir <dir>]* [--filtersdir <dir>]*
                    [--schemasdir <dir>]* <SPECS>+\n" ) );
}

sub setup_cfg_paths {
    my ( $specdir, $defaultsdir ) = @_;

    # Clear the spec path
    foreach my $dir ( Lire::Config->config_spec_path() ) {
        Lire::Config->del_config_spec_path_dir( $dir );
    }
    Lire::Config->add_config_spec_path_dir( $specdir );

    if ( $defaultsdir ) {
        foreach my $file ( Lire::Config->config_files() ) {
            Lire::Config->del_config_file( $file );
        }
        Lire::Config->add_config_path( $defaultsdir );
    }
    Lire::Config->init();
}

sub setup_cfg_var {
    my ( $listname, $varname, $dirs ) = @_;

    return unless defined $dirs;

    my $path = Lire::Config->get_var( $listname );
    my $dir_spec = $path->spec()->get( $varname );

    # Clear the path
    while ( $path->elements() ) {
        $path->remove(0);
    }

    # Adds the new dirs
    foreach my $dir ( @$dirs ) {
        $path->append( $dir_spec->instance( 'value' => $dir )  );
    }
}

sub load_spec {
    my $spec = $_[0];

    if ( $spec =~ /^filter:(.*?):(.*)/ ) {
        return Lire::FilterSpec->load( $1, $2 );
    } elsif ( $spec =~ /^report:(.*?):(.*)/ ) {
        return Lire::ReportSpec->load( $1, $2 );
    } elsif ( $spec =~ /^report_cfg:(.*?):(.*)/ ) {
        return new_from_file Lire::ReportConfig( $1, $2 );
    } elsif ( $spec =~ /^schema:(.*)/ ) {
        return Lire::DlfSchema::load_schema( $1 );
    } elsif ( $spec =~ /^config_spec:(.*)/ ) {
        my $file = $1;
        my $parser = new Lire::Config::SpecParser();
        $parser->merge_specification( $file );
        my $obj = $parser->configspec();
        $obj->xml_file( $file );
        return $obj
    } else {
        die __( "specs are strings of the form schema:<id>, filter:<schema>:<id>, report:<schema>:<id>, config_spec:<file> or report_cfg:<schema>:<file>\n" );
    }
}


my %opts = ();
my $success = GetOptions( \%opts, 'cfgspecdir=s', 'defaultsdir=s',
                          'reportsdir=s@', 'filtersdir=s@', 'schemasdir=s@' );

setup_cfg_paths( $opts{'cfgspecdir'},
                 $opts{'defaultsdir'} )
  if ( defined $opts{'cfgspecdir'} );

usage() unless $success;

usage( "missing one or more spec arguments\n" )
  unless @ARGV;

# Since this script is used from the source tree before any Lire installation
# exists, we cannot 'use' Lire::Program. Otherwise it will try to parse the
# not-yet installed configuration files.
require Lire::Program;

eval {
    setup_cfg_var( 'lr_schemas_path', 'schemas', $opts{'schemasdir'} );
    setup_cfg_var( 'lr_reports_path', 'reports', $opts{'reportsdir'} );
    setup_cfg_var( 'lr_filters_path', 'filters', $opts{'filtersdir'} );
};
lr_err( __x( 'An error occured: {error}', 'error' => $@ ) ) if $@;

my @specs = ();
foreach my $spec ( @ARGV ) {
    eval {
        push @specs, load_spec( $spec );
    };
    lr_warn( __x( "Error loading spec '{spec}': {error}",
                  'spec' => $spec, 'error' => $@ ) )
      if $@;
}

eval {
    my $extractor = new Lire::XMLSpecI18N( @specs );
    $extractor->generate_catalog();
};
lr_err( __x( 'An error occured: {error}', 'error' => $@ ) ) if $@;

# Local Variables:
# mode: cperl
# End:

__END__

=pod

=head1 NAME

lr_spec2pot - extracts strings for L10N from Lire XML specifications

=head1 SYNOPSIS 

FIXME

=head1 DESCRIPTION

FIXME


=head1 VERSION

$Id: lr_spec2pot.in,v 1.2 2004/01/25 15:44:33 flacoste Exp $

=head1 AUTHORS

Francis J. Lacoste <flacoste@logreport.org>
Wolfgang Sourdeau <wolfgang@logreport.org>

=head1 COPYRIGHT

Copyright (C) 2004 Stichting LogReport Foundation LogReport@LogReport.org

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 (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html or write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.

=cut


