#!/usr/bin/env perl
# This file belongs to TLPM v2.14, TeX Live Package Manager
# Public Domain, P.Jackowski@gust.org.pl

BEGIN{$tlpm_version="2.14";$tlpm_os="i386-linux"}

# the main tlpm script

use TLPM::common;
use TLPM::constants;

use TLPM::pkg;
use TLPM::reg;
use TLPM::cmdl;

use TLPM::list;
use TLPM::incl;
use TLPM::blng;
use TLPM::inst;
use TLPM::uninst;
use TLPM::check;
use TLPM::help;
use TLPM::info;

%commands =
(
        'exit' => sub {exit(0)},
        'quit' => sub {exit(0)},
        'list' => \&list,
        'incl' => \&incl,
        'blng' => \&blng,
        'inst' => \&inst,
      'uninst' => \&uninst,
       'check' => \&check,
        'info' => \&info,
        'help' => \&help,
#        'Help' => \&readme,
#      'readme' => \&readme,
     'version' => \&version,
         'cmd' => \&sys
);

# interactive mode

sub prompt
{
      local ($cmd_name,@cmd_args,$cmd_line);
      while($true)
      {
            print STDOUT $tlpm_prompt;
            $cmd_line = <STDIN>;
            exit(0) unless defined $cmd_line; # otherwise looop after 'echo <command> | tlpm'
            chomp $cmd_line;
            &execute_cmd();
      }
}

# batch mode

sub batch
{
      local ($cmd_name,@cmd_args,$cmd_line);
      local *INP;
      &norm_path($batch_file);
      $batch_file = &rel2abs($batch_file,$curr_dir);
      open(INP,"<$batch_file") or exit $error{'wrong_inp'} -> ($batch_file);
      $message -> ("Executing batch file '$batch_file'");
      foreach $cmd_line (<INP>)
      {
            exit(0) unless defined $cmd_line;
            chomp $cmd_line;
            next if $cmd_line =~ /\A\s*(%|#|\z)/; # comment
            print STDOUT $tlpm_prompt . $cmd_line . "\n";
            &execute_cmd();
      }
      close INP;
}

sub execute_cmd
{
      my $err;
      &resolve_env($cmd_line);
      &set_out($cmd_line);
      &open_out();
      @cmd_args = &parse_cmd($cmd_line);
      return(0) unless @cmd_args; # return prompt on ENTER
      $cmd_name = shift @cmd_args;
      if(defined $commands{$cmd_name})
      {
            $err = $commands{$cmd_name} -> (@cmd_args);
      }
      else
      {
            $err = $error{'wrong_cmd'} -> ($cmd_name,'help');
      }
      &close_out();
      return $err;
}

# the main procedure

sub main
{
      my ($arg,@args);
      @args = &parse_cmd(join(" ",@ARGV));
      while(defined($arg = shift @args))
      {
               $arg eq ''                    and next
#            or &no_opt($arg)                 and return $error{'wrong_cmd'} -> ($arg,'tlpm --help')
            or &no_opt($arg)                 and do {$tl_binaries{$arg} = $true}
            or &is_opt($arg,'d','directory') and do {$tl_target = shift @args;1}
            or &is_opt($arg,'s','source')    and do {$tl_source = shift @args;1}
            or &is_opt($arg,'v','version')   and return $heeelp -> ('tlpm_banner')
            or &is_opt($arg,'h','help')      and return &help(shift @args)
            or &is_opt($arg,'H','Help','readme') and return &readme()
            or &is_opt($arg,'b','batch')     and do {$batch_file  = shift @args;$batch_mode = 1}
#            or &is_opt($arg,'q','quiet')     and do {$quiet_mode = 1}
            or &is_opt($arg,'D','debug')     and do {$debug_mode = 1}
            or &rem_opt($arg)                and return $error{'wrong_opt'} -> ($arg,'tlpm --help');
      }
      unless(defined $tl_source){$tl_source = $ENV{'tl_source'}}# ...or remains undefined
      unless(defined $tl_target){$tl_target = $ENV{'tl_target'}}
      $tl_source_indeed = $false; # we need to check the source availability anyway
      unless(defined %tl_binaries) # new; installing not only native binaries
      {
            %tl_binaries = ($tlpm_os => $true);
      }
      $tl_target_indeed = $false;
      if($batch_mode)
      {
            &batch($batch_file);
      }
      else
      {
            $heeelp -> ('tlpm_banner');
            $heeelp -> ('type_help');
            &prompt();
      }
}

#$quiet_mode = 0;
$debug_mode = 0;
$batch_mode = 0;
$batch_file = '';

&main();

# uff