#!/usr/bin/perl

use File::stat;

##############################################################################
# Please change the following values to ones that are appropriate for your
# environment.
##############################################################################

# Set path:
$ENV{'PATH'} = "/bin:/usr/bin:".$ENV{'PATH'};

# Set temp directory:
$tmpdir = "/tmp";

# Set pickup directory:
$pickupdir = "/usr/public/pickup";
 
# Set mail server:
#$mailhost = "mail.phys.virginia.edu";

# Set ps filter:
#$psfilter = "/usr/lib/magicfilter/psonly300-filter";

# Set metamail mailer:
#$metamail_mailer = "/usr/bin/mailto";

# Set berkeley mailer:
#$berkeley_mailer = "/bin/mail";

# Set pickup host:
#$pickup_url = "http://print.phys.virginia.edu/pickup";

# Set max file size to mail (bytes):
$max_mail_size = 1440000;

# Set file expiration warning, for large files:
$expire_warning = "The file will be available for 2 hours.";

##############################################################################
# No Changes should be necessary below this line.
##############################################################################

# Parse arguments:
while ( $i = shift ARGV) {
  ARGSPEC: for ($i) {
      /^-/ && do {
	  
	  $flag  = $i;
	  $flag  =~ /^-(.).*$/;
	  $flag  = $1;
	  
	  $value = $i;
	  $value =~ /^-.(.*)$/;
	  $value = $1;

	SWITCH: for ($flag) {
	    /n/ && do {
		$User = $value;
		last SWITCH;
	    };
	    /h/ && do {
		$Host = $value;
		last SWITCH;
	    };
	    /e/ && do {
		$File = $value;
		last SWITCH;
	    };
	}
	  
	  last ARGSPEC;
      };

      $AcctFile = "$i";

  }
}

# Check to make sure it's postscript. Convert it to PS if not:
#open ( PSFILTER, "|$psfilter > $tmpdir/$$.pdfmail.ps" );
#while ( <> ) {
#    print PSFILTER "$_";
#}
#close ( PSFILTER );

# Convert postscript to pdf:
#system ( "ps2pdf -dMaxSubsetPct=100 -dCompatibilityLevel=1.2 -dSubsetFonts=true -dEmbedAllFonts=true $tmpdir/$$.pdfmail.ps $tmpdir/$$.pdfmail.pdf" );
system ( "ps2pdf13 $tmpdir/$$.ps $tmpdir/$$.pdf" );

# Get size of pdf file:
#$size = stat( "$tmpdir/$$.pdfmail.pdf" )->size;

# Compute checksum:
#$checksum = (split(/\s+/,`md5sum $tmpdir/$$.pdfmail.pdf`))[0];

# If the file is small, mail it as an attachment. 
# Otherwise, send the user a link to a pickup area:
#if ( $size < $max_mail_size ) {

# Mail pdf file to recipient:
#`$metamail_mailer -s "PDF mail from $User at $Host" $User\@$mailhost <<EOF
#The following attachment has been sent to you via a PS --> PDF converter.
#(size $size bytes)
#~/50000000
#~*
#1
#$tmpdir/$$.pdfmail.pdf
#application/pdf ; name="pdfmail.pdf"
#y
#				Thank you.
#EOF`;
#
#unlink ( "$tmpdir/$$.pdfmail.pdf" );
#
#} else {
#
# Put PDF into pickup dir:
#rename ( "$tmpdir/$$.pdf", "$pickupdir/$$.pdf" );

# Write html wrapper:
#open ( HTMLFILE, ">$pickupdir/$checksum.html" );
#print HTMLFILE "<html><head><title>Converted PDF file</title></head><body bgcolor=white>\n";
#print HTMLFILE "<h1>Converted PDF File</h1>\n";
#print HTMLFILE "Your file is available here: <a href=$checksum.pdf><img alt=\"Click to Download\" src=/icons/pdf.gif></a>\n"; 
#($size bytes)\n";
#print HTMLFILE "<br><b>Note:</b> In some browsers you may need to press the shift key while clicking";
#print HTMLFILE "on the icon in order to download the file.\n";
#print HTMLFILE "<hr><i>$expire_warning</i>\n";
#print HTMLFILE "</body></html>\n";
#close ( HTMLFILE );

# Make files readable:
#chmod ( 0744, "$pickupdir/$checksum.html" );
#chmod ( 0744, "$pickupdir/$$.pdf" );

# Mail pdf file to recipient:
#`$berkeley_mailer -s "PDF mail from $User at $Host" $User\@$mailhost <<EOF
#The following message has been sent to you by a PS --> PDF converter.
#Your PDF document can be picked up at the following Web address:
#
# $pickup_url/$checksum.html
#
#$expire_warning
#(File size is $size bytes)
#EOF`;

unlink ( "$tmpdir/$$.pdf" );

#}

unlink ( "$tmpdir/$$.ps" );

print "done\n";
exit;




