#!/usr/local/bin/perl -I/home/users/dberansky/perl/lib

use Cwd;
use CGI qw(fatalsToBrowser);
use File::Find;

$q = new CGI;
$e = new CGI(\%ENV);
$0=~ /(.*\/)[^\/]*/;
$PWD = $1;
($baseURL = "http://".$ENV{HTTP_HOST}.$q->script_name()) =~ s/[^\/]*$//;

require $PWD."miscLib.pl";

#print $q->header;
#$e = new CGI(\%ENV);
#print $e->dump;
$username = $q->cookie(-name=>'username');
$currDir = $q->cookie(-name=>'dir');

if( !$username || !$currDir )
{
   print $q->redirect( $baseURL."browser.pl" );
}

setCookies();

$q->param('fileName') =~ /([^\/]+)$/;
$fileName = $1;

#print "Saving $fileName to $currDir";

# Copy a binary file to somewhere safe
#open (OUTFILE,">$currDir/$fileName") ;
#open (OUTFILE,">upload") ;
if( !OUTFILE )
      {
         print $q->header;
         print "can't save to $currDir/$fileName";
      }

while ($bytesread=read($q->param('fileName'),$buffer,1024)) 
{
   print OUTFILE $buffer;
}

print $q->header( -cookie=>$cookie1, -Location=>$baseURL."browser.pl" );

