File:  [LON-CAPA] / loncom / interface / printout.pl
Revision 1.134: download - view: text, annotated - select for diffs
Tue Mar 18 10:29:56 2008 UTC (16 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
- Interal refactoring of convert_image's final filename generation.
- Simplification of path generation so it's easier to understand
 (replace a regsub with call to dirname).

    1: #!/usr/bin/perl
    2: # CGI-script to run LaTeX, dvips, ps2ps, ps2pdf etc.
    3: #
    4: # $Id: printout.pl,v 1.134 2008/03/18 10:29:56 foxr Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: use lib '/home/httpd/lib/perl';
   30: use LONCAPA::loncgi;
   31: use File::Path;
   32: use File::Basename;
   33: use File::Copy;
   34: use IO::File;
   35: use Image::Magick;
   36: use Apache::lonhtmlcommon();
   37: use Apache::lonnet;
   38: use Apache::loncommon();
   39: use Apache::lonlocal;
   40: use Apache::lonmsg();
   41: use LONCAPA::Enrollment;
   42: use LONCAPA::Configuration;
   43: 
   44: use strict;
   45: 
   46: my $busy_wait_timeout = 30; 
   47: 
   48: #   Determine if a user is operating as a student for this course/domain.
   49: #Parameters:
   50: #    none
   51: #Implicit:
   52: #    $env{request.role} contains the role under which this user operated this
   53: #                       this request.
   54: sub is_student {
   55:     return ($env{'request.role'}=~/^st\./);
   56: }
   57: 
   58: #
   59: #   Debugging:  Dump the environment for debugging.
   60: #
   61: sub dumpenv  {
   62:     print "<br />-------------------<br />";
   63:     foreach my $key (sort (keys %env)) {
   64: 	print "<br />$key -> $env{$key}";
   65:     }
   66:     print "<br />-------------------<br />";
   67: }
   68: 
   69: #
   70: #   This sub sends a message to the appropriate person if there was an error
   71: #   rendering the latex  At present, there's only one case to consider:
   72: #   a student printing inside a course results in messages to the course coordinator.
   73: #Parmaeters:
   74: #    identifier -  The unique identifier of this cgi request.
   75: #    badresource-  Filepath to most likely failing 
   76: #    logfile    -  The contents of the log file (included in the message).
   77: #    texfile    -  reference to an array containing the LaTeX input file
   78: #                  (included in the message).
   79: #Implicit inputs:
   80: #   From the environment:
   81: #       cgi.$identifier.user     - User doing the printing.
   82: #       cgi.$identifier.domain   - Domain the user is logged in on with printing.
   83: #       cgi.$identifier.courseid - Id of the course (if this is a course).
   84: #       cgi.$identifier.coursedom- Domain in which course was constituted.
   85: #       cgi.$identifier.resources - List of resource URL's for which the print
   86: #                                  was attempted.
   87: # 
   88: sub send_error_mail {
   89:     my ($identifier, $badresource, $logfile, $texfile) = @_;
   90:     my $user     = $env{"cgi.$identifier.user"};
   91:     my $domain   = $env{"cgi.$identifier.domain"};
   92:     my $courseid = $env{"cgi.$identifier.courseid"};
   93:     my $coursedom= $env{"cgi.$identifier.coursedom"};
   94:     my $resources= $env{"cgi.$identifier.resources"};
   95: 
   96:     #  resource file->URL
   97:     #
   98:     my $badurl = &Apache::lonnet::declutter($badresource);
   99: 
  100:     # &dumpenv();
  101: 
  102: 
  103: 
  104:     #  Only continue if there is a user, domain, courseid, course domain
  105:     #  and resources:
  106: 
  107:     if(defined($user)       && defined($domain) && defined($courseid) &&
  108:        defined($coursedom)  && defined($resources) ){
  109: 	   
  110: 	#  Only mail if the conditions are ripe for it:
  111: 	#  The user is a student in the course:
  112: 	#
  113: 	
  114: 	if (&is_student()) {
  115: 	    # build the subject and message body:
  116: 	    # print "sending message to course coordinators.<br />";
  117: 
  118: 	    # Todo: Convert badurl into a url from file path:
  119: 
  120: 	    my $subject  = "Error [$badurl] Print failed for $user".'@'.$domain;
  121: 	    my $message .= "Print failed to render LaTeX for $user".'@'."$domain\n";
  122: 	    $message    .= "  User was attempting to print: \n";
  123: 	    foreach my $resource (split(/:/,$resources)) {
  124: 		$message    .= "       $resource\n";
  125: 	    }
  126: 	    $message    .= "--------------------LaTeX logfile:------------ \n";
  127: 	    $message    .= $logfile;
  128: 	    $message    .= "-----------------LaTeX source file: ------------\n";
  129: 	    
  130: 	    foreach my $line (@$texfile) {
  131: 		$message .= "$line\n";
  132: 	    }
  133: 	    my (undef, %receivers) = &Apache::lonmsg::decide_receiver(undef, 0,
  134: 								      1,1,1);
  135: 	    # print "<br /> sending...section:  $env{'request.course.sec'}";
  136: 	    foreach my $dest (keys %receivers) {
  137: 		# print "<br /> dest is $dest";
  138: 		my @destinfo = split(/:/,$dest);
  139: 		my $user = $destinfo[0];
  140: 		my $dom  = $destinfo[1];
  141: 
  142: 		&Apache::lonmsg::user_normal_msg($user, $dom,
  143: 						 $subject, $message);
  144: 		
  145: 		# No point in looking at the return status as there's no good
  146: 		# error action I can think of right now (log maybe?).
  147: 	    }
  148: 	}
  149:     }
  150: }
  151: 
  152: $|=1;
  153: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
  154:     print <<END;
  155: Content-type: text/html
  156: 
  157: <html>
  158: <head><title>Bad Cookie</title></head>
  159: <body>
  160: Your cookie information is incorrect.
  161: </body>
  162: </html>
  163: END
  164:     return;
  165: }
  166: 
  167: my %perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')};
  168: &Apache::lonlocal::get_language_handle();
  169: &Apache::loncommon::content_type(undef,'text/html');
  170: $env{'request.noversionuri'} = '/cgi-bin/printout.pl';
  171: print(&Apache::loncommon::start_page('Creating PDF'));
  172: 
  173: my $identifier = $ENV{'QUERY_STRING'};
  174: my $texfile = $env{'cgi.'.$identifier.'.file'};
  175: my $laystyle = $env{'cgi.'.$identifier.'.layout'};
  176: my $numberofcolumns = $env{'cgi.'.$identifier.'.numcol'};
  177: my $paper = $env{'cgi.'.$identifier.'.paper'};
  178: my $selectionmade = $env{'cgi.'.$identifier.'.selection'};
  179: my $tableofcontents = $env{'cgi.'.$identifier.'.tableofcontents'};
  180: my $tableofindex = $env{'cgi.'.$identifier.'.tableofindex'};
  181: my $advanced_role = $env{'cgi.'.$identifier.'.role'};
  182: my $number_of_files = $env{'cgi.'.$identifier.'.numberoffiles'}+1;
  183: my $student_names = $env{'cgi.'.$identifier.'.studentnames'};
  184: my $backref = &Apache::lonnet::unescape($env{'cgi.'.$identifier.'.backref'});
  185: 
  186: 
  187: my @names_pack=();
  188: if ($student_names=~/_END_/) {  
  189:     @names_pack=split(/_ENDPERSON_/,$student_names);
  190: }
  191: if ($backref) {
  192:     print('<p>'.&mt("[_1]Return[_2] to editing resource.",
  193: 		    "<a href=\"$backref\"><b>","</b></a>").'</p>');
  194: }
  195: my $figfile = $texfile;
  196: $figfile =~ s/^(.*_printout)_\d+_\d+_\d+\.tex/$1\.dat/;
  197: my $duefile = $texfile;
  198: $duefile =~ s/^(.*_printout)_\d+_\d+_\d+\.tex/$1\.due/;
  199: 
  200: 
  201: #-------------------------------------------------------------------------------------
  202: #
  203: #   Each print may have associated with it a file that contains a set of figures
  204: #   that need to be converted to .eps from whatever form they were in when included
  205: #   in the resource.  The name of the figure file is in $figfile.  If it exists,
  206: #   it contains the names of the files that need to be converted, one per line.
  207: #
  208: 
  209: if (-e $figfile) {
  210:     # print "$figfile exists\n";
  211:     my %done_conversion;
  212:     my $temporary_file=IO::File->new($figfile) || die "Couldn't open fig file $figfile for reading: $!\n";
  213:     my @content_of_file = <$temporary_file>;
  214:     close $temporary_file;  
  215:     my $noteps;
  216:     my %prog_state;
  217:     if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Converting Images to EPS','Picture Conversion Status',$#content_of_file,'inline','80');  }
  218:     print('<br />');
  219:     foreach my $not_eps (@content_of_file) {
  220: 	chomp($not_eps);
  221: 	if ($not_eps ne '') {
  222: 	    $not_eps=~s|\/\.\/|\/|g;
  223: 	    if (!$done_conversion{$not_eps}) { #  Only convert multiple includes once.
  224: 		&convert_figure($not_eps);
  225: 		$done_conversion{$not_eps} = 1;
  226: 	    }
  227: 	}
  228:     }
  229:     if ($advanced_role) { 
  230: 	&Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); 
  231:     }
  232:     unlink($figfile);
  233: }
  234: #     End of figure conversion section:
  235: #
  236: #--------------------------------------------------------------------------------------------
  237: 
  238: 
  239: 
  240: 
  241:   #print "$texfile\n"; #name of the tex file for debugging only   
  242:   my @texfile=($texfile);
  243:   if ($number_of_files>1) {
  244:       @texfile=();
  245:       for (my $i=1;$i<=$number_of_files;$i++) {
  246: 	  my $new_texfile=$texfile;
  247: 	  $new_texfile=~s/\.tex//;
  248: 	  $new_texfile = sprintf("%s_%03d.tex", $new_texfile,$i);
  249: 	  push @texfile,$new_texfile;
  250:       } 
  251:   }
  252: 
  253: my $ind=-1;
  254: my %prog_state;
  255: if ($advanced_role) { %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Print Status','Class Print Status',$number_of_files,'inline','80'); }
  256: print "<br />";
  257: my $num_files = @texfile;
  258: foreach $texfile (@texfile) {
  259:   my $status_statement='';
  260:   my $link_text='download PDF';
  261:   $ind++;
  262:   my @stud_info=split(/_END_/,$names_pack[$ind]);
  263:   my @tempo_array=split(/:/,$stud_info[0]);
  264:   my $name;
  265:   my $name_range='';
  266:   if ($tempo_array[3]) {
  267:       $name=$tempo_array[3];
  268:       ($name_range) = split(/,/,$name, 2);
  269:   } else {
  270:       $name=$tempo_array[0].'@'.$tempo_array[1];
  271:       $name_range = $tempo_array[0];
  272:   }
  273:   if (($name ne "") && ($name ne '@') ) { # Could be printing codes...
  274:       $link_text='<b>'.$name.'</b>';
  275:       $status_statement.=$name;
  276:   }
  277:   if ($#stud_info>0) {
  278:       @tempo_array=split(/:/,$stud_info[-1]);
  279:       if ($tempo_array[3]) {
  280: 	  $name=$tempo_array[3];
  281: 	  my ($lastname) = split(/,/, $name,2);
  282: 	  $name_range .= "-".$lastname;
  283:       } else {
  284: 	  $name=$tempo_array[0].'@'.$tempo_array[1];
  285: 	  $name_range .= '-'.$tempo_array[0];
  286:       }
  287:       if (($name ne "") && ($name ne '@')) {
  288: 	  $link_text.=' - <b>'.$name.'</b>';
  289: 	  $status_statement.=' -  '.$name;
  290:   
  291:       }
  292:   }
  293:   if(($num_files > 1) && ($link_text eq 'download PDF')) { # Printing codes
  294:       $link_text = '<b>'.basename($texfile,'.tex').'.pdf</b>';
  295:       $status_statement .= basename($texfile);
  296:   }
  297:   $name_range =~ s/'//g;	# O'Neil -> ONeil e.g.
  298:   print "<br/>";
  299:   if ($advanced_role) { &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,'Creating PDF for '.$status_statement); }
  300:   #  This little piece of dirt puts username ranges into the original tex
  301:   #  Tex filename from which they'll propagate into the other filenames as well.
  302:   #
  303:   if (-e $texfile) {
  304:       if (($name_range ne '') && ($num_files > 1)) {
  305: 	  my $newtexfile = $texfile;
  306: 	  $newtexfile    =~ s/\.tex/$name_range\.tex/;
  307: 	  rename($texfile, $newtexfile);
  308: 	  $texfile       = $newtexfile;
  309:       }
  310:       $texfile =~ m/^(.*)\/([^\/]+)$/; 
  311:       my $name_file = $2;
  312:       my $path_file = $1.'/';
  313:       chdir $path_file;
  314:       my $dvi_file= $name_file; $dvi_file =~ s/\.tex$/\.dvi/;
  315:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  316: 			 "for $status_statement now LaTeXing file",
  317: 			 \%prog_state,$dvi_file, $busy_wait_timeout);
  318:       if ($tableofcontents eq 'yes') {
  319:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  320: 			 "for $status_statement First LaTeX of file for table of contents",
  321: 			 \%prog_state,$dvi_file, $busy_wait_timeout);
  322:       &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  323: 			 "for $status_statement Second LaTeX of file for table of contents",
  324: 			 \%prog_state,$dvi_file,$busy_wait_timeout);
  325:       } #to create table of contents
  326:       my $idxname=$name_file;
  327:       $idxname=~s/\.tex$/\.idx/;
  328:       if ($tableofindex eq 'yes') {
  329: 	  &busy_wait_command("makeindex $idxname",
  330: 			     "making index file",
  331: 			     \%prog_state,$idxname);
  332: 	  &busy_wait_command("latex $name_file 1>/dev/null 2>/dev/null",
  333: 			     "for $status_statement now LaTeXing file for index section",
  334: 			     \%prog_state,$dvi_file, $busy_wait_timeout);
  335:       } #to create index
  336:       #Do we have a latex error in the log file?
  337:       my $logfilename = $texfile; $logfilename =~ s/\.tex$/\.log/;
  338:       my $temporary_file=IO::File->new($logfilename) || die "Couldn't open log file $logfilename for reading: $!\n";
  339:       my @content_of_file = <$temporary_file>;
  340:       close $temporary_file; 
  341:       my $body_log_file = join(' ',@content_of_file);
  342:       $logfilename =~ s/\.log$/\.html/;
  343:       $temporary_file = IO::File->new('>'.$logfilename); 
  344:       print $temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_log_file.'</pre></body></html>'."\n";
  345:       if ($body_log_file=~m/!\s+Emergency stop/) {
  346: 	  my $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART';
  347: 	  my $whereitends = rindex $body_log_file,'STAMPOFPASSEDRESOURCEEND';
  348: 	  my $badresource;
  349: 	  my $badtext;
  350: 	  if ($whereitbegins!=-1 and $whereitends!=-1) {
  351: 	      $badtext = substr($body_log_file,$whereitbegins+26, $whereitends-$whereitbegins-26);
  352: 	      $whereitbegins  = rindex $badtext,'located in';
  353: 	      if ($whereitbegins != -1) {
  354: 		  
  355: 		  $badresource = substr($badtext, $whereitbegins+27, 
  356: 					length($badtext) - $whereitbegins - 48);
  357: 		  # print "<br />failing resourcename: $badresource<br />";
  358: 	      }
  359: 	  }
  360: 	  
  361:           if ($advanced_role) {  
  362: 	      #LaTeX failed to parse tex file 
  363: 	      print "<h2>LaTeX could not successfully parse your tex file.</h2>";
  364: 	      print "It probably has errors in it.<br />";
  365: 	      print "With very high probability this error occured in ".$badtext."<br /><br />";
  366: 	      print "Here are the error messages in the LaTeX log file<br /><pre>";
  367: 
  368: 	      my $sygnal = 0;
  369: 	      for (my $i=0;$i<=$#content_of_file;$i++) {
  370: 		  if ($content_of_file[$i]=~m/^Runaway argument?/ or $content_of_file[$i]=~m/^!/) {
  371: 		      $sygnal = 1;
  372: 		  } 
  373: 		  if ($content_of_file[$i]=~m/Here is how much of/) {
  374: 		      $sygnal = 0;
  375: 		  } 
  376: 		  if ($sygnal) {
  377: 		      print "$content_of_file[$i]";
  378: 		  }  
  379: 	      }
  380: 	      print "</pre>\n";
  381: 	      # print "<br /> Advanced role <br />";
  382:               print "<b><big>The link to ";
  383:               $logfilename=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool};
  384: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  385: 	      print "\n";
  386:               #link to original LaTeX file
  387: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
  388: 	      my @tex_content_of_file = <$tex_temporary_file>;
  389: 	      close $tex_temporary_file; 
  390: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  391: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  392: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  393: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  394: 	      print "<br /><br />";
  395: 	      print "<b><big>The link to ";
  396:               $texfile=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool};
  397: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  398: 	      print "\n";
  399: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
  400: 	      print ("$help_text");
  401: 
  402: 	  } else {		# Student role...
  403: 	      #  at this point:
  404: 	      #    $body_log_file - contains the log file.
  405:               #    $name_file     - is the name of the LaTeX file.
  406:               #    $identifier    - is the unique LaTeX identifier.l
  407: 
  408: 	      print "<br />There are errors in $badtext";
  409: 	      print "<br />These errors prevent this resource from printing correctly";
  410: 	      my $tex_handle = IO::File->new($name_file);
  411: 	      my @tex_contents = <$tex_handle>;
  412: 	      &send_error_mail($identifier, $badresource, $body_log_file, \@tex_contents);
  413: 	      print "<br />A message has been sent to the instructor describing this failure<br />";
  414: 	      my $help_text = &Apache::loncommon::help_open_topic("Print_Resource", "Help on printing");
  415: 	      print  ("$help_text");
  416: 
  417: 	  }
  418: 
  419:       } elsif ($body_log_file=~m/<inserted text>/) {
  420: 	  my $whereitbegins = index $body_log_file,'<inserted text>';
  421: 	  print "You are running LaTeX in <b>batch mode</b>.";
  422: 	  while ($whereitbegins != -1) {
  423: 	      my $tempobegin=$whereitbegins;
  424: 	      $whereitbegins = rindex $body_log_file,'STAMPOFPASSEDRESOURCESTART',$whereitbegins;
  425: 	      my $whereitends = index $body_log_file,'STAMPOFPASSEDRESOURCEEND',$whereitbegins;
  426: 	      print "<br />It has found an error in".substr($body_log_file,$whereitbegins+26,$whereitends-$whereitbegins-26)." <br /> and corrected it.\n";
  427: 	      print "Usually this correction is valid but you probably need to check the indicated resource one more time and implement neccessary corrections by yourself.\n";
  428: 	      $whereitbegins = index $body_log_file,'<inserted text>',$tempobegin+10;
  429: 	  }
  430: 	  $name_file =~ s/\.tex/\.dvi/;
  431: 	  my $new_name_file = $name_file;
  432: 	  $new_name_file =~ s/\.dvi/\.ps/;
  433: 	  my $papera=$paper;
  434: 	  if ($papera eq 'letter') {$papera='';}
  435: 	  if ($papera ne '') {$papera='-t'.$papera;}
  436: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
  437: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  438: 			     "for $status_statement now Converting to PS",
  439: 			     \%prog_state,$new_name_file);
  440: 	  if (-e $new_name_file) {
  441: 	      my $latex_file = $name_file;
  442: 	      $latex_file    =~ s/\.dvi/\.tex/;
  443: 	      &repaginate($new_name_file, $latex_file, $numberofcolumns);
  444: 	      #
  445: 	      #  Now have to re-latex, re dvips again to 
  446: 	      #  get the repaginated postscript.
  447: 	      #
  448: 	      &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
  449: 				 "for $status_statement first latex to repaginate",
  450: 				 \%prog_state, $name_file,$busy_wait_timeout);
  451: 	      if ($tableofcontents eq 'yes') {
  452: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
  453: 				     "for $status_statement second latex to repaginate",
  454: 				     \%prog_state, $name_file,$busy_wait_timeout);
  455: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
  456: 				     "for $status_statement third latex to repaginate",
  457: 				     \%prog_state, $name_file,$busy_wait_timeout);
  458: 	      }
  459: 	      if ($tableofindex eq 'yes') {
  460: 		  my $idxname = $latex_file;
  461: 		  $idxname =~ s/\.tex$/\.idx/;
  462: 		  &busy_wait_command("makeindex $idxname",
  463: 				     "Re-creating index file",
  464: 				     \%prog_state, $idxname);
  465: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
  466: 				     "for $status_statement now Recreting index (latex)",
  467: 				     \%prog_state, $dvi_file,$busy_wait_timeout);
  468: 
  469: 	      }
  470: 	      &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  471: 				 "for $status_statement dvips to repaginate",
  472: 				 \%prog_state, $new_name_file);
  473: 	      #
  474: 	      #  One last little hinky kinky thing.
  475: 	      #  It's just possible that some fonts could not be maded
  476: 	      #  at the resolution of the pdf print driver.
  477: 	      #  In that case a file called missfont.log will have been
  478: 	      #  created that will contain the commands that were attempted
  479: 	      # to create the missing fonts.  If we basically
  480: 	      # take all the 8000 strings in that file, and
  481: 	      # replace them with 600 (the ljfour resolution)
  482: 	      # run the commands in that file and redvips,
  483: 	      # we'll be able to print the missing glyphs at 600dpi.
  484: 	      #
  485: 	      # Supposedly it is possible to tune TeX/Metafont to do this
  486: 	      # right but I failed to get that to work when following the
  487: 	      # docs at the tug site, hence this rather kludgey fix.
  488: 	      #
  489: 	      #  We make the (I think) reasonable assumption that
  490: 	      #  missing glyphs won't change the pagination and I think
  491: 	      #  this is true because TeX/dvips will leave a space
  492: 	      #  instead of these glyphs based on the font metrics
  493: 	      #  (fancy way to say there will be a blank the size of the missing
  494: 	      #  glyphs).
  495: 	      #
  496: 	      my $print_directory = dirname($name_file);
  497: 	      my $missfonts_file  = $print_directory."/missfont.log";
  498: 	      #print("<br /> Missing fonts file is: $missfonts_file");
  499: 	      if (-e $missfonts_file) {
  500: 		  #print("<br />Missing fonts file exists\n");
  501: 		  &create_missing_fonts($missfonts_file,\%prog_state);
  502: 		  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  503: 				     "for $status_statement dvips generated missing fonts",
  504: 				     \%prog_state, $new_name_file);
  505: 	      }
  506: 
  507: 	      #
  508: 	      print "\n<h1>PDF output file (see link below)</h1>\n";
  509: 	      $new_name_file =~ m/^(.*)\./;
  510: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
  511: 	      my $pdf_file = $1.'.pdf';
  512: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  513: 		  my $papera=$paper;
  514:                   if ($papera eq 'letter') {$papera='';}
  515: 		  if ($papera ne '') {$papera='-p'.$papera;}
  516: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
  517: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
  518: 				     "for $status_statement now Modifying PS layout",
  519: 				     \%prog_state,$tempo_file); 
  520: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  521: 		  my $papera=$paper;
  522:                   if ($papera eq 'letter') {$papera='';}
  523: 		  if ($papera ne '') {$papera='-p'.$papera;}
  524: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
  525: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
  526: 				     "for $status_statement now Modifying PS layout",
  527: 				     \%prog_state,$tempo_file); 
  528: 
  529: 	      } else {
  530: 		  $ps_file=$new_name_file;
  531: 	      }	    
  532: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
  533: 				 "for $status_statement now Converting PS to PDF",
  534: 				 \%prog_state, $pdf_file);
  535: 
  536: 	      my $texlog = $texfile;
  537: 	      my $texaux = $texfile;
  538: 	      my $texdvi = $texfile;
  539: 	      my $texps = $texfile;
  540: 	      $texlog =~ s/\.tex/\.log/;
  541: 	      $texaux =~ s/\.tex/\.aux/;
  542: 	      $texdvi =~ s/\.tex/\.dvi/;
  543: 	      $texps =~ s/\.tex/\.ps/;
  544: 	      my @garb = ($texaux,$texdvi,$texps);
  545: #	  unlink @garb;
  546: 	      unlink($duefile);
  547: 	      print "<a href=\"/prtspool/$pdf_file\">Your PDF document</a>";
  548: 	      unlink($missfonts_file);
  549: 
  550: 	  }
  551: 	  if ($advanced_role) {  
  552: 	      print "<br /><br />";
  553: 	      print "<b><big>The link to ";
  554:               $logfilename=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool};
  555: 	      print "<a href=\"$logfilename\">Your log file </a></big></b>";
  556: 	      print "\n";
  557: 	      #link to original LaTeX file
  558: 	      my $tex_temporary_file=IO::File->new($texfile) || die "Couldn't open tex file $texfile for reading: $!\n";
  559: 	      my @tex_content_of_file = <$tex_temporary_file>;
  560: 	      close $tex_temporary_file; 
  561: 	      my $body_tex_file = join(' ',@tex_content_of_file);
  562: 	      $texfile =~ s/\.tex$/aaaaa\.html/;
  563: 	      $tex_temporary_file = IO::File->new('>'.$texfile); 
  564: 	      print $tex_temporary_file '<html><head><title>LOGFILE</title></head><body><pre>'.$body_tex_file.'</pre></body></html>'."\n";
  565: 	      print "<br /><br />";
  566: 	      print "<b><big>The link to ";
  567:               $texfile=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool};
  568: 	      print "<a href=\"$texfile\">Your original LaTeX file </a></big></b>";
  569: 	      print "\n";
  570: 	  }
  571: 
  572:       } else {
  573: 	  #LaTeX successfully parsed tex file 
  574: 	  $name_file =~ s/\.tex/\.dvi/;
  575: 	  my $new_name_file = $name_file;
  576: 	  $new_name_file =~ s/\.dvi/\.ps/;
  577: 	  my $papera=$paper;
  578: 	  if ($papera eq 'letter') {$papera='';}
  579: 	  if ($papera ne '') {$papera='-t'.$papera;}
  580: 	  my $comma = "dvips $papera -Ppdf -G0 -o $new_name_file";
  581: 	  &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  582: 			     "for $status_statement now Converting to PS",
  583: 			     \%prog_state,$new_name_file);
  584: 	  #
  585: 	  #  One last little hinky kinky thing.
  586: 	  #  It's just possible that some fonts could not be maded
  587: 	  #  at the resolution of the pdf print driver.
  588: 	  #  In that case a file called missfont.log will have been
  589: 	  #  created that will contain the commands that were attempted
  590: 	  # to create the missing fonts.  If we basically
  591: 	  # take all the 8000 strings in that file, and
  592: 	  # replace them with 600 (the ljfour resolution)
  593: 	  # run the commands in that file and redvips,
  594: 	  # we'll be able to print the missing glyphs at 600dpi.
  595: 	  #
  596: 	  # Supposedly it is possible to tune TeX/Metafont to do this
  597: 	  # right but I failed to get that to work when following the
  598: 	  # docs at the tug site, hence this rather kludgey fix.
  599: 	  #
  600: 	  #  We make the (I think) reasonable assumption that
  601: 	  #  missing glyphs won't change the pagination and I think
  602: 	  #  this is true because TeX/dvips will leave a space
  603: 	  #  instead of these glyphs based on the font metrics
  604: 	  #  (fancy way to say there will be a blank the size of the missing
  605: 	  #  glyphs).
  606: 	  #
  607: 	  my $print_directory = dirname($name_file);
  608: 	  my $missfonts_file  = $print_directory."/missfont.log";
  609: 	  #print("<br /> Missing fonts file is: $missfonts_file");
  610: 	  if (-e $missfonts_file) {
  611: 	      #print("<br />Missing fonts file exists\n");
  612: 	      &create_missing_fonts($missfonts_file,\%prog_state);
  613: 	      &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  614: 				 "for $status_statement dvips generated missing fonts",
  615: 				 \%prog_state, $new_name_file);
  616: 	  }
  617: 	  if (-e $new_name_file) {
  618: 	      my $latex_file = $name_file;
  619: 	      $latex_file =~ s/\.dvi/\.tex/;
  620: 	      &repaginate($new_name_file, $latex_file,  $numberofcolumns);
  621: 	      &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
  622: 				 "for $status_statement first latex to repaginate",
  623: 				 \%prog_state, $name_file, $busy_wait_timeout);
  624: 	      if ($tableofcontents eq 'yes') {
  625: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
  626: 				     "for $status_statement second latex to repaginate",
  627: 				     \%prog_state, $name_file,  $busy_wait_timeout);
  628: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
  629: 				     "for $status_statement third latex to repaginate",
  630: 				     \%prog_state, $name_file, $busy_wait_timeout);
  631: 	      }
  632: 	      if ($tableofindex eq 'yes') {
  633: 		  my $idxname = $latex_file;
  634: 		  $idxname    =~ s/\.tex$/\.idx/;
  635: 		  &busy_wait_command("makeindex $idxname",
  636: 				     "Re-creating index file",
  637: 				     \%prog_state, $idxname);
  638: 		  &busy_wait_command("latex $latex_file 1>/dev/null 2>/dev/null",
  639: 				     "for $status_statement now Recreting index (latex)",
  640: 				     \%prog_state, $dvi_file, $busy_wait_timeout);
  641: 	      }
  642: 	      &busy_wait_command("$comma $name_file 1>/dev/null 2>/dev/null",
  643: 				 "for $status_statement dvips to repaginate",
  644: 				 \%prog_state, $new_name_file);
  645: 
  646: 	      print "<br />";
  647: 	      $new_name_file =~ m/^(.*)\./;
  648: 	      my $ps_file = my $tempo_file = $1.'temporar.ps';
  649: 	      my $pdf_file = $1.'.pdf';
  650: 	      $papera=~s/t/p/;
  651: 	      if ($laystyle eq 'album' and $numberofcolumns eq '2') {
  652: 		  $comma = "psnup $papera -2 -s1.0 $new_name_file";
  653: 		  &busy_wait_command("$comma $tempo_file 1>/dev/null 2>/dev/null",
  654: 				     "for $status_statement now Modifying PS layout",
  655: 				     \%prog_state,$tempo_file);
  656: 	      } elsif ($laystyle eq 'book' and $numberofcolumns eq '2') {
  657: 		  $comma = 'pstops '.$papera.' "2:0+1(0.48w,0)"';
  658: 		  &busy_wait_command("$comma $new_name_file $tempo_file 1>/dev/null 2>/dev/null",
  659: 				     "for $status_statement now Modifying PS layout",
  660: 				     \%prog_state,$tempo_file); 
  661: 	      } else {
  662: 		  $ps_file=$new_name_file;
  663: 	      }
  664: 	      my $addtoPSfile={'legal'=>'<< /PageSize [612 1008] >> setpagedevice',
  665:                                'tabloid'=>'<< /PageSize [792 1224] >> setpagedevice',
  666:                                'executive'=>,'<< /PageSize [540 720] >> setpagedevice',
  667:                                'a2'=>'<< /PageSize [1195.02 1690.09] >> setpagedevice',
  668:                                'a3'=>'<< /PageSize [842 1195.02] >> setpagedevice',
  669:                                'a4'=>'<< /PageSize [595.2 842] >> setpagedevice',
  670:                                'a5'=>'<< /PageSize [421.1 595.2] >> setpagedevice',
  671:                                'a6'=>'<< /PageSize [298.75 421.1] >> setpagedevice',
  672: 			   };
  673: 	      if ($paper ne 'letter') {
  674: 		  open(FFH,'<',$ps_file) || die "Couldn't open ps file $ps_file for reading: $!\n";
  675: 		  my $new_ps_file='new'.$ps_file;
  676: 		  open(FFHS,'>',$new_ps_file) || die "Couldn't open new ps file $new_ps_file for reading: $!\n";
  677: 		  print FFHS $addtoPSfile->{$paper}."\n";
  678: 		  while (<FFH>) {
  679: 		      print FFHS $_;
  680: 		  }
  681: 		  close(FFH);
  682: 		  close(FFHS);
  683: 		  $ps_file=$new_ps_file;	  
  684: 	      }
  685: 	      &busy_wait_command("ps2pdf $ps_file $pdf_file 1>/dev/null 2>/dev/null",
  686: 				 "for $status_statement now Converting PS to PDF",
  687: 				 \%prog_state,$pdf_file);
  688: 	    
  689: 	      my $texlog = $texfile;
  690: 	      my $texaux = $texfile;
  691: 	      my $texdvi = $texfile;
  692: 	      my $texps = $texfile;
  693: 	      $texlog =~ s/\.tex/\.log/;
  694: 	      $texaux =~ s/\.tex/\.aux/;
  695: 	      $texdvi =~ s/\.tex/\.dvi/;
  696: 	      $texps =~ s/\.tex/\.ps/;
  697: 	      my @garb = ($texlog,$texaux,$texdvi,$texps);
  698: #	  unlink @garb;
  699: 	      unlink($duefile);
  700: 	      print "<a href=\"/prtspool/$pdf_file\">$link_text - click here to download pdf</a>";
  701: 	      print "\n";
  702: 	  }
  703: 	  unlink($missfonts_file);
  704: 
  705:       }  
  706:   } else {
  707:       print "LaTeX file $texfile was not created successfully";
  708:   }
  709: }
  710: print "<br />";
  711: if ($number_of_files>1) {
  712:     my $zipfile=$texfile[0];
  713:     $zipfile=~s/\.tex/\.zip/;
  714:     my $statement="zip $zipfile";
  715:     foreach my $file (@texfile) {
  716: 	$file=~s/\.tex/.\pdf/;
  717: 	$statement.=' '.$file; 
  718:     }
  719:     print("<pre>Zip Output:\n");
  720:     system($statement);
  721:     print("</pre>");
  722:     $zipfile=~s{^\Q$perlvar{'lonPrtDir'}\E}{/prtspool};
  723:     print "<br /> A <a href=\"$zipfile\">ZIP file</a> of all the PDFs.";
  724: }
  725: if ($advanced_role) { &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state); }
  726: print(&Apache::loncommon::end_page());
  727: my $done;
  728: 
  729: sub REAPER {
  730:     $done=1;
  731: }
  732: #
  733: #  Execute a command updating the status window as the command's
  734: #  output file builds up (at intervals of a second).
  735: #
  736: #   If the timeout argument defined, then if that many seconds
  737: #   elapses without an increase in the size of the output file,
  738: #   the command will be killed (this deals with the case when
  739: #   latex crawls into an infinite loop).
  740: #
  741: sub busy_wait_command {
  742:     my ($command,$message,$progress_win,$output_file, $timeout)=@_;
  743:     
  744:     $SIG{CHLD} = \&REAPER;
  745:     $done=0;
  746:     my $pid=open(CMD,"$command |");
  747:     if ($advanced_role) {
  748: 	&Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,$message);
  749:     }
  750:     my $last_size      = 0;
  751:     my $unchanged_time = 0;
  752:     while(!$done) {
  753: 	sleep 1;
  754: 	my $extra_msg;
  755: 	if ($output_file) {
  756: 	    my $size=(stat($output_file))[7];
  757: 	    $extra_msg=", $size bytes generated";
  758: 	    if ($size == $last_size) {
  759: 		$unchanged_time++;
  760: 		if ($timeout && ($unchanged_time > $timeout)) {
  761: 		    print "<h1>Operation timed out!</h1>\n";
  762: 		    print "<p>Executing $command, the output file $output_file did not grow\n";
  763: 		    print "after $timeout seconds.  This <em>may</em> indicate $command\n";
  764: 		    print "is in an infinite loop.\n";
  765: 		    print "See if printing fewer copies helps.  Please contact LON-CAPA\n";
  766: 		    print "support about this in any event.";
  767: 		    print "</p>";
  768: 		    kill(9, $pid); # Reaper will do the rest...I hope there's errors in the log.
  769: 		}
  770: 	    } else {
  771: 		$last_size      = $size;
  772: 		$unchanged_time = 0;
  773: 	    }
  774: 	}
  775: 	if ($advanced_role) {
  776: 	    &Apache::lonhtmlcommon::Update_PrgWin('',$progress_win,
  777: 						  $message.$extra_msg);
  778: 	}
  779:     }
  780:     $SIG{CHLD}='IGNORE';
  781:     close(CMD);
  782: }
  783: 
  784: #  Repagninate
  785: #  What we need to do:
  786: #   - Count the number of pages in each student.
  787: #   - Rewrite the latex file replacing the \specials that
  788: #     mark the end of student with an appropriate number of newlines.
  789: #   parameters:
  790: #     psfile     - Postscript filename
  791: #     latexfile  - LaTeX filename
  792: #     columns    - number of columns.
  793: sub repaginate {
  794: 
  795:     # We will try to do this in 2 passes through the postscript since
  796:     # the postscript is potentially large, to do 2 passes, the first pass
  797:     # must be able to calculate the total number of document pages so that
  798:     # at the beginning of the second pass we already know how to replace
  799:     #  %%Pages:
  800: 
  801:     #  Figure out
  802:     #    1. Number of pages in the document
  803:     #    2. Maximum number of pages in a student
  804:     #    3. Number of pages in each student.
  805: 
  806:     my ($postscript_filename, $latex_filename, $num_columns) = @_;
  807:     open(PSFILE, "<$postscript_filename");
  808:     my $line;
  809:     my $total_pages;		# Total pages in document.
  810:     my $seen_pages        = 0;	# There are several %%Pages only the first is useful
  811:     my @pages_in_student;	# For each student his/her initial page count.
  812:     my $max_pages = 0;		# Pages in 'longest' student.
  813:     my $page_number = 0;
  814:     &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
  815: 					  &mt("Counting pages for student: [_1]",1));
  816: 
  817:     while ($line = <PSFILE>) {
  818: 	
  819: 	# Check for total pages (%%Pages:)
  820: 
  821: 	if (($line =~ /^%%Pages:/) && (!$seen_pages)) {
  822: 	    my @pageinfo = split(/ /,$line);
  823: 	    $total_pages = $pageinfo[1];
  824: 	    $seen_pages  = 1;
  825: 	}
  826: 	#  Check for %%Page: n m  $page_number will be the
  827: 	#  biggest of these until we see an endofstudent.
  828: 	#  Note that minipages generate spurious %Page: 1 1's so
  829: 	#  we only are looking for the largest n (n is page number at the
  830: 	#  bottom of the page, m the page number within the document.
  831: 	#
  832: 
  833: 	if ($line =~ /^%%Page:\s+\d+\s+\d+/) {
  834: 	    my @pageinfo = split(/\s+/, $line);
  835: 	    if ($page_number < $pageinfo[1]) {
  836: 		$page_number = $pageinfo[1];
  837: 	    } elsif ($pageinfo[2] ne 1) {
  838: 		#  current page count reset, and it's not because of a 
  839: 		#    minipage 
  840: 		# - save the page_number, reset and, if necessary
  841: 		#    update max_pages.
  842: 		push(@pages_in_student, $page_number);
  843: 		&Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
  844: 						      &mt("Counting pages for student: [_1]", scalar(@pages_in_student)));
  845: 		if ($page_number > $max_pages) {
  846: 		    $max_pages = $page_number;
  847: 		}
  848: 		$page_number = $pageinfo[1];
  849: 	    }
  850: 	}
  851: 
  852: 	
  853:     }
  854:     # file ended so one more student
  855:     push(@pages_in_student, $page_number);
  856:     &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
  857: 					  &mt("Counting pages for student: [_1]",scalar(@pages_in_student)));
  858:     if ($page_number > $max_pages) {
  859: 	$max_pages = $page_number;
  860:     }
  861:     $page_number = 0;
  862:     
  863:     close(PSFILE);
  864: 
  865:     #  If 2 columns, max_pages must go to an even number of columns:
  866: 
  867:    
  868:     if ($num_columns == 2) {
  869: 	if ($max_pages % 2) {
  870: 	    $max_pages++;
  871: 	}
  872:     }
  873:     
  874:     #  Now rewrite the LaTex file, substituting our \special
  875:     #  with an appropriate number of \newpage directives.
  876: 
  877:     my $outfilename = $latex_filename."temp";
  878: 
  879:     open(LATEXIN, "<$latex_filename");
  880:     open(LATEXOUT, ">$outfilename");
  881: 
  882: 
  883:     my $student_number    = 0;	# Index of student we're working on.
  884:     &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
  885: 					  "Repaginating student ".$student_number+1);
  886: 
  887:     while (my $line = <LATEXIN>) {
  888: 	if ($line eq "\\special{ps:ENDOFSTUDENTSTAMP}\n") {
  889: 	    # only end of student stamp if next line is ENDOFSTUDENTSTAMP:
  890: 
  891: 
  892: 	    # End of student replace with 0 or more newpages.
  893: 	    
  894: 	    my $addlines = $max_pages - $pages_in_student[$student_number];
  895: 	    while($addlines)  {
  896: 		print LATEXOUT '\clearpage \strut \clearpage';
  897: 
  898: 		$addlines--;
  899: 	    }
  900: 	    
  901: 	    $student_number++;
  902: 	    &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state, 
  903: 						  "Repaginating student ".$student_number+1);
  904: 	    
  905: 	} else {
  906: 	    print LATEXOUT $line;
  907: 	}
  908:     }
  909: 
  910:     close(LATEXIN);
  911:     close(LATEXOUT);
  912:     rename($outfilename, $latex_filename);
  913: 
  914: }
  915: 
  916: #
  917: #   Create missing fonts given a latex missfonts.log file.
  918: #   This file will have lines like:
  919: #
  920: #   mktexpk --mfmode ljfour --bdpi 8000 --mag 1+0/8000 --dpi 8000 tcrm0500
  921: #
  922: #  We want to execute those lines with the 8000's changed to 600's
  923: #  in order to match the resolution of the ljfour printer.
  924: #  Of course if some wiseguy has changed the default printer from ljfour
  925: #  in the dvips's config.ps file that will break so we'll also
  926: #  ensure that --mfmode is ljfour.
  927: #
  928: sub create_missing_fonts {
  929:     my ($fontfile, $state) = @_;
  930: 
  931:     # Open and read in the font file..we'll read it into the array
  932:     #  font_commands.
  933:     #
  934:     open(my $font_handle, $fontfile);
  935:     my @font_commands = <$font_handle>;
  936: 
  937:     # make the list contain each command only once
  938:     my %uniq;
  939:     @font_commands = map { $uniq{$_}++ == 0 ? $_ : () } @font_commands;
  940: 
  941:     #  Now process each command replacing the appropriate 8000's with
  942:     #  600's ensuring that font names with 8000's in them are not corrupted.
  943:     #  and if the --mfmode is not ljfour we turn it into ljfour.
  944:     #   Then we execute the command.
  945:     #
  946:     
  947:     foreach my $command (@font_commands) {
  948: 	#print("<br />Raw command: $command");
  949: 	$command =~ s/ 8000/ 600/g;    # dpi directives.
  950: 	$command =~ s/\/8000/\/600/g;  # mag directives.
  951: 	#print("<br />After dpi replacements: $command");
  952: 
  953: 	my @cmdarray = split(/ /,$command);
  954: 	for (my $i =0; $i < scalar(@cmdarray); $i++) {
  955: 	    if ($cmdarray[$i] eq '--mfmode') {
  956: 		$cmdarray[$i+1] = "ljfour";
  957: 	    }
  958: 	}
  959: 	#print("<br /> before reassembly : (@cmdarray)");
  960: 	$command = join(" ", (@cmdarray));
  961: 
  962: 	#print("<br />Creating fonts via command: $command");
  963: 	&busy_wait_command("$command 1>/dev/null 2>/dev/null",
  964: 			   "Creating missing font",
  965: 			   $state);
  966: 			   
  967:     }
  968: 
  969: }
  970: #
  971: #  Convert a figure file to encapsulated postscript:
  972: #  At present, this is using a lot of file scoped globals to pass data around. 
  973: # Parameters:
  974: #    not_eps  - The name of the file to convert which, presumably, is not
  975: #               already an eps file.
  976: #
  977: sub convert_figure {
  978:     my ($not_eps) = @_;
  979: 
  980:     my $status_statement='EPS picture for '.$not_eps;
  981:     my $eps_f = $not_eps;
  982: 
  983:     if ($eps_f=~/\/home\/([^\/]+)\/public_html\//) {
  984: 	$eps_f=~s/\/home\/([^\/]+)\/public_html/$1/;
  985:     } elsif ($eps_f=~/$perlvar{'lonDocRoot'}\/res\//) {
  986: 	$eps_f=~ s/$perlvar{'lonDocRoot'}\/res\/(.+)/$1/;
  987:     } elsif ($eps_f=~/$perlvar{'lonUsersDir'}\//) {
  988: 	$eps_f=~ s/$perlvar{'lonUsersDir'}\/([^\/]+)\/\w\/\w\/\w\/(.+)/$1\/$2/;
  989:     }
  990: 
  991:     $eps_f = $perlvar{'lonPrtDir'}.'/'.$eps_f;
  992: 
  993:     # Spaces are problematic for system commands and LaTeX, replace with _
  994: 
  995:     $eps_f  =~ s/ /\_/g; 
  996: 
  997:     # 
  998:     # If the file is already an .eps or .ps file (eps_f still has the original
  999:     # file type),
 1000:     # We really just need to copy it from where it was to prtspool
 1001:     # but with the spaces substituted to _'s.
 1002:     #
 1003:     my ($nsname,$path, $sext) = &fileparse($eps_f, qr/\.(ps|eps)/i);
 1004:     if ($sext =~/ps$/i) {
 1005: 	&File::Path::mkpath($path,0,0777);
 1006: 	copy("$not_eps", "$eps_f"); 
 1007:     } else {
 1008: 	
 1009: 	$eps_f .= '.eps';	# Just append the eps ext.
 1010: 	my $path= &dirname($eps_f);
 1011: 	&File::Path::mkpath($path,0,0777);
 1012: 	$not_eps =~ s/^\s+//;
 1013: 	$not_eps =~ s/\s+$//;
 1014: 	$not_eps =~ s/ /\\ /g;
 1015: 	if ($advanced_role) {
 1016: 	    my $prettyname=$not_eps;
 1017: 	    $prettyname=~s|/home/([^/]+)/public_html|/priv/$1|;
 1018: 	    $prettyname=~s|$perlvar{'lonDocRoot'}/|/|;
 1019: 	    &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,
 1020: 						  'Converting to EPS '.$prettyname);
 1021: 	}
 1022: 	system("convert $not_eps $eps_f");
 1023: 
 1024: 	if (not -e $eps_f) {
 1025: 	    # converting an animated gif creates either:
 1026: 	    # anim.gif.eps.0
 1027: 	    # or
 1028: 	    # anim.gif-0.eps
 1029: 	    for (my $i=0;$i<10000;$i++) {
 1030: 		if (-e $eps_f.'.'.$i) {
 1031: 		    rename($eps_f.'.'.$i, $eps_f);
 1032: 		    last;
 1033: 		}
 1034: 		my $anim_eps = $eps_f;
 1035: 		$anim_eps =~ s/(\.[^.]*)\.eps$/$1-$i\.eps/i;
 1036: 		if (-e $anim_eps) {
 1037: 		    rename($anim_eps, $eps_f);
 1038: 		    last;
 1039: 		}
 1040: 	    }
 1041: 	}
 1042: 	
 1043: 	# imagemagick 6.2.0-6.2.7 fails to properly handle
 1044: 	# convert anim.gif anim.gif.eps
 1045: 	# it creates anim.eps instead. 
 1046: 	if (not -e $eps_f) {
 1047: 	    my $eps_f2 = $eps_f;
 1048: 	    $eps_f2 =~ s/\.[^.]*\.eps$/\.eps/i;
 1049: 	    if(-e $eps_f2) {
 1050: 		rename($eps_f2,$eps_f);
 1051: 	    }
 1052: 	}
 1053:     }
 1054:     
 1055: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>