#! /usr/bin/perl
require '/usr/local/news/lib/innshellvars.pl';

# simpleftp
# Author: David Lawrence <tale@isc.org>
#
# Fetch files to the local machine based on URLs on the command line.
# Uses perl's ftp.pl, which in turn uses chat2.pl.  Not very speedy.
#   There is a better perl ftp module in the libnet package,
# but it would require the INN admin to install yet another package.
#   There is a perl www module which can do ftp in the libwww package,
# but that has the same issue as the libnet module.
#   INN's configure searches for ncftp, wget, linx, et cetera,
# but they're all system add-ons, so this is provided.
#   Perl5 is already required by other parts of INN, it only took a half hour
# to write this, so this was the easiest way to go for a backup plan.
#   This script is nowhere near as flexible as libwww.  If you really need
# that kind of power, get libwww and use it.  This is just sufficient for what
# INN needed.

# NOTE: ftp.pl and chat2.pl are not -w safe (uninitialized values errors)

use strict;
use Sys::Hostname;

# $ENV{'PATH'} = '/bin:/usr/bin:/usr/ucb';

# trick ftp.pl into not demanding this file.
$INC{'sys/socket.ph'} = 1;
require 'ftp.pl';

$0 =~ s(.*/)();

my $usage = "Usage: $0 ftp-URL ...\n";

@ARGV
  or die $usage;

for (@ARGV) {
  m(^ftp://)
    or die $usage;
}

my ($user, $pass, $lasthost);

$user = 'anonymous';
$pass = (getpwuid($<))[0] . '@' . hostname;

# this will keep track of how many _failed_
my $exit = @ARGV;

for (@ARGV) {
  my ($host, $path) = m%^ftp://([^/]+)(/.+)%;
  unless (defined $host && defined $path) {
    warn "$0: bad URL: $_\n";
    next;
  }

  # NOTE: URLs with usernames, passwords, ports or parameters are not supported

  if (defined $lasthost && $host ne $lasthost) {
    ftp::close();
    $lasthost = undef;
  }

  unless (defined $lasthost) {
    ftp::open($host, getservbyname('ftp', 'tcp') || 21, 0, 1)
      or next;
    ftp::login($user, $pass)
      or next;
    ftp::type("i")
      or next;
  }

  my $localfile = $path;
  $localfile =~ s(.*/)();
  ftp::get($path, $localfile)
    or next;

  $exit--;
  $lasthost = $host;
}

ftp::close()
  if defined $lasthost;

exit $exit;
