#! /usr/bin/perl -w

# vim:syntax=perl

use strict;

use lib '/usr/share/perl5';

use Locale::TextDomain qw/lire/;

use Lire::Logger qw/lr_err/;
use Lire::Config;
use Lire::Error qw/ an_error_occured invalid_superservice /;
use Lire::DlfSchema;
use Lire::ReportConfig;
use Lire::I18N qw/set_fh_encoding/;
use Getopt::Long;

sub usage {
    lr_err( @_,
            __(
"Usage: lr_report_cfg2xml <superservice> <name> <report_cfg_file>\n" ) );
}

sub setup_cfg_paths {
    my $specdir = $_[0];

    # 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 );
    Lire::Config->init();

    return;
}

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 )  );
    }

    return;
}

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

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

usage() unless $success;

usage() unless @ARGV == 3;

my ( $super, $name, $file ) = @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( an_error_occured( $@ ) ) if $@;

lr_err( invalid_superservice( $super ) )
  unless Lire::DlfSchema->has_superservice( $super );

eval {
    my $report_cfg = new_from_file Lire::ReportConfig( $super, $file );
    my $cfg = $report_cfg->as_config_value( $name );

    set_fh_encoding( \*STDOUT, 'utf-8' );
    print <<EOF;
<?xml version="1.0"?>
<!DOCTYPE lrcsml:config-spec PUBLIC
  "-//LogReport.ORG//DTD Lire Report Configuration Specification Markup Language V1.0//EN"
  "http://www.logreport.org/LRCSML/1.0/lrcsml.dtd">
<lrcsml:config-spec xmlns="http://www.logreport.org/LRCML/"
                    xmlns:lrcsml="http://www.logreport.org/LRCSML/">
 <lrcsml:report-config name="$name">
EOF
    $cfg->save_xml( \*STDOUT, 2 );
    print <<EOF;
 </lrcsml:report-config>
</lrcsml:config-spec>
EOF
};
lr_err( an_error_occured( $@ ) ) if $@;

# Local Variables:
# mode: cperl
# End:

__END__

=pod

=head1 NAME

lr_report_cfg2xml - Convert old style report configuration to XML

=head1 SYNOPSIS 

  lr_report_cfg2xml I<supservice> I<name> I<report_cfg_file> > I<xml_spec_file>

=head1 DESCRIPTION

The B<lr_report_cfg2xml> command converts a report configuration file
as used with Lire version 1.5 and below to a report configuration
template.

The script takes three mandatory arguments:

=over 4

=item superservice

The superservice used by the report configuration file.

=item name

The name of the template that will be generated.

=item report_cfg_file

The report configuration file to convert.

=back

It writes on STDOUT a Lire Configuration Specification file. This
specification file contains one report-config element named I<name>.
Its default value will be identical to the content of the report
configuration file. If you install that file in the
I<sharedir>/lire/templates, you will be able to instantiate report
configuration using that template from the lire(1) interface.

=head1 VERSION

$Id: lr_report_cfg2xml.in,v 1.4 2006/07/23 13:16:33 vanbaal Exp $

=head1 AUTHOR

 Francis J. Lacoste <flacoste@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.

=cut


