#/usr/local/bin/perl
#
# A very simple gopher client in Perl 4.036
#
require "chat2.pl";
require "win.pl";

$args = "gocamel feenix.metronet.com 70 1/perlinfo";
$pager = "/usr/local/bin/less";
$mailer = "/usr/local/bin/mush -h";
$title ="                      GOCAMEL: A perl script delivery tool",
$footer =
 "Enter Item Number or :  f - forward page  b - back page  q - quit  u - up";

($junk, $host, $port, $path) = split(' ',$args);
# get initial menu , start recursion
&get_selection($host,$path,$port,$title,$footer);
print "\e"."["."2"."J";    # clean up
print "\e"."["."0"."m";    # restore good mode
##############################################################################

sub send_request{
  local($path, $port,$host) = @_;
  local($line,$i) = (1,0);
  local(@page);
  $timeout = 2;
  local($N) = &chat'open_port($host,$port);
  if(!defined $N){ die "open control: $!";}
  &chat'print($N,"$path\r\n");
  while(1){                 #While receiving data
   $line = &chat'expect($N, $timeout, '^(.*)\r?\n', '"$1"' );
   last if ($line =~ /^[\.]/);
   $page[$i++] = $line;
  }
&chat'close($N);
@page;
}

sub get_selection{
local($host,$path,$port,$title,$footer) = @_;
local($line,$selection,$i,$j,$b,$cur,@A,@types,@text,@paths,@hosts,@ports);
local(@cur_types,@cur_text,@cur_paths,@cur_hosts,@cur_ports,@tmpfile);
local(@win1)=(0,21,79,1,0,7,$title,$footer);
print "\e"."["."2"."J";
&win'title(@win1);
&win'footer(@win1);

local(@buffer) = &send_request($path, $port, $host); 
foreach $b (@buffer){
   if($j==19){$j=0;$i++;}
   @A = split(/\t/,$b);
   $A[0] =~  s/^[0-9]// && ( $types[$i] = $1); 
   $types[$i].=" #"; $text[$i].=($j+1)."> "."$A[0]#"; 
   $paths[$i].="$A[1]#"; $hosts[$i].="$A[2]#";$ports[$i].="$A[3]#";
   $j++;
}
while($j<19){
   $types[$i].=" #"; $text[$i].=" #"; 
   $paths[$i].=" #"; $hosts[$i].=" #";$ports[$i].=" #";
   $j++;
}
$cur=0;
while($selection ne "q"){
  @cur_text = split(/#/,$text[$cur]);
  &win'refresh(scalar(@cur_text),@cur_text,scalar(@win1),@win1);
  chop($selection=&win'getdata(10,23,"Enter Your Selection    > ",7));
  if( $selection =~ /^[fF]/){
    if($cur < $i) {$cur++;next;}
  }
  elsif ($selection =~ /^[bB]/){
    if($cur > 0) {$cur--;next;}
  }
  elsif ($selection =~ /^[uU]/){
    return;
  }
  elsif (($selection < 20) && ($selection > 0)){
   $selection--;
   @cur_hosts=split(/#/,$hosts[$cur]); @cur_paths=split(/#/,$paths[$cur]);
   @cur_ports=split(/#/,$ports[$cur]); @cur_types=split(/#/,$types[$cur]);
   if(($cur_paths[$selection] =~ /^1/) || ($cur_paths[$selection] =~ /^ftp/)){
     &get_selection($cur_hosts[$selection],$cur_paths[$selection],
     $cur_ports[$selection],$cur_text[$selection],$footer);
     print "\e"."["."2"."J";
     &win'title(@win1);
     &win'footer(@win1);
     next;
   }
   elsif($cur_paths[$selection] =~ /^7/){
     chop($keyword =&win'getdata(10,23,"Keyword to search for?    > ",7));
     if(length($keyword) < 1){ 
       &win'getdata(10,23,"Bad Keyword, hit Return > ",7);
       next;
     }
     &get_selection($cur_hosts[$selection],$cur_paths[$selection]."\t".$keyword,
     $cur_ports[$selection],$cur_text[$selection],$footer);
     print "\e"."["."2"."J";
     &win'title(@win1);
     &win'footer(@win1);
     next;
   }
   elsif($cur_types[$selection] == 0 || $cur_paths[$selection] =~ /^.*0\//){
     $cur_paths =~ s#^.*/##;
     @tmpfile = &send_request($cur_paths[$selection], $cur_ports[$selection],
                            $cur_hosts[$selection]); 
     open(LOOKATME,">/tmp/gocam$$")||die "couldnt open that file, dude(ette)"; 
     foreach $line (@tmpfile){
      print LOOKATME $line;
      print LOOKATME "\n";
     }
     close LOOKATME;
     system "clear";
     system "$pager /tmp/gocam$$";
     print "\e"."["."2"."J";
     chop($keyword =&win'getdata(10,23,
          "s to save, m to mail, or anything else to continue   > ",7));
     if($keyword eq "m"){
       system "$mailer /tmp/gocam$$";  
       system "rm -f /tmp/gocam$$"; 
       print "\e"."["."2"."J";
       &win'title(@win1);
       &win'footer(@win1);
       next;}
     elsif($keyword eq "s"){
      print "\e"."["."2"."J";
      chop($keyword =&win'getdata(11,23, 
           "Enter name of file to save to   > ",7));
      if (length($keyword)<2){print "error there, bud"; next;}
      system "mv /tmp/gocam$$ $ENV{'HOME'}/$keyword";
      &win'title(@win1);
      &win'footer(@win1);
      next;
     }
     else{
      system "rm -f /tmp/gocam$$"; 
      print "\e"."["."2"."J";
      &win'title(@win1);
      &win'footer(@win1);
      next;
     }
   }
  else{print" Not implemented, sorry bout that"; next;}
  }
  else{next;}
}
print "\e"."["."2"."J";
exit 0;
}