Annotation of loncom/interface/printout.pl, revision 1.128

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

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