#!/usr/bin/env perl ######################################################################### # This script attempts to solve the "problem" that MetaPost has with # # the EPS numbering scheme. That is, this script renames the output # # EPS files to more "user-friendly" names. # ######################################################################### ################################################## # Author : Troy Henderson # # Created On : Tue Jun 29 14:27:08 CDT 2004 # # Last Modified By: Troy Henderson # # Last Modified On: Sun Feb 20 14:43:08 CDT 2005 # # Update Count : 2 # ################################################## use POSIX qw(ceil); use File::Temp; use File::Copy; use Cwd; ######################################## # Make sure input argument are correct # ######################################## $0 =~/.*[\/\\](.*)/;$scriptname = $1; if (@ARGV != 1 & @ARGV != 2) { &usage; } if (@ARGV == 1 ) { $MPfile=$ARGV[0]; } else { &usage; } $MPfile =~ /(.*)\.mp$/; $basename = $1; if ( $MPfile ne "$basename\.mp" ) { &usage; } ###################################### # Execute MetaPost on the input file # ###################################### print("=====================================================\n"); print("Running MetaPost on $MPfile\n"); print("=====================================================\n"); system("mpost $TEX $MPfile"); ##################################################### # Read in the LOG file and remove all \n characters # ##################################################### $LOGfile=$basename."\.log"; if (! -f "$LOGfile") { printf("$LOGfile does not exist\n"); exit; } open MYFILE, $LOGfile or die "$!"; $LOG = join "", ; $LOG =~ s/\n//g; close MYFILE; ################################################################################ # This single command magically gets MetaPost output EPS numbers from LOG file # ################################################################################ my %seen; @nums = grep !$seen{$_}++, sort {$a <=> $b} map { length $_ ? $_ : -1 } $LOG =~ /\($MPfile(.*)\)/ ? $1 =~ /\[(\d*)\]/g : (); ####################################################################### # Create new EPS filenames to replace original MetaPost naming scheme # ####################################################################### $digits=1; if ($nums[$#nums] > 9) { $digits=ceil(log($nums[$#nums]+1)/log(10)); } $digits = "_\%0"."$digits"; $digits = $digits . "d"; print("==============================\n"); print("Renaming MetaPost output files\n"); print("==============================\n"); $n=0; foreach $EPS (@nums) { if ($EPS == -1 ) { $oldEPS[$n] = $basename."\.ps"; $newBASENAME[$n] = $basename; } else { $oldEPS[$n] = $basename."\.$EPS"; $newBASENAME[$n] = $basename.sprintf($digits,$EPS); } print("$oldEPS[$n] -> $newBASENAME[$n]\.mps\n"); rename "$oldEPS[$n]","$newBASENAME[$n]\.mps"; $n++; } sub usage { print("Usage: $scriptname \n\n"); print("Note: MetaPost filename MUST have a \.mp extension\n"); exit; }