#!/usr/bin/perl -w
use strict;
use vars qw(%opts $ua $lines_max @pagers @dists);
use Term::ReadLine;
use Getopt::Std;
use LWP::UserAgent;
use Config;
use IO::Handle;
# use the CPAN module to install distributions
use CPAN;
# if you want to use CPAN::QuickInstall to install
# distributions, which dispenses with getting the
# CPAN index files for explicitly named distributions,
# comment out the 'use CPAN;' line above and
# uncomment the 'use CPAN::QuickInstall' line below
# see 'perldoc spause' for details on getting CPAN::QuickInstall

#use CPAN::QuickInstall;

$lines_max = $ENV{LINES} || 20;
search_pagers();
getopts('a:m:d:r:c:hs', \%opts);
$ua = new LWP::UserAgent;
$ua->agent("$0/0.1 " . $ua->agent);
print "\n";
TYPE: {
 if ($opts{'a'}) {
   get_cpanid();
   search_shell() if defined $opts{s};
   last TYPE;
 }
 if ($opts{'m'}) {
   get_mod();
   search_shell() if defined $opts{s};
   last TYPE;
 }
 if ($opts{'d'}) {
   get_dist();
   search_shell() if defined $opts{s};
   last TYPE;
 }
 if ($opts{'r'}) {
   if ($opts{r} !~ /^\d+$/) {
     print "\nPlease specify the 'recent' option by an integer\n(representing the most recent uploads in days)\n\n";
     exit;
   }
   get_recent();
   search_shell() if defined $opts{s};
   last TYPE;
 }
 if ($opts{'c'}) {
   check_mod();
   search_shell() if defined $opts{s};
   last TYPE;
 }
 if (defined $opts{'s'}) {
   search_shell();
   last TYPE;
 }
 if (defined $opts{'h'}) {
   help();
   last TYPE;
   exit;
 }
 help();
}
print "\n";


sub search_shell {
 my $term = Term::ReadLine->new('PAUSE-search interface');
 my $rl_package = $term->ReadLine;
 my $prompt = 'spause> ';
 my $OUT = $term->OUT || '';
 select $OUT;
 my ($rl_avail);
 if ($rl_package eq "Term::ReadLine::Gnu") {
   $rl_avail = 'enabled';
 }
 else {
   if ($rl_package eq 'Term::ReadLine::Perl'  ||
       $rl_package eq 'Term::ReadLine::readline_pl') {
     $rl_avail = 'enabled';
   }
   else {
     $rl_avail = "available (get Term::ReadKey and"
       . " Term::ReadLine::[Perl|GNU])";
   }
 }
 my $number;
 print <<"END";

Interactive interface to search PAUSE via the web.
TermReadLine $rl_avail. Type 'help' or '?'  for help.

END
 while ( defined ($_ = $term->readline($prompt)) ) {
   last if /^\s*(quit|exit|q)\s*$/;
   if (/^\s*(h|help|\?)\s*$/) {
     shell_help();
     next;
   }
   print "\n";
 TYPE: {
     if (($opts{a} = $_) =~ s/^\s*a\s+(.*?)\s*$/$1/) {
       if ($opts{a} =~ /\S+/) {
         get_cpanid();
       }
       else {
         print "Please specify an author name or ID search term\n";
       }
       last TYPE;
     }
     if (($opts{m} = $_) =~ s/^\s*m\s+(.*?)\s*$/$1/) {
       if ($opts{m} =~ /\S+/) {
         get_mod();
       }
       else {
         print "Please specify a module name search term\n";
       }
       last TYPE;
     }
     if (($opts{d} = $_) =~ s/^\s*d\s+(.*?)\s*$/$1/) {
       if ($opts{d} =~ /\S+/){
         get_dist();
       }
       else {
         print "Please specify a distribution search term\n";
       }
       last TYPE;
     }
     if (($opts{r} = $_) =~ s/^\s*r\s+(.*?)\s*$/$1/) {
        if ($opts{r} =~ /\S+/) {
          get_recent();
        }
       else {
         print "Please specify a maximum age (in days)\n";
       }
       last TYPE;
     }
     if (($opts{c} = $_) =~ s/^\s*c\s+(.*?)\s*$/$1/) {
       if ($opts{c} =~ /\S+/) {
         check_mod();
       }
       else {
         print "Please specify a module search term\n";
       }
       last TYPE;
     }
     if (($number = $_) =~ s/^\s*install\s+(.*?)\s*$/$1/) {
       if ($number =~ /^\d+$/) {
         search_install($dists[$number]);
       }
       else {
         print "Please specify a number associated with the last successful search results\n";
       }
       last TYPE;
     }
     if (($number = $_) =~ s/^\s*l\s+(.*?)\s*$/$1/) {
       if ($number =~ /^\d+$/) {
         list_mods($dists[$number]);
       }
       else {
         print "Please specify a number associated with the last successful search results\n";
       }
       last TYPE;
     }
     print qq{Sorry - "$_" not understood. Type 'help' or '?' for help.\n} if /\S/;
   }
   $term->addhistory($_) if (defined $_  and /\S/);
   print "\n";
 }
}

sub get_dist {
 my ($count, $packname, $cpanid);
 format DIST =
[@<.] @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$count, $packname, $cpanid