#!/usr/local/bin/perl -w # #-------------------------------------------------------------------- # CERN, European Laboratory for Particle Physics # # ATLAS TDR Template # # Utility to generate fmbatch file to update and print an ATLAS TDR # FrameMaker book from a specified book template #-------------------------------------------------------------------- # # # Assumptions: The book file and any of the component files may not # be opened by FrameMaker during execution. # # Switches: -keepmyoverrides -keepmyvariables # Parameters: # # Created: 96/10/18, Mario Ruggier # Check and process command line options # $keepOverrides = "NO"; # Flag for keeping user format overrides $keepVariables = "NO"; # Flag for keeping user variable formats $commandLineOptions = ""; while ($ARGV[0] =~ /^-/) { $_ = shift; $commandLineOptions = $commandLineOptions . " " . $_; if (/^-keepmyoverrides$/) { $keepOverrides = "YES"; } elsif (/^-keepmyvariables$/) { $keepVariables = "YES"; } elsif (/^-h(elp)?$/) { &usage(); } else { print "Unrecognised switch: $_\n"; &usage(); } } # Check command line parameters # foreach $x (0,1) { $commandLineOptions = $commandLineOptions . " " . $ARGV[$x]; if (length($ARGV[$x]) == 0) { &usage(); } } if ( length($ARGV[2]) != 0) { print "Too many command line parameters!\n"; &usage(); } $commandLineOptions =~ s/^\s*//; # Global variables # $UD = $ARGV[0]; # Reference Template full path and book filename $RT = $ARGV[1]; # Update Document full path and book filename # Arrays for storing component files of the books %UDComps; # Keyed on components, for legacy book document @UDIndex; # Indexed on order of appearance, for legacy book document %RTComps; # Keyed on components, for reference book template $RTComps{"FrontMatter/FrontMatter.doc"}=1; $RTComps{"AutoFiles/TDR_TOC.doc"}=1; $RTComps{"AutoFiles/TDR_LOF.doc"}=1; $RTComps{"AutoFiles/TDR_LOT.doc"}=1; $RTComps{"Chapter/Chapter.doc"}=1; $RTComps{"Appendix/Appendix.doc"}=1; %cleanUp; # Array accumulating filenames for cleanup # Book path and filenames for reference template and legacy doc ($RTPath, $RTFilename) = &extractPathFile($RT); ($UDPath, $UDFilename) = &extractPathFile($UD); $FMBATCH = "/pttools/FrameMaker/Frame/bin/fmbatch"; $FMBFile = "$UDPath" . "/UpdateBatchFile.fmbatch"; $FMBtmp = "$UDPath" . "/fmbatchtmp.fmbatch"; $FMBOpts = ""; # To contain update options for fmbatch; # Request user confirmation before any file modifications # print "Working with: \n\n"; print " Switches: \n"; print " -keepmyoverrides $keepOverrides\n"; print " -keepmyvariables $keepVariables\n\n"; print " Parameters: \n"; print " Legacy document path $UDPath\n"; print " Legacy document book filename $UDFilename\n"; print " Reference template path $RTPath\n"; print " Reference template book filename $RTFilename\n\n"; if (! &ifYes("Please confirm [Y/N]: ")) { &usage(); } # Write an fmbatch file for MIF generation of book file # Append list of files for cleanup # Run fmbatch # open(O,"> $FMBtmp") || die "$!.\n"; $UDMIF=&writeSaveAsMIF(O,"$UD"); close(O); $cleanUp{"$FMBtmp"}=1; $cleanUp{"$UDMIF"}=1; print "Running fmbatch to get MIF of the book file... \n"; &sysCommand("$FMBATCH < $FMBtmp"); # Get file components of legacy book document # print "Parsing book MIF to extract component files... \n"; %UDComps = &getBookComponents($UDMIF); # From associative first also create an indexed array foreach $comp (keys(%UDComps)) { $UDIndex[$UDComps{$comp}]=$comp; } # Write an fmbatch file to import formats # Do not append list of FM backup files for cleanup # (keeps a backup of the origs, just in case) # print "\n"; print "Document to be updated ($UD) contains the component files:\n"; print "$UDPath/$UDFilename\n"; open(O,"> $FMBFile") || die "$!.\n"; require "ctime.pl"; print O "#" . "-" x 70 . "\n"; print O "#\n"; print O "# This fmbatch file has been automatically created:\n#\n"; print O "# By: $0\n"; print O "# On: " . &ctime(time) . "#\n"; print O "# With the following command line parameters: \n#\n"; print O "# $commandLineOptions\n#\n"; print O "# Please search for lines starting with \"####\" and edit as needed. \n#\n"; print O "#" . "-" x 70 . "\n"; print O "\n\n# Open all template files\n"; print O "#\n"; foreach $templateFile (keys(%RTComps)) { print O "Open $RTPath/$templateFile\n"; } print O "\n\n# Update all legacy component files\n"; print O "#\n"; $FMPrintString = ""; if ( $keepOverrides eq "YES" ) { $FMBOpts = $FMBOpts . "BO"; } if ( $keepVariables eq "YES" ) { $FMBOpts = $FMBOpts . "pflcrtxkm"; } for ($i=1; $i<@UDIndex; $i++) { local($f) = "$UDIndex[$i]"; print " " x (length($UDPath) + 1); print "$f"; local($legacyFile) = "$UDPath/$f"; local($formatsFile) = "$RTPath/$f"; if (! -e "$formatsFile") { $formatsFile = "$RTPath/Chapter/Chapter.doc"; print " " x (42 - length($f)); print "<<-- Chapter or Appendix ?"; } print "\n"; print O "echo Updating $legacyFile from $formatsFile\n"; print O "Open $legacyFile\n"; $FMPrintString = $FMPrintString . "Open $legacyFile\n"; if (-e "$legacyFile") { if ("$formatsFile" eq "$RTPath/Chapter/Chapter.doc") { print O "#\n#### Uncomment 1 of the 2 next lines and comment out echo ####\n"; print O "# ImportFormats $FMBOpts $legacyFile $formatsFile\n"; $formatsFile = "$RTPath/Appendix/Appendix.doc"; print O "# ImportFormats $FMBOpts $legacyFile $formatsFile\n"; print O "echo YOU FORGOT TO UNCOMMENT ImportFormats line for $legacyFile \n#\n"; } else { print O "ImportFormats $FMBOpts $legacyFile $formatsFile\n"; } print O "Save $legacyFile\n"; print O "Quit $legacyFile\n\n"; $FMPrintString = $FMPrintString . "Print $legacyFile $RTPath/Chapter/Chapter.doc\n"; $FMPrintString = $FMPrintString . "Quit $legacyFile\n"; } } # Also update the document &writeUpdateBook(O,"$UDPath/$UDFilename"); # Print the individual files print O "\n\n# Print the individual component files\n"; print O "#\n"; print O "echo Printing the component files individually\n"; print O "$FMPrintString"; print O "\n\n# Close all open template files\n"; print O "#\n"; foreach $templateFile (keys(%RTComps)) { print O "Quit $RTPath/$templateFile\n"; } close(O); print "\n"; print "Please open the fmbatch file ($FMBFile)\n"; print "with a text editor and uncomment the appropriate lines such that:\n"; print "\n"; print " All chapter files are updated from the Chapter Template File\n"; print " All appendix files are updated from the Appendix Template File \n"; print "\n"; print "Then execute the fmbatch file by typing: \n"; print "\n"; print " fmbatch $FMBFile\n"; print "\n"; # Cleanup # &cleanUp(""); ######################################################### sub usage { die <<"EndUsage"; USAGE: $0 [-keepmyoverrides] [-keepmyvariables] EXAMPLE: $0 ./TDR.bk ~/TDR/TDR.bk EndUsage } sub extractPathFile { local($full) = @_; local($path,$file); @full=split(/\//,"$full"); $file = $full[$#full]; if ($#full>0) { $path = join("/",@full[0..($#full-1)]); } else { $path = "."; } return ($path,$file); } sub ifYes { local($prompt) = @_; local($answer); print STDOUT $prompt; $answer = scalar(); $answer =~ m/^[y\n]/i; } sub writeSaveAsMIF { local($FH,$file) = @_; local($MIF)=$file; $MIF =~ s/\.book$/\.MIF/i; $MIF =~ s/\.bk$/\.MIF/i; $MIF =~ s/\.doc$/\.MIF/i; print $FH "Open $file\n"; print $FH "SaveAs m $file $MIF\n"; print $FH "Quit $file\n"; return $MIF; } sub cleanUp { # print "Cleaning up the files: \n"; foreach $file (keys(%cleanUp)) { if (-e "$file") { # print " $file\n"; unlink("$file"); if (-e "$file") { print "$0: Unable to remove $file\n"; } } } # print "Done cleaning up. \n"; } sub sysCommand { local($command) = @_; system("$command"); &exitStatus($?,"$command"); } sub exitStatus { local($status, $command) = @_; if ( $status != 0 ) { print "$0: The command:\n"; print " $command\n"; print " exits abnormally with status $status.\n"; print "$0: Quitting.\n"; &cleanUp(); exit $status; } } sub getBookComponents { local($MIF) = @_; local(*I); local($bookFlag,$i)=(0,0); local($F1,$F2,$component)=("",""); local(%bookByComp); open(I,"$MIF") || die "$!.\n"; line: while () { ($F1,$F2) = split(' ', $_, 9999); if ($. == 1) { if ($F1 eq "//g; $component =~ s/`//g; $component =~ s//\//g; $s = "'>", $component =~ s/$s//g; $s = "`", $component =~ s/$s//g; $i++; $bookByComp{$component} = $i; } } return %bookByComp; } sub writeUpdateBook { local($FH,$file) = @_; print $FH "\n# Update book\n"; print $FH "#\n"; print $FH "echo Updating the book $file\n"; print $FH "Open $file\n"; print $FH "Update $file\n"; print $FH "echo Print the book $file\n"; print $FH "Print $file\n"; print $FH "Save $file\n"; print $FH "Quit $file\n"; }