#!/usr/bin/perl -d
# BG easy file transfer program
# Usage: bgftp [ -u usr ] [ -p passwd ] [ hostname ]
# { [ ( 
# KH, January 1994
# Version 1.00 is for DECnet and FTP only.

require "getopts.pl";

$prompt = "bgftp> ";
do Getopts('u:p:');

# general environment
$usr = $opt_u;
$pw = $opt_p;
$sys = $ARGV[0];
$ok = 0;

# DECnet environment
$dnusr = $usr? $usr: $ENV{"NET_USER"}? $ENV{"NET_USER"}: "";
$dnpw = $pw? $pw: "";
$dnsys = $sys? $sys: "";
$dndev = "dka0";
$dndir = "login";

# FTP environment
$ftpusr = $usr? $usr: $ENV{"USER"}? $ENV{"USER"}: "";
$ftppw = $pw? $pw: "";
$ftpsys = $sys? $sys: "";
$ftpdir = $ftpusr? "~$ftpusr": "/";

# DECnet commands
$dn{"!"}	= "shell:! [ command ]";
$dn{"?"}	= "help:? [ command ]";
$dn{"bye"}	= "bye:Exit bgftp";
$dn{"close"}	= "dnclose:Close the connection";
$dn{"cd"}	= "dncd:Change relative dir on rem host";
$dn{"exit"}	= "bye:Exit bgftp";
$dn{"gct"}	= ":get remote-ct-files:dnmget:ct";
$dn{"get"}	= "dnget:get remote-file [ local-file ]";
$dn{"ggrc"}	= ":get remote-grc-files:dnmget:grc";
$dn{"glc"}	= ":get remote-lc-files:dnmget:lc";
$dn{"help"}	= "help:help [ command ]";
$dn{"lcd"}	= "lcd:Change dir on local host";
$dn{"ls"}	= "dnls:List files";
$dn{"lls"}	= "lls:List files on local host";
$dn{"lpwd"}	= "lpwd:Print working dir on local host";
$dn{"mget"}	= "dnmget:mget remote-files";
$dn{"mput"}	= "dnmput:mput local-files";
$dn{"pct"}	= ":put local-ct-files:dnmput:ct";
$dn{"pcps"}	= ":put local-cps-files:dnmput:cps";
$dn{"put"}	= "dnput:put local-file [ remote-file ]";
$dn{"pwd"}	= "dnpwd:Print working dir on remote host";
$dn{"open"}	= "dnopen:Establish a connection";
$dn{"setdef"}	= "dnsetdef:Set default dir on remote host";
$dn{"show"}	= "show:Show context of bgftp";
$dn{"switch"}	= "switch:Switch context of bgftp";
$dn{"user"}	= "dnusr:Identify an account";

# FTP commands
$ftp{"!"}	= "shell:! [ command ]";
$ftp{"?"}	= "help:? [ command ]";
$ftp{"bye"}	= "bye:Exit bgftp";
$ftp{"close"}	= "ftpclose:Close the connection";
$ftp{"cd"}	= "ftpcd:Change relative dir on rem host";
$ftp{"exit"}	= "bye:Exit bgftp";
$ftp{"gct"}	= ":get remote-ct-files:ftpmget:ct";
$ftp{"get"}	= "ftpget:get remote-file [ local-file ]";
$ftp{"ggrc"}	= ":get remote-grc-files:ftpmget:grc";
$ftp{"glc"}	= ":get remote-lc-files:ftpmget:lc";
$ftp{"help"}	= "help:help [ command ]";
$ftp{"lcd"}	= "lcd:Change dir on local host";
$ftp{"ls"}	= "ftpls:List files";
$ftp{"lls"}	= "lls:List files on local host";
$ftp{"lpwd"}	= "lpwd:Print working dir on local host";
$ftp{"mget"}	= "ftpmget:mget remote-files";
$ftp{"mput"}	= "ftpmput:mput local-files";
$ftp{"pct"}	= ":put local-ct-files:ftpmput:ct";
$ftp{"pcps"}	= ":put local-cps-files:ftpmput:cps";
$ftp{"put"}	= "ftpput:put local-file [ remote-file ]";
$ftp{"pwd"}	= "ftppwd:Print working dir on remote host";
$ftp{"open"}	= "ftpopen:Establish a connection";
$ftp{"show"}	= "show:Show context of bgftp";
$ftp{"switch"}	= "switch:Switch context of bgftp";
$ftp{"user"}	= "ftpusr:Identify an account";

# DOS commands 	#NOTYET
$dos{"!"}	= "shell:! [ command ]";
$dos{"?"}	= "help:? [ command ]";
$dos{"bye"}	= "bye:Exit bgftp";
$dos{"exit"}	= "bye:Exit bgftp";
$dos{"get"}	= "dosget:get remote-file [ local-file ]";
$dos{"help"}	= "help:help [ command ]";
$dos{"ls"}	= "dosls:List files";
$dos{"mget"}	= "dosmget:mget remote-files";
$dos{"mput"}	= "dosmput:mput local-files";
$dos{"put"}	= "dosput:put local-file [ remote-file ]";
$dos{"show"}	= "show:Show context of bgftp";
$dos{"switch"}	= "switch:Switch context of bgftp";

# The context
@contexts=("dn", "ftp", "dos");
%context = %dn;	

# Main interpreter loop
print $prompt;
while (<STDIN>) {
  chop;
  #s/^!/!  /;
  #s/^\?/?\  /;
  @arg = split(/ /, $_);
  $thiscmd = $arg[0];
  $fun = "";
  ($ncmds, $cmds) = &matchcmd($thiscmd);
  if (!$ncmds) {
    if ($thiscmd) {
      print "Illegal command: $thiscmd\n";
    }
  } elsif ($ncmds == 1) {
    ($fun, $what, $mfun, $marg) = split(/:/, $dn{$cmds});
    if ($mfun && &$mfun($marg) == $ok) {
      print "$thiscmd: $what, done.\n"; 
    } elsif (&$fun == $ok) {
      print "$thiscmd: $what, done.\n"; 
    }
  } else {
    print "Equivocal command: $thiscmd: $cmds\n";
  }
  print $prompt;
}

# DECnet routines
sub dnopen {
  $dnsys = $arg[1];
  0;
}

sub dncd {
  if ($arg[1]) {
    $dndir .= "." . ($arg[1] eq ".."? "-": $arg[1]);
  } else {
    $dndir = "login";
  }
  0;
}

sub dnpwd {
  print "$dnsys::$dndev:[$dndir]\n";
  0;
}

sub dnsetdef {
  local($defdev, $defdir);
  if (!$arg[1]) {
    $dndir = "login";
  } elsif ($arg[1] =~ /:/) {
    ($defdev, $defdir) = split(/:/, $arg[1]);
    if ($defdir =~ /\[.*\]/) {
      $dndir=$defdir;
      if ($defdev) {
        $dndev = $dev;
        0;
      }
    } else {
      print "Illegal default: $defdev:$defdir\n";
      1;
    }
  } elsif ($arg[1] =~ /\[.*\]/) {
    $dndir=$arg[1];
  } else {
    print "Illegal default: $arg[1]\n";
    1;
  }
}

sub dnusr {
  if ($arg[1]) {
    $dnusr = $arg[1];
    $dnpw = 0;
    if ($arg[2]) {
      $dnpw = $arg[2];
      0;
    } else {
      &dngetpw;
    }
  } else {
    1;
  }
}

sub dnclose {
  $dnsys = "";
  0;
}

sub dnget {
  if (&dnok($dnsys, $dnusr, $dndev)) {
    if ($arg[1] && $arg[2]) {
      &dndoit("dncp -nv -u $dnusr -p $dnpw $dnsys::$dndev:[$dndir]$arg[1] ./$arg[2]");
    } elsif ($arg[1]) {
      &dndoit("dncp -nv -u $dnusr -p $dnpw $dnsys::$dndev:[$dndir]$arg[1] .");
    } else {
      print "No remote-file specified\n";
      1;
    }
  } else {
    1;
  }
}

sub dnput {
  if (&dnok($dnsys, $dnusr, $dndev)) {
    if ($arg[1] && $arg[2]) {
      &dndoit("dncp -nv -u $dnusr -p $dnpw ./$arg[1] $dnsys::$dndev:[$dndir]$arg[2]");
    } elsif ($arg[1]) {
      &dndoit("dncp -nv -u $dnusr -p $dnpw ./$arg[1] $dnsys::$dndev:[$dndir]$arg[1]");
    } else {
      print "No local-file specified\n";
      1;
    }
  } else {
    1;
  }
}

sub dnls {
  local($rs, $item);
  if (&dnok($dnsys, $dnusr, $dndev)) {
    if ($arg[1]) {
      shift(@arg);
      foreach $item (@arg) {
        if (!($rs = &dndoit("dnls -u $dnusr -p $dnpw $dnsys::$dndev:[$dndir]$item"))) {
          last;
        }
      }
    } else {
      $rs = &dndoit("dnls -u $dnusr -p $dnpw $dnsys::$dndev:[$dndir]*.*");
    }
    rs;
  } else {
    1;
  }
}

sub dnmget {
  local($item, $bn, $dnspec);
  if (&dnok($dnsys, $dnusr, $dndev)) {
    if ($_[0] eq "ct") {
      $dnspec = "$dnsys::$dndev:[$dndir]*.$_[0]";
    } elsif ($_[0] eq "grc") {
      $dnspec = "$dnsys::$dndev:[$dndir]*.$_[0]";
    } elsif ($_[0] eq "lc") {
      $dnspec = "$dnsys::$dndev:[$dndir]*.$_[0]";
    } else {
      if ($arg[1]) {
        $dnspec = "";
        shift(@arg);
        foreach $item (@arg) {
          $dnspec .= "$dnsys::$dndev:[$dndir]$item ";
        }
      } else {
        $dnspec = "$dnsys::$dndev:[$dndir]*.*";
      }
    }

    1;
    foreach $item (`dnls -u $dnusr -p $dnpw $dnspec`) {
      chop($item);
      $bn = $item;
      $bn =~ s/^.*::.*:\[.*\]//;	# chop off the vms prefixes
      print "$bn? ";
      chop($nser = <STDIN>);
      if (!$nser) {
        $nser = "yes";
      }
      if ($nser =~ /^y/i) {
        if (&dndoit("dncp -u $dnusr -p $dnpw $item .") != $ok) {
          last;
        }
        if ($_[0] eq "gro" || $_[0] eq "lc") {
          if (!&tocps($bn)) {
            unlink($bn);
          } else {
            last;
          }
        } 
      }
    }
  } else {
    1;
  }
}

sub dnmput {
  local($item, $fspec);
  if (&dnok($dnsys, $dnusr, $dndev)) {
    if ($_[0] eq "ct" || $_[0] eq "cps") {
      $fspec="*.$_[0]";
    } else {
      if ($arg[1]) {
        $fspec="";
        shift(@arg);
        foreach $item (@arg) {
          $fspec .= "$item ";
        }
      } else {
        $fspec="*"
      }
    }

    1;
    foreach $item (`ls $fspec`) {
      chop($item);
      print "$item? ";
      chop($nser = <STDIN>);
      if (!$nser) {
        $nser = "yes";
      }
      if ($nser =~ /^y/i) {
        if ($_[0] eq "cps") {
          if (!&togrc($item)) {
            $item =~ s/\.$_[0]$/\.grc/;	# replace the the suffix
          } else {
            last;
          }
        } 
        if (&dndoit("dncp -u $dnusr -p $dnpw $item $dnsys::$dndev:[$dndir]$item") != $ok) {
          last;
        }
      }
    }
  } else {
    1;
  }
}

sub dnok {
  if (!$_[0]) {
    print "No DECnet peer system defined\n"; 0;
  } elsif (!$_[1]) {
    print "No DECnet account defined\n"; 0;
  } elsif (!$_[2]) {
    print "No VMS device defined\n"; 0;
  } else {
    1;
  }
}

sub dndoit {
  &dngetpw;
  system($_[0]);
}

sub dngetpw {
  while (!$dnpw) {
    print "Password for user $dnusr? ";
    chop($dnpw = <STDIN>);
  }
  0;
}

# FTP routines

sub ftpopen {
  $ftpsys = $arg[1];
  0;
}

sub ftpclose {
  $ftpsys = "";
  0;
}

sub ftpcd {
  if ($arg[1]) {
    if ($arg [1] =~ /^\//) {
      $ftpdir = $arg[1];
    } else {
      $ftpdir .= "/" . $arg[1];
    }
  } else {
    $ftpdir = "~ftpusr";
  }
  0;
}

# Conversions
sub tocps {
  if (($_[0] =~ /\.grc$/) || ($_[0] =~ /\.lc$/)) {
    print "converting $_[0] to .cps\n";
    $_[0] =~ /\.grc$/ ? system("grc_to_cps $_[0]"): system("lc_to_cps $_[0]");
  } else {
    1;
  }
}

sub togrc {
  if ($_[0] =~ /\.cps$/) {
    print "converting $_[0] to .grc\n";
    system("cps_to_grc $_[0]");
  } else {
    1;
  }
}

# General routines
sub help {
  local($ff, $cmd, @keys) = 1;
  local($fun, $what);
  unless ($arg[1]) {
    @keys = sort keys %context;
    foreach $cmd (@keys) {
      if ($ff % 2) {
        $fun1 = $cmd;
        ($fun, $what1) = split(/:/, $context{$cmd});
      } else {
        $fun2 = $cmd;
        ($fun, $what2) = split(/:/, $context{$cmd});
        write;
      }
      ++$ff;
    }
    unless ($ff % 2) {
      $fun2 = $what2 = "";
      write;
    }
  } else {
    ($ncmds, $cmds) = &matchcmd($arg[1]);
    if (!$ncmds) {
      print "No such command: $arg[1]\n"; 1;
    } elsif ($ncmds == 1) {
      ($fun1, $what1) = split(/:/, $context{$cmds});
      print "$arg[1]: $what1\n";
      0;
    } else {
      print "Equivocal command: $arg[1]: $cmds\n"; 1;
    }
  }
  0;
}

sub lls {
  system("ls -l");
}

sub lpwd {
  system("pwd");
}

sub shell {
  shift(@arg);
  system("@arg");
}

sub lcd {
  unless (chdir $arg[1]) {
    print "Could not change directory to $arg[1]\n"; 1;
  }
}

sub bye {
  print "Done.\n";
  exit;
}

sub matchcmd {
  local($ncmds, $cmds, $cmd, $full) = 0, "";
  foreach $cmd (keys %context) {
    $full = $cmd;
    while (length($cmd) >= 2 || ($cmd && ($_[0] eq "?" || $_[0] eq "!"))) {	# match at least 2 chars
      if ($_[0] eq $cmd) {
        $ncmds++;
        if (!$cmds) {
          $cmds = "$full";
        } else {
          $cmds .= " $full";
        }
      }
      chop $cmd;
    }
  } 
  ($ncmds, $cmds);
}

sub show {
  if (%context == %dn) {
    print "Context is DECnet\n"; 0;
  elsif (%context == %ftp) {
    print "Context is FTP\n"; 0;
  elsif (%context == %dos) {
    print "Context is DOS\n"; 0;
  } else {
    print "Context not found\n"; 1;
  }
}

sub switch {
  local($reply);
  if ($arg[1]) {
    $reply = $arg[1];
  } else {
    REPLY: {
    print "Context (@contexts)? ";
    chop($reply = <STDIN>);
    foreach $item (@contexts) {
      if ($item eq $reply) {
        last REPLY;
      } else {
        redo REPLY;
      }
    }
  }
  }
  if ($reply eq "dn") {
    %context = %dn;
    print "Context is now DECnet\n"; 0;
  } elsif ($reply eq "ftp") {
    %context = %ftp;
    print "Context is now FTP\n"; 0;
  } elsif ($reply eq "dos") {
    %context = %dos;
    print "Context is now DOS\n"; 0;
  } else {
    print "No such context\n"; 1;
  }
}

# Formats
format STDOUT = 
@<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$fun1, $what1,                           $fun2, $what2
.
# ) ] }
