#!/usr/bin/perl -w
######################################################################
# uachunk.pl
######################################################################
# Perl Power! - Michael Schilli 1998
######################################################################

######################################################################
# uachunk.pl URL localfile - fetch file morsel by morsel
######################################################################
use LWP::UserAgent;                   # WWW access
$| = 1;                               # do not buffer STDOUT output

($url, $localfile) = @ARGV;           # analyze command line
$#ARGV == 1 || usage("Wrong argument count");

$ua  = new LWP::UserAgent;            # create user agent and request
$req = HTTP::Request->new('GET', $url);

open(FILE, ">$localfile") || usage("Cannot open '$localfile'");

$response = $ua->request($req, 
                         sub { $data = shift;           # morsel
                               $total += length($data); # bytes up to now
                               print FILE $data;        # -> file
                               print "\r$total";        # -> display
                             }, 
                         1000);       # proceed by 1000 at a time
close(FILE);

die $response->as_string() if $response->is_error();

######################################################################
sub usage {
######################################################################
    ($prog = $0) =~ s#.*/##;

    print "$prog: @_\n";
    print "usage: $prog URL localfile\n";
    exit 0;
}

