#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#
# $Id: paraget-child,v 1.4 2001/05/10 04:57:08 lrclause Exp $
#

use strict;
use English;
use Getopt::Long;
use IO::File;

use URI;
use Net::FTP;

use constant DEBUG => 0;

my %options;

GetOptions( \%options, 'url=s', 'offset=i', 'output=s', 'size=i' );

my $output     = $options{output}  or die "no output file given";
my $url_option = $options{url}     or die "no URL given";
my $offset     = $options{offset};
my $size       = $options{size};

die "no offset" unless defined $offset;

my $url = URI->new( $url_option );

my $protocol = $url->scheme();
my $host     = $url->host()     or die "no host in URL $url";
my $user     = $url->user()     || 'anonymous';
my $pass     = $url->password   || 'anonymous@anonymous.org';
my $path     = $url->path()     or die "no path to get in URL $url";

die "protocol of $url is not supported" unless $protocol =~ /^ftp$/i;

my $ftp = Net::FTP->new( $host ) or die "error connecting to $host: $EVAL_ERROR";

( $ftp->login( $user, $pass )
  and $ftp->binary()
)
  or die "error getting $url: ", $ftp->message();

if (defined $size) 
{
    my $our_size = $ftp->size( $path );
    die "error getting $url: ", $ftp->message()
	if not defined $our_size;

    die "size mismatch: $our_size should be $size" unless $our_size == $size;
}
$ftp->get( $path, $output, $offset )
    or die "error getting $url: ", $ftp->message();

