#!/usr/bin/perl ########################################################################## #Program: mprename #Author: Daniel Eric Smith # #E-mail: desmith@jsu.edu # #Date: Jan 03, 2005 # #Version: 1.6 # ########################################################################## #The strict pragma forces varible declaration and some other things use strict; #The warnings pragma enables all warnings use warnings; #number of arguments passed to mprename #and then the number of elts in @switches or @mpfiles my $argnum=@ARGV; #assigns arguments to my array @args my @args=@ARGV; chomp(@args); #determines if a log file will be written (default: write log file) my $logbool=1; #determines if MetaPost will be run on each file (default: don't run) my $mpostbool=0; #array of switches passes to mprename my @switches; #array of MetaPost files passes to mprename my @mpfiles; #location of the . in the MetaPost file name my $wheredot; #holds the MetaPost file name without the .mp my $filenamesub; #holds the name of the log file my $logfilename; #time varible for log file my $tm; #holds MetaPost file lines while processing my $line; #name of the old postscript file my $oldfile; #name of the new postscript file my $newfile; #if varible needed to debug use this #my $debug #DEBUG line #print "DEBUG: The number of arguments passed equals ",scalar @ARGV,"\n"; #print "DEBUG: The value of argnum is $argnum\n"; #print "DEBUG: The arguments passed are @ARGV\n"; #DEBUG end #Checks if any arguments are passed if($argnum==0) { #If no arguments are passed ask what to do: print "Give me a MetaPost file(s) (and some switches) to process:\n\n"; &print_usage; print "Enter something:"; $line=; chomp $line; #If still no input exit program if($line eq "") { print "\nYou still have not given me something to do, so I quit!\n"; print "But please try again.\n\n"; exit; } #Converts the string $line into an array splitting at whitespaces @args = split /\s+/, $line; #DEBUG line #print "DEBUG: You typed: $line\nAnd I have args=(@args)\n"; #DEBUG end } #run through each element in the array foreach (@args) { if($_ eq "--help") { #Prints the usage of this command (a subroutine) &print_usage; exit; } elsif($_ =~ m/^-\w+/)#checks if a switch { #how many elements in array @switches $argnum=$#switches; #adds new element to @switches $switches[$argnum+1]=$_; #DEBUG lines #$debug=$argnum+1; #print "DEBUG: switches[$debug]=$_\n"; #DEBUG end } else #not a switch must be a MetaPost file { #how many elements in array @mpfiles $argnum=$#mpfiles; #adds new element to @mpfiles $mpfiles[$argnum+1]=$_; #DEBUG lines #$debug=$argnum+1; #print "DEBUG: mpfiles[$debug]=$_\n"; #DEBUG end } } #DEBUG lines #print "DEBUG: mpfiles=@mpfiles\n"; #print "DEBUG: scalar mpfiles=@mpfiles\n"; #DEBUG end #Check to see if any files are passed unless(@mpfiles) { print "You did not give me any files to process.\n"; print "Please try again.\n\n"; #Prints the usage of this command (a subroutine) &print_usage; exit; } ###### #ADD # ################################################### #Should check that the files passed exist # #if a file does not exist remove it from @mpfiles # #if none exists print error message and exit # #Put check here # ################################################### ###### #ADD # ############################################## #Should check that switches passed are valid # #print the invalid switch and continue # #Put check here # ############################################## foreach(@switches) { $logbool=0 if($_ eq "-nlog"); #turns off log file $mpostbool=1 if($_ eq "-mpost"); #run MetaPost } #process each MetaPost file #Note: uses default varible $_ foreach(@mpfiles) { #DEBUG line: what is the value of logbool #print "logbool=$logbool\n\n"; #DEBUG end #where is the last period in the file name $wheredot=rindex($_,"."); #grab everything before the last period $filenamesub=substr($_,0,$wheredot); #do this if -nlog is not passed if($logbool) { #the log file will be called "filename.mprlog" $logfilename=$filenamesub . ".mprlog"; print "\nThe log file name is $logfilename\n\n"; unless (open LOG, "> $logfilename") { die "Cannot create $logfilename: $!"; } print LOG "File Name:$_ \n"; print LOG "Arguments passed to mprename:\n"; print LOG "Switches:@switches \n"; print LOG "MetaPost Files:@mpfiles \n"; #use module Time use Time::localtime; #get current date/time $tm = localtime; #fancy print date/time printf(LOG "Run on Date: %04d-%02d-%02d\n\n",$tm->year+1900,($tm->mon)+1,$tm->mday); } #if -mpost switch passed run MetaPost if($mpostbool) { print "MetaPost output:\n"; if(system "mpost $_") { die "\nError:MetaPost unable compile $_\n\n"; } print "\n"; } #name the file handel for the file that is to be processed unless (open MPFILE, "$_") { #if the file cannot be found exit die "Cannot find $_: $!"; } #grabs each line from the MetaPost file and puts it into $line until EOF while (defined($line=)) { #removes newline character chomp($line); #Checks to see if the line begins with: #beginfig(#);(whitespace)%(whitespace)file(whitespace)=(whitespace)filename.extension #where filename.extension is what you want to rename the file, e.g. picture.eps if($line =~ m/^beginfig\((\d+)\);\s*%\s*file\s*=\s*((\w|\W)+.(\w|\W)+)$/) { #displays the line giving the new file name print "I see the line: $line\n"; #do this if -nlog is not passed #displays the line giving the new file name in the log file print LOG "I see the line: $line\n" if($logbool); #sets $oldfile to the old file name chomp($oldfile="$filenamesub.$1"); #sets $newfile to the new file name chomp($newfile=$2); #tries to rename the file if(rename $oldfile, $newfile) { #successful rename operation #tells you which file got renamed to what new name print "I renamed $oldfile to $newfile\n\n"; #do this if -nlog is not passed #tells you which file got renamed to what new name in the log file print LOG "I renamed $oldfile to $newfile\n\n" if($logbool); } else { #unsuccessful rename operation print "I could not renamed $oldfile to $newfile.\n"; print "Check to see if $oldfile exists.\n"; print "If not you may need to run MetaPost.\n\n"; #do this if -nlog is not passed if($logbool) { select LOG; print "I could not renamed $oldfile to $newfile.\n"; print "Check to see if $oldfile exists.\n"; print "If not you may need to run MetaPost.\n\n"; select STDOUT; } } } } } sub print_usage { my $lastslash=rindex($0,"/"); my $progname=substr($0,$lastslash+1); print "Usage: $progname [switches] FILE0.mp [FILE1.mp] [FILE2.mp] ...\n\n"; print "Switches:\n"; print "\t-nlog \tSupress writing the log file(s)\n"; print "\t-mpost \tRun MetaPost on the file(s)\n\n"; print "FILE(s):\n"; print "\tYou must supply at least one MetaPost file.\n"; print "\tEach file must be of the form filename.extension.\n"; print "\tYou may process as many file as you want.\n\n"; }