#!/usr/local/bin/perl

use CGI qw(:standard);
use CGI::Carp;

sub die_page {
	print header();
	print start_html("Error on upload");
	print strong(@_);
	print strong("<P>Click back to return");
	exit;
}

my $query = new CGI;

my $ultimate;

my $destination = $query->param('refresh_destination')
	or die_page("No return page specified");

my $table = $query->param('mvc_data_file')
	or die_page("No return page specified");

my $file;

# Process the form if there is a file name entered
my $dir = $query->param('mvc_vendroot') 
	or die_page("No VENDROOT specified"); 

#my $tmpfile;

if ($file = $query->param('filename')) {
	$ultimate = "$dir/upload/$table";
	umask 0;
	open(OUT, ">$ultimate") 
		or die_page("create $ultimate: $!\n");
	while(<$file>) {
		print OUT $_;
		$tried = 1;
	}
	close OUT
		or die_page("Couldn't close $ultimate");
	unless($tried) {
		die_page("Never got to a file part for $file! " . "to copy to $ultimate " .
			$query->uploadInfo($file)->{'Content-Type'} . " " .
			$query->uploadInfo($file)->{'Content-Length'}
			);
	}
	close OUT;
}

print redirect($destination);
