#!/usr/bin/perl
#
# $Id$
#
use strict;
use warnings;

use FindBin;

BEGIN {
   push @INC, "$FindBin::Bin/../lib"; # Metabrik Perl modules
   push @INC, "$FindBin::Bin/../../repository/lib"; # Metabrik main repository for Briks

   # Everything should be fine now, but we prefer to check anyway.
   eval("use Metabrik::Core::Context;");
   if ($@) {
      die("[F] metabrik: you have to set PERL5LIB environment variable like:\n\n".
          'shell$ export PERL5LIB=/path/to/metabrik/core/lib'.
          "\n\n".
          "ERROR: $@\n"
      );
   }
}

use Getopt::Long;

# Default values
my %lopts = (
   verbose => 2,
   debug => 0,
   no_splash => 0,
   rc => $ENV{HOME}."/.metabrik_rc",
   script_rc => $ENV{HOME}."/.metabrik_script_rc",
);
GetOptions(
   "script=s" => \$lopts{script},
   "rc=s" => \$lopts{rc},
   "script-rc=s" => \$lopts{script_rc},
   "verbose=i" => \$lopts{verbose},
   "debug=i" => \$lopts{debug},
   "no-splash" => \$lopts{no_splash},
) or usage();

my $context = Metabrik::Core::Context->new;

$context->brik_init
   or die("[F] metabrik: context init: failed\n");

$context->set('core::log', 'level', $lopts{verbose});

$context->set('core::log', 'debug', $lopts{debug});
$context->set('core::context', 'debug', $lopts{debug});
$context->set('core::shell', 'debug', $lopts{debug});
$context->set('core::global', 'debug', $lopts{debug});

if (defined($lopts{script})) {
   if (-f $lopts{script_rc}) {
      $context->use('shell::rc')
         or die("[F] metabrik: use: shell::rc: failed\n");

      $context->set('shell::rc', 'file', $ENV{HOME}."/.metabrik_script_rc");
      $context->set('shell::rc', 'debug', $lopts{debug});

      $context->run('shell::rc', 'load')
         or die("[F] metabrik: run: shell::rc: load: failed\n");
      $context->run('shell::rc', 'exec', '$RUN')
         or die("[F] metabrik: run: shell::rc: exec: failed\n");
   }
   else {
      print("[!] metabrik: script-rc: file not found: $lopts{script_rc}\n");
   }

   $context->use('shell::script')
      or die("[F] metabrik: use: shell::script: failed\n");

   $context->set('shell::script', 'debug', $lopts{debug});
   $context->set('shell::script', 'file', $lopts{script});

   $context->run('shell::script', 'load')
      or die("[F] metabrik: run: shell::script: load: failed\n");
   $context->run('shell::script', 'exec', '$RUN')
      or die("[F] metabrik: run: shell::script: exec: failed\n");
}
else {
   $context->use('shell::rc')
      or die("[F] metabrik: use: shell::rc: failed\n");

   $context->set('shell::rc', 'file', $lopts{rc});
   $context->set('shell::rc', 'debug', $lopts{debug});

   $context->run('shell::rc', 'load')
      or die("[F] metabrik: run: shell::rc: load: failed\n");
   $context->run('shell::rc', 'exec', '$RUN')
      or die("[F] metabrik: run: shell::rc: exec: failed\n");

   unless ($lopts{no_splash}) {
      $context->run('core::shell', 'splash')
         or die("[F] metabrik: run: core::shell: splash: failed\n");
   }

   $context->run('core::shell', 'cmdloop');
}

exit(0);

sub usage {
   print<<EOF

Usage: metabrik [options]

   --rc <file>           use specified rc file (default: ~/.metabrik_rc)
   --no-splash           don't print the splash screen
   --verbose <0|1|2|3>   verbosity level (default: 2)
   --debug <0|1>         enable debugging (default: 0)
   --script <file>       execute given script
   --script-rc <file>    use specified script rc file (default: ~/.metabrik_script_rc)

EOF
;

   exit(0);
}
