#!/usr/bin/perl
# uploader script
# $id:$
#


use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use Text::TagTemplate;

BEGIN {
	require 'upload.pl';    
	require 'tags.pl';
}

my $CONTRIBDIR  = '/www/Detritus/uploads';
my $TEMPLATEDIR = '/www/Detritus/templates';
$| = 1;
umask(0002);
$q = new CGI;
$q->import_names;

print $q->header;

my $sizelimit = 250000;    #  max size of uploaded file.
my $target = time . $$;
my $template= "$TEMPLATEDIR/upload.html";
my $outputfile;

# do ERROR-checking and print any ERRORs before printing form (if applicable)
# submit the data and we're done.  

if ($q->param()) {      

      # error check 
      if($Q::soundfile) {
      	 $Q::soundfile =~ /\.(\w+$)/;  # get the extension
	 $target .= ".$1";
      } else {
         $ERROR .= "no sound file chosen.";
      }
      
      
      
      if(!$ERROR) {  
	 my ($error, $file) = &SubmitFile($Q::soundfile, "$CONTRIBDIR/" . $target, 'mp3|jpg',$q, $sizelimit);
	 warn " target is $target";	    
	 $ERROR .= $error if($error);	    
	 
	 unless($file) { $ERROR .= "No successful uploads.\n<br>" };
	 
	
	 if(!$ERROR) {
	   
	    $MESSAGE .= "<h2>OK, file has been added. thank you!</h2>";
	    
	    # now send an email to steev 	    
   	    open(MAIL, '| /usr/sbin/sendmail -t steev@detritus.net');
   	    print MAIL <<"EOM";
To: steev\@detritus.net
From: The Detritus Uploader <uploader\@detritus.net>
Subject: an upload for the streaming lounge

An upload has occurred. 
file: $file 
user: $Q::name, $Q::email
EOM
	 } else {
	    unlink($file);
	 }
      }
      
      # if there's no ERRORs , write the html
      if (!$ERROR)  {
         $template = "$TEMPLATEDIR/results.html"; 	 
	 
      }  
  
}  # end of big block

# print the results, one way or the other.
$PARSER->template_file($template);
print $PARSER->parse_file();
