#!/usr/bin/perl

#    Copyright (c) 2007-2008 Dominique Dumont.
#
#    This file is part of Config-Model-Itself.
#
#    Config-Model is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser Public License as
#    published by the Free Software Foundation; either version 2.1 of
#    the License, or (at your option) any later version.
#
#    Config-Model 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
#    Lesser Public License for more details.
#
#    You should have received a copy of the GNU Lesser Public License
#    along with Config-Model; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
#    02110-1301 USA

use strict ;
use warnings ;

use Config::Model;
use Getopt::Long ;
use Pod::Usage ;
use Log::Log4perl ;
use Config::Model::Itself ;
use Tk ;
use Config::Model::TkUI ;
use Config::Model::Itself::TkEditUI ;

# lame tracing that will be replaced by Log4perl
use vars qw/$verbose $debug $VERSION/ ;

$VERSION = sprintf "1.%04d", q$Revision: 841 $ =~ /(\d+)/;

$verbose = 0;
$debug = 0;

my $system_log4perl_conf_file = '/etc/log4config-model-edit.conf' ;
my $user_log4perl_conf_file = 'log4config-model-edit.conf' ;
my $fallback_conf = << 'EOC';
log4perl.logger.ConfigModel=WARN, A1
log4perl.appender.A1=Log::Dispatch::File
log4perl.appender.A1.filename=/tmp/ConfigModel.log
log4perl.appender.A1.mode=append
log4perl.appender.A1.layout=Log::Log4perl::Layout::SimpleLayout
EOC

my $log4perl_conf 
    = -e $user_log4perl_conf_file   ? $user_log4perl_conf_file 
    : -e $system_log4perl_conf_file ? $system_log4perl_conf_file 
    :                                 \$fallback_conf ;

Log::Log4perl::init($log4perl_conf);

use lib qw/lib/ ;

my $dev_model_dir = 'lib/Config/Model/models/';
my $root_model ;
my $trace = 0 ;


=head1 NAME

config-model-edit -  Graphical model editor for Config::Model

=head1 SYNOPSIS

  config-model-edit [options] -model Xorg

=head1 DESCRIPTION

config-model-edit will provides a Perl/Tk graphical interface to edit
configuration models that will be used by Config::Model.

Config::Model is a general purpose configuration framework based on
configuration models (See L<Config::Model> for details).

This configuration model is also expressed as structured data. This
structure data is structured and follow a set of rules which are
described for humans in L<Config::Model>.

The structure and rules documented in L<Config::Model> are also expressed
in a model in the files provided with L<Config::Model::Itself>.

Hence the possibity to verify, modify configuration data provided by
Config::Model can also be applied on configuration models using the
same user interface as L<config-edit>.

The model editor program is config-model-edit.

=head1 USAGE

By default, C<config-model-edit> will try to load a model file from
C<lib/Config/Model/models>. If no model is found, C<config-model-edit>
will try to load installed models (i.e. located in
C</usr/share/perl/...>). 

Modified models will be saved (by default) in
C<$PWD/lib/Config/Model/models/>.

You can override this behavior with option C<-dir>.

When you specify a C<-model> options, only configuration models matching
this options will be loaded. I.e.

  config-model-edit -model Xorg

will load models C<Xorg> (file C<Xorg.pl>) and all other C<Xorg::*> like
C<Xorg::Screen> (file C<Xorg/Screen.pl>).

=head1 Options

=over

=item -model

Mandatory option that specifies the configuration data to be
edited. By default, installed models will be edited. If you are
running E.g. this command:

  config-edit -model Fstab

will look for C</etc/config-model.d/Fstab.pl> model file. See
L<Config::Model> for more details.

=item -if

Specify the user interface type. 

=over

=item *

C<shell>: provides a shell like interface.  See L<Config::Model::TermUI>
for details.

=item *

C<curses>: provides a curses user interface (If
Config::Model::CursesUI is installed).

=back

=item -verbose

Be (very) verbose

=item -debug

Provide debug infos.

=item -trace

Provides a full stack trace when exiting on error.

=item -force-load

Load file even if error are found in data. Bad data are discarded

=back


=cut

my $man = 0;
my $help = 0;
my $force_load = 0;
my $model_dir ;

my $result = GetOptions (
			 "dir=s"            => \$model_dir,
			 "model=s"          => \$root_model,
			 "verbose!"         => \$verbose,
			 "debug!"           => \$debug,
			 "trace!"           => \$trace,
			 "man!"             => \$man,
			 "help!"            => \$help,
			 "force_load!"      => \$force_load,
			);

pod2usage(2) if not $result ;
pod2usage(1) if $help;
pod2usage(-verbose => 2) if $man;

Config::Model::Exception::Any->Trace(1) if $trace ;

die "Unspecified root configuration model (option -model)\n"
  unless defined $root_model ;

my $wr_model_dir = $model_dir || $dev_model_dir ;

if (! -d $wr_model_dir) {
    mkdir $wr_model_dir, 0755 || die "can't create $wr_model_dir:$!";
}

my $meta_model = Config::Model -> new();

my $meta_inst 
  = $meta_model->instance (root_class_name => 'Itself::Model' ,
			   instance_name   => $root_model.' model' ,
			   force_load      => $force_load,
			  );

my $meta_root = $meta_inst -> config_root ;

# now load model

my $rw_obj = Config::Model::Itself -> new(model_object => $meta_root ) ;

my $read_model_dir = $model_dir || $dev_model_dir ;

if (not -e $read_model_dir.'/'.$root_model.'.pl') {
    $read_model_dir =  $INC{'Config/Model.pm'} ;
    $read_model_dir =~ s/\.pm//;
    $read_model_dir .= '/models' ;
}

$rw_obj -> read_all( model_dir  => $read_model_dir, 
		     force_load => $force_load ,
		     root_model => $root_model ) ;

my $write_sub = sub { 
    my $wr_dir = shift || $wr_model_dir ;
    $rw_obj->write_all(model_dir => $wr_dir);
} ;

my $mw = MainWindow-> new ;
$mw->withdraw ;

my $cmu = $mw->ConfigModelEditUI (-root      => $meta_root,
				  -store_sub => $write_sub,
 				  -read_model_dir => $read_model_dir,
 				  -write_model_dir => $wr_model_dir,
				  -model_name => $root_model,
				 ) ;

&MainLoop ; # Tk's

=head1 AUTHOR

Dominique Dumont, ddumont at cpan dot org

=head1 SEE ALSO

L<Config::Model::Model>, 
L<Config::Model::Instance>, 
L<Config::Model::HashId>,
L<Config::Model::ListId>,
L<Config::Model::WarpedNode>,
L<Config::Model::Value>

=cut




