#!/usr/bin/perl

=head1 NAME

osgish - Admin's OSGi Shell

=cut

use Getopt::Long qw(GetOptionsFromArray);
use strict;
use JMX::Jmx4Perl::Config;
use Config::General;
use OSGi::Osgish;

=head1 SYNOPSIS

  osgish [options] 

  osgish --help

  osgish --version

 Options:
   --server <server>       URL or symbolic name of OSGi server to connect to
   --user <user>           Credential used for authentication   
   --password <pwd>  
   --proxy <url>           URL to proxy
   --proxy-user <user>     Authentication information for a proxy
   --proxy-password <pwd>
   --config                Path to an optional configuration file (default: ~/.osgish)
   --color [yes|no]        Color option (default: yes)

=head1 DESCRIPTION

B<Osgish> is the administrator's shell for OSGi. It's focus is on simple usage
for common administrative tasks. There are many other shells for OSGi as well,
most (if not all) implemented in Java. Osgish unique features are

=over 4

=item *

Readline and history support based on GNU Readline/History as known from other
shells like 'bash'. When GNU Readline is not available, a pure Perl Module is
used instead.

=item *

Context sensitive argument completion, e.g. on bundle symbolic names. 

=item * 

Colored output (can be switched off)

=item *

Multi-Server support

=item * 

Remote operation via HTTP(S)

=back

=cut

my %opts = ();
my $result = GetOptions(\%opts,
                        "server|s=s",
                        "user|u=s","password|p=s",
                        "proxy=s",
                        "proxy-user=s","proxy-password=s",
                        "config=s",
                        "version!",
                        "color=s",
                        "help|h!" => sub { &Getopt::Long::HelpMessage() }
                       );

if ($opts{version}) {
    print "osgish ",$OSGi::Osgish::VERSION,"\n";
    exit(0);
}

# Parse configuration files
my $jmx_config = new JMX::Jmx4Perl::Config(&get_config($opts{config}));

# Create global context object
my $osgish = new OSGi::Osgish(config => $jmx_config,args => \%opts);

# Let the shell run
$osgish->run;

sub get_config {
    my $file = shift || $ENV{HOME} . "/.osgish";
    my $ret = {};

    # Merge if servers from ~/.j4p
    my $default =  {};
    $default = { new Config::General(-ConfigFile => $ENV{HOME} . "/.j4p",-LowerCaseNames => 1)->getall } 
      if -e $ENV{HOME} . "/.j4p";
    if ($file && -e $file) {
        $ret = { new Config::General(-ConfigFile => $file,-LowerCaseNames => 1,-DefaultConfig => $default)->getall };        
    } 
    return $ret;
}








