
package url_dump;


sub do_get {
   local($debug, $thisurl, $outfile) = @_;
   local ($subhost, $destfilename,$junk) = &parse_href($thisurl);

   # return() syntax is   $start_place, $start_point, $start_port 
   $subhost =~ s/(http:|file:|telnet:|news:|gopher:|wais:)\/\///i;
   $#destpath = 0;
   $destname = "";
   if ($debug >=4 ) {
       print "subhost is $subhost\n";
       print "destfilename is $destfilename\n";
   };
   @destpath = split("/",$destfilename);
   if ($debug >=4 ) {
      print "destpath array contains @destpath \n";
   };
   if (substr($destfilename,-1,1) eq "/") {
       if ($debug >=4 ) {
           print STDOUT "I think $destfilename ends in '/'\n";
       };
       $destname = "index.html";
   } else {
       $destname = $destpath[$#destpath];
       if ($debug >=4 ) {
           print STDOUT "destfilename ends in $destname\n";
       };
   };
   if ($debug >= 4) { print "destination file is called $destname\n"; }
   if ( ! -d $subhost) {
       mkdir ($subhost,777) || print "Could not create directory $subhost!\n";
       $chresult = chmod 0755, "$subhost";
       if ($main'debug >=4 ) {
           print "chmod(); result $chresult on $subhost\n";
       }
   }
   $creatpath = $subhost;
   if ($#destpath > 0) {
       for ($i=1; $i <$#destpath; $i++) {
           $creatpath = $creatpath."/".$destpath[$i];
           if ($main'debug >=5) { print "creating $creatpath \n";}
           mkdir($creatpath,777) ;
           $chresult = chmod 0755, "$creatpath";
           if ($main'debug >=5) {
                print "chmod(); result $chresult on $creatpath\n";
           }
       };
   };
   $destfilename = $creatpath."/".$destname;
   if ($debug >=4) {
       print STDOUT "writing to $destfilename\n";
   };
   if ($debug >=6) {
       print "\nOutfile:\n---\n$outfile\n---\n\n";
   };
   if ( -e $destfilename) { 
       if ($main'debug >=2) {
           print "ERROR: $destfilename already exists!\n"; 
       }
       $retvar=1;
       return ($retvar); 
   }
   open (SYSTST, ">$destfilename"); select SYSTST;  $|=1;
   printf(SYSTST "%s", $outfile); ### change here
   close SYSTST;
   if ($debug >=6) {
       print $STDOUT "\n---\n\n$outfile\n\n----\n";
   }
   if ($debug >=4 ) {
       print STDOUT "wrote $destfilename\n";
   }
   $retvar=0;
   return ($retvar);
}
#===============================================================
sub parse_href {
    local($ref) = @_ ;
    local ( $start_place, $start_point, $start_port ) = "";
    if ($debug >=2 ) {
        print "parsing $ref\n";
    }

    @parsed_ref = &url_parse'parse_url($ref);
    if ($debug >=4) {print "@parsed_ref\n";}
    if (@parsed_ref[0] eq "gopher") {
	#   return ($protocol, $server, $port, $gtype, $selector, $search);
        $start_place = @parsed_ref[0].":".@parsed_ref[1];
	$start_port  = @parsed_ref[2];
	$start_point = @parsed_ref[3].@parsed_ref[4].@parsed_ref[5];
    } else {
    if (@parsed_ref[0] eq "http") {
        $start_place = "http://".@parsed_ref[1];
	$start_port  = @parsed_ref[2];
	$start_point = @parsed_ref[3];
        if (@parsed_ref[4] ne "") { 
            $start_point == $start_point.".".@parsed_ref[4];
        }
    } else {
    if (@parsed_ref[0] eq "news") {
        $start_place = "news:".@parsed_ref[1];
	$start_port  = @parsed_ref[2];
	$start_point = @parsed_ref[3];

    } else {
    if (@parsed_ref[0] eq "telnet") {
	#   return($protocol, $host, $port, $user);
        $start_place = "telnet:".@parsed_ref[1];
	$start_port  = @parsed_ref[2];
	$start_point = @parsed_ref[3];

    } else {
    if (@parsed_ref[0] eq "file") {
        $start_place = "file:".@parsed_ref[1];
	$start_port  = @parsed_ref[2];
	$start_point = @parsed_ref[3];

    } else {
    if (@parsed_ref[0] eq "wais") {
	#   return ($protocol, $server, $port, $database, $search);
        $start_place = "wais:".@parsed_ref[1];
	$start_port  = @parsed_ref[2];
	$start_point = @parsed_ref[3]."?".@parsed_ref[4];
    } 
    }}}}}
    if ($debug >=2 ) {
        print "parser: returning $start_place,:$start_port, $start_point\n"; 
    }
    return ( $start_place, $start_point, $start_port );
}
1;
