home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / sbin / foomatic-printermap-to-gimp-print-xml < prev    next >
Encoding:
Text File  |  2007-03-07  |  5.0 KB  |  132 lines

  1. #!/usr/bin/perl
  2.  
  3. # This script updates the printer list of the driver XML file of
  4. # Gimp-Print (db/source/driver/gimp-print.xml in foomatic-db package)
  5. # according to the file src/foomatic/foomatic-printermap of the source
  6. # tarball of Gimp-Print 4.2.x or later.
  7. #
  8. # Remarks:
  9. #
  10. # - Manually added printer entries will not be removed. They drop out
  11. #   to the beginning of the printer list during the update. The same
  12. #   happens to printers which are removed from the new foomatic-printermap
  13. #
  14. # - All printers of foomatic-printermap are put to the end of the list,
  15. #   the order of foomatic-printermap is preserved.
  16. #
  17. # - The user will be informed by screen messages if the new
  18. #   foomatic-printermap contains new printers which were neither in the
  19. #   previous foomatic-printermap nor under the manually added printers.
  20. #
  21. # - A printer which was added manually before and is in foomatic-printermap
  22. #   now will be removed from the list of manually added printers and added
  23. #   to the list of printers from foomatic-printermap.
  24.  
  25. #use strict;
  26.  
  27. # Read out the program name with which we were called, but discard the path
  28. $0 =~ m!/([^/]+)\s*$!;
  29. my $progname = $1;
  30. my $debug = 0;
  31.  
  32. my ($opt_f, $opt_g, $opt_h);
  33. use Getopt::Std;
  34. getopts("f:g:h") or $opt_h = 1;
  35. if ($opt_h) {
  36.     print "
  37. foomatic-printermap-to-gimp-print-xml [ -f <foomatic-printermap> ] \
  38.                               [ -g <gimp-print.xml> ]
  39.  -f <foomatic-printermap>: File src/foomatic/foomatic-printermap of
  40.                     Gimp-Print source tarball (4.2.x or later). Default
  41.                     is foomatic-printermap in the current directory
  42.  
  43.  -g <gimp-print.xml>: File gimp-print.xml whose printer list should be
  44.                     updated. Default is the gimp-print.xml of the
  45.                     Foomatic database currently in use.
  46.  
  47. This program updates the printer list of the driver XML file of
  48. Gimp-Print (db/source/driver/gimp-print.xml in foomatic-db package)
  49. according to the file src/foomatic/foomatic-printermap of the source
  50. tarball of Gimp-Print 4.2.x or later.
  51.  
  52. ";
  53.     exit 0;
  54. }
  55.  
  56. # Determine gimp-print.xml file
  57. my $gimpprintxmlfile = $opt_g;
  58. if (!$gimpprintxmlfile) {
  59.     use Foomatic::Defaults;
  60.     $gimpprintxmlfile = "$libdir/db/source/driver/gimp-print.xml";
  61. }
  62.  
  63. # Determine foomatic-printermap file
  64. my $foomaticprintermapfile = $opt_f;
  65. if (!$foomaticprintermapfile) {
  66.    $foomaticprintermapfile = "./foomatic-printermap";
  67. }
  68.  
  69. # Read list of printer IDs from Gimp-Print's foomatic-printermap
  70. open PM, "< $foomaticprintermapfile" or
  71.     die "Cannot read $foomaticprintermapfile!\n";
  72. my @printermap = <PM>;
  73. close PM;
  74.  
  75. (s/^\s*\S+\s+\S+\s+(\S+)\s*$/$1/) foreach @printermap;
  76.  
  77. #print $#printermap;
  78.  
  79. # Read gimp-print.xml
  80. open GPX, "< $gimpprintxmlfile" or
  81.     die "Cannot read $gimpprintxmlfile!\n";
  82. my $gimpprintxml = join("", <GPX>);
  83. close GPX;
  84.  
  85. # Remove printer list
  86. #$gimpprintxml =~ s!(<\s*printers\s*>).*(\n\s*<\s*/\s*printers\s*>)!$1$2!s;
  87.  
  88. # Mark beginning of printer list from foomatic-printermap before deleting
  89. # these printer entries
  90. $gimpprintxml =~ s:\n  <!-- The following printers are listed in the current foomatic-printermap -->:XXXXXXXXXX:s;
  91.  
  92. # Remove only those printers which are in foomatic-printermap. We
  93. # re-add them in the next step, this way we have all printers of
  94. # foomatic-printermap together and in the order of
  95. # foomatic-printermap, and we have all manually added printers in the
  96. # beginning of the list, before the printers of foomatic-printermap
  97. # are listed.
  98. print STDERR "Removing old printer entries ";
  99. foreach my $printer (@printermap) {
  100.     print STDERR ".";
  101.     $gimpprintxml =~ s:\s*<\!\-\-[^<>]*?\-\->\s*<printer>\s*<id>\s*$printer\s*</id>.*?</printer>::s or
  102.     $gimpprintxml =~ s:\s*<printer>\s*<id>\s*$printer\s*</id>.*?</printer>::s or 
  103.     print STDERR "\n\nNew printer: $printer\n\n";
  104. }
  105. print STDERR "\n\n";
  106.  
  107. # Insert comment to mark the part of the list set up manually
  108. $gimpprintxml =~ s:(\n\s*<\s*printers\s*>)\n\s*<!--\s*Manually inserted printer entries\s*-->:$1:s;
  109. $gimpprintxml =~ s:(\n\s*<\s*printers\s*>):$1\n  <!-- Manually inserted printer entries -->:s;
  110.  
  111. # Insert comment to mark the part of the list generated from
  112. # foomatic-printermap
  113. $gimpprintxml =~ s:\n  <!-- The following printers were deleted in the current foomatic-printermap -->::s;
  114. $gimpprintxml =~ s:\n  <!-- The following printers are listed in the current foomatic-printermap -->::s;
  115. $gimpprintxml =~ s:XXXXXXXXXX:\n  <!-- The following printers were deleted in the current foomatic-printermap -->:s;
  116. $gimpprintxml =~ s:(\n\s*<\s*/\s*printers\s*>):\n  <!-- The following printers are listed in the current foomatic-printermap -->$1:s;
  117.  
  118. # Insert printers of foomatic-printermap to the end of the list
  119. print STDERR "Inserting printer entries of foomatic-printermap ";
  120. foreach my $printer (@printermap) {
  121.     print STDERR ".";
  122.     $gimpprintxml =~ s:(\n\s*<\s*/\s*printers\s*>):\n  <printer>\n   <id>$printer</id>\n  </printer>$1:s;
  123. }
  124. print STDERR "\n\n";
  125.  
  126. open GPX, "> $gimpprintxmlfile" or
  127.     die "Cannot write $gimpprintxmlfile!\n";
  128. print GPX $gimpprintxml;
  129. close GPX;
  130.  
  131. exit 0;
  132.