#! /usr/local/bin/perl

#require 'sys/socket.ph'

$|=1 ;
$[=0 ;

$agent="NPAC/fetch" ;
$rn="\r\n";
$update="Pragma: no-cache${rn}";
$proxy = "" ;
$post = 0 ;

#$host = "wonderdog.npac.syr.edu" ;
#$port = 4096 ;
#print $host, $port, "\n" ;

&init_sock($host,$port) || die "$0: Cannot initialize socket\n";

&transfer("$path", "/$file") ;

sub transfer {
    local($url, $file) = @_ ;
#print $url, "  ", $file, "\n" ;
    local($status,$code,$info,$line,$length,$html,$redirect,$update,$data,
      $bytes,$step,$slice);

    socket (SOCKET, 2, 2, $local_prot) || die ("No socket");
    bind (SOCKET, $local_sock) || die ("Can't bind");
    connect (SOCKET, $remote_sock);
    $_=select(SOCKET); $|=1; select($_);
#    if ($verbose>3) {
#	print(($post?"POST":"GET") . " $proxy$url HTTP/1.0${rn}" .
#	      "User-Agent: $agent${rn}Accept: */*${rn}$update");
#	print  "Authorization: $userpass${rn}" if $userpass;
#	if ($post) {
#	    print  "Content-Length: " . length($post_data) .
#		"${rn}Content-Type: application/x-www-form-urlencoded" .
#		    "${rn}${rn}" . $post_data;
#	} else {
#	    print  "${rn}";
#	}
#    }

#    print SOCKET ($post?"POST":"GET") . " $proxy$url HTTP/1.0${rn}" .
#	"User-Agent: $agent${rn}Accept: */*${rn}$update";
#    print SOCKET "Authorization: $userpass${rn}" if $userpass;
    print SOCKET ("GET") . " $file HTTP/1.0${rn}" .
       "User-Agent: $agent${rn}Accept: */*${rn}$update";
    if ($post) {
	print SOCKET "Content-Length: " . length($post_data) .
	    "${rn}Content-Type: application/x-www-form-urlencoded${rn}${rn}" .
		$post_data;
    } else {
	print SOCKET "${rn}";
    }
    chop($status=<SOCKET>);
    $status=~s/\r$//;
    undef $code;
    ($code,$info)=($status=~m!^HTTP\S+\s+(\d\d\d)\s*(.*)$!i);
    print "Status: $code - $info -\n" if $verbose;
#    print "Status: $code - $info -\n" ;
    if ($code==200 || $code==302) {
	undef $length;
	undef $redirect;
	undef $update;
	chop($line=<SOCKET>);
	$line=~s/\r$//;
	while (defined $line) {
	    last if $line=~/^$/;
	    print "$line\n" if $verbose>1;
	    $html=1 if $line=~m!^Content-Type:\s+[^/\s]+/html.*!i;
	    $length=$1 if $line=~/^Content-length:\s*(\d+)/i;
	    $redirect=$1 if $line=~/^Location:\s+(\S+)/i;
	    $update=$1 if $line=~/^Last-modified:\s*(.*)/i;
	    chop($line=<SOCKET>);
	    $line=~s/\r$//;
	}
	if ($code==302 && defined($redirect)) {
	    $html=$redirect;
	    while (defined($data=<SOCKET>)) { } # chomp!
#	} elsif (open(F,$stdout ? ">&STDOUT" : ">".&real($tmpfile))) {
	} elsif (open(F,">$tmpfile")) {
	    if (defined($length)) {
		print "$length bytes " if $verbose;
		$step=$slice=($length/10);
	    } else {
		print "Unknown length " if $verbose;
		$step=$slice=2048;
	    }
	    $bytes=0;
	    binmode(SOCKET);
	    binmode(F);
	    while (defined($data=<SOCKET>)) {
		print F $data;
		$bytes+=length($data);
		print "." while ($verbose && $bytes>=$step && ($step+=$slice));
	    }
	    close(F);
#	    &move($file) unless $stdout;
#	    &set_date($file,$update) if ((defined($update)) && !$stdout);
#	    if (defined($length) && $bytes!=$length) {
#		&log("($bytes<>$length)");
#	    }
#	    print " $bytes bytes" if ($verbose && !defined($length));
#	    print "\n" if $verbose;
	} else {
	    $code=100;
	    $info="Transfer $!";
	}
    }
    close(SOCKET);
}

sub init_sock {
    local($host,$port)=@_;
print $host, " ", $port, "\n" ;
    $sockaddr='S n a4 x8';
    chop($local_host=`hostname`);
    $local_prot=(getprotobyname('tcp'))[2];
($d1, $d2, $local_prot) = getprotobyname("tcp") ;
#    $local_sock=pack($sockaddr,&AF_INET,0,(gethostbyname($local_host))[4]); #P4
#    $remote_sock=pack($sockaddr,&AF_INET,$port,(gethostbyname($host))[4]); #P4
    $local_sock=pack($sockaddr,2,0,(gethostbyname($local_host))[4]); #P4
    $remote_sock=pack($sockaddr,2,$port,(gethostbyname($host))[4]); #P4
}
