Drop grml-postfix
[grml-scripts.git] / usr_sbin / alignmargins
1 #! /usr/bin/perl
2 # Filename:      alignmargins
3 # Purpose:       adjust the margins and the position of the printed contents on the paper
4 # Authors:       (C) 2001 by Till Kamppeter
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       Free software under the terms of the GNU General Public License (GPL)
7 ################################################################################
8 # Downloaded from http://www.linuxprinting.org/download/printing/alignmargins
9
10 $0 =~ m!^(.*)/[^/]+$!;
11 my $programpath = $1;
12 my $printcommand = '/usr/bin/lpr -P ';
13 my $egrep = '/bin/egrep';
14 my $cat = '/bin/cat';
15 my $cut = '/usr/bin/cut';
16 my $head = '/usr/bin/head';
17 my $tail = '/usr/bin/tail';
18 my $wc = '/usr/bin/wc';
19 my $adjustmentpagename = 'align.ps';
20 my $adjustmentpagepath = ($programpath ? "${programpath}:" : "") . '.:~:/usr/share/grml-scripts:/usr/share/alignmargins:/usr/local/share/alignmargins:/usr/share:/usr/local/share:/usr/share/printer-testpages:/usr/local/share/printer-testpages:/usr/share/ghostscript/*/lib:/usr/local/share/ghostscript/*/lib';
21 my $ppddir = '/etc/cups/ppd';
22 my $printerconffile = '/etc/cups/printers.conf';
23
24 # Find "ælign.ps"
25
26 my $adjustmentpage;
27 for $path (split(":", $adjustmentpagepath)) {
28     if (-r "$path/$adjustmentpagename") {
29         $adjustmentpage = "$path/$adjustmentpagename";
30         last;
31     }
32 }
33
34 # Are we running as root?
35
36 if (!(-w $printerconffile)) {die "\"alignmargins\" must be run logged in as \"root\"!";}
37
38 # Check whether there are local printer queues 
39
40 open NUMBEROFQUEUES, "$cat $printerconffile | $egrep '<Printer|<DefaultPrinter' | $wc -l |" or die "Cannot read local printer configuration!";
41  
42 my $nqueues = <NUMBEROFQUEUES>;
43 close NUMBEROFQUEUES;
44
45 # Ask the user which printer he wants to align
46
47 print "\n";
48 print "CUPS printer margin and offset alignment\n";
49 print "----------------------------------------\n";
50 print "\n";
51 print "(C) 2001 by Till Kamppeter\n";
52 print "Free software under the terms of the GNU General Public License (GPL)\n";
53 my $queue = "";
54
55 do {
56   print "\n";
57   print "With this program you can adjust the margins and the position of the\n";
58   print "printed contents on the paper. This way you get well-centered printouts and\n";
59   print "you can make use of the whole imageable area of your printer, The driver\n";
60   print "settings are overridden when this adjustment is used.\n";
61   print "\n";
62   print "This is especially important when your printer is used with a driver for\n";
63   print "another printer to which yours is compatible (for example many laser\n";
64   print "printers are compatible to the HP LaserJet printers). Your printer prints\n";
65   print "with this driver, but the non-printable margins are usually different or the\n";
66   print "contents is even not centered. With this program you can fix these problems\n";
67   print "\n";
68   print "The program can only be applied to local printer queues. The following\n";
69   print "queues are available:\n";
70   print "\n";
71
72   system "$cat $printerconffile | $egrep '<Printer|<DefaultPrinter' | $cut -d ' ' -f 2 | $cut -d '>' -f 1 | $cat -n";
73
74   print "\n";
75   print "Please enter the number of the desired printer and make sure that it is\n";
76   print "connected to your computer and turned on.\n";
77   print "\n";
78
79   print "Number: ";
80   my $input = <STDIN>;
81
82   if ( $input =~ m/^\s*(\d+)\D*/ ) {
83     my $number = $1;
84     if (($number > 0) && ($number <= $nqueues)) {
85       open QUEUE, "$cat $printerconffile | $egrep '<Printer|<DefaultPrinter' | $cut -d ' ' -f 2 | $cut -d '>' -f 1| $head -$number | $tail -1 |";
86       if (!$?) {
87         $queue = <QUEUE>; 
88         close QUEUE;
89       }
90     }
91   } else {
92     print "\nWrong input, try again!\n";
93   }
94 } until ($queue ne "");
95
96 chomp $queue;
97
98 print "\n";
99 print "Printing margin/offset adjustment page ...\n";
100 print "\n";
101
102 # The "%!" which is needed in a file to be recognized as a PostScript file
103 # is missing in the adjustment page, therefore it is preceeded to the file
104 # here.
105 if (system "(echo %!; $cat $adjustmentpage) | $printcommand$queue") {
106 die "Could not print the adjustment page.";
107 }
108
109 print "Please read the instructions on the margin adjustment page and determine the\n";
110 print "six numbers mentioned there. If you measure in cm and not in inches, devide\n";
111 print "the measured quantities by 2.54 before you insert them into the equations\n";
112 print "shown on the page. You do not need to create any file with PostScript\n";
113 print "commands, this program will insert your settings into your printer's\n";
114 print "configuration.\n";
115 print "\n";
116 print "If the adjustment page did not come out of your printer, this method cannot\n";
117 print "be applied, press Ctrl + C to stop this program. This can especially happen\n";
118 print "with very old PostScript printers.\n";
119 print "\n";
120 print "Note also that this adjustment does not necessarily work with every driver.\n";
121 print "The concept is taken from GhostScript and the implementation of this program\n";
122 print "is not much tested yet.\n";
123 print "\n";
124
125 print "Please enter your results now:\n";
126 print "\n";
127
128 my $ml = 9999999.;
129 my $mb = 9999999.;
130 my $mr = 9999999.;
131 my $mt = 9999999.;
132 my $x = 9999999.;
133 my $y = 9999999.;
134
135 do {
136   print "ml: ";
137   my $input = <STDIN>;
138
139   if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
140     my $number = $1;
141     if (($number >= -100000) && ($number <= 100000)) {
142       $ml = $number * 1.;
143     }
144   } else {
145     print "Wrong input, try again!\n";
146   }
147 } until ($ml != 9999999.);
148
149 do {
150   print "mb: ";
151   my $input = <STDIN>;
152
153   if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
154     my $number = $1;
155     if (($number >= -100000) && ($number <= 100000)) {
156       $mb = $number * 1.;
157     }
158   } else {
159     print "Wrong input, try again!\n";
160   }
161 } until ($mb != 9999999.);
162
163 do {
164   print "mr: ";
165   my $input = <STDIN>;
166
167   if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
168     my $number = $1;
169     if (($number >= -100000) && ($number <= 100000)) {
170       $mr = $number * 1.;
171     }
172   } else {
173     print "Wrong input, try again!\n";
174   }
175 } until ($mr != 9999999.);
176
177 do {
178   print "mt: ";
179   my $input = <STDIN>;
180
181   if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
182     my $number = $1;
183     if (($number >= -100000) && ($number <= 100000)) {
184       $mt = $number * 1.;
185     }
186   } else {
187     print "Wrong input, try again!\n";
188   }
189 } until ($mt != 9999999.);
190
191 do {
192   print "x: ";
193   my $input = <STDIN>;
194
195   if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
196     my $number = $1;
197     if (($number >= -100000) && ($number <= 100000)) {
198       $x = $number * 1.;
199     }
200   } else {
201     print "Wrong input, try again!\n";
202   }
203 } until ($x != 9999999.);
204
205 do {
206   print "y: ";
207   my $input = <STDIN>;
208
209   if ( $input =~ m/^\s*([+-]?[\d\.]+)\D*/ ) {
210     my $number = $1;
211     if (($number >= -100000) && ($number <= 100000)) {
212       $y = $number * 1.;
213     }
214   } else {
215     print "Wrong input, try again!\n";
216   }
217 } until ($y != 9999999.);
218
219 my $ppdfilename = "$ppddir/$queue.ppd";
220 print "\n";
221 print "Saving your settings in $ppdfilename ...\n";
222 print "\n";
223
224 my @marginsoption = (
225   "*OpenUI *Margins/Page Margins/Offsets: PickOne\n",
226   "*DefaultMargins: Custom\n",
227   "*Margins Default/Driver Default: \"\"\n",
228   "*Margins Custom/Custom (set with 'alignmargins'): \"<</.HWMargins[$ml $mb $mr $mt] /Margins[$x $y]>>setpagedevice\"\n",
229   "*CloseUI: *Margins\n"
230 );
231
232 # Read PPD file of the chosen printer
233  
234 if (!(-f $ppdfilename)) {die "No PPD file $ppdfilename!"};
235 open PPDFILE, "$ppdfilename" or die "Can't open $ppdfilename!";
236 my @ppdfile = <PPDFILE>;
237 close PPDFILE;
238
239 # Remove an old margin adjustment option
240
241 ($_ =~ m!^\s*\*OpenUI\s*\*Margins/.*:! and $_="") foreach @ppdfile;
242 ($_ =~ m!^\s*\*DefaultMargins:! and $_="") foreach @ppdfile;
243 ($_ =~ m!^\s*\*Margins\s*.*/.*:! and $_="") foreach @ppdfile;
244 ($_ =~ m!^\s*\*CloseUI:\s*\*Margins! and $_="") foreach @ppdfile;
245
246 # Insert the new margin adjustment option
247
248 splice(@ppdfile,-1,0,@marginsoption);
249
250 # Write back PPD file
251
252 open PPDFILE, ">$ppdfilename" or die "Can't open $ppdfilename";
253 print PPDFILE @ppdfile;
254 close PPDFILE;
255
256 # Re-initialize CUPS (must be done after a "manual" change on the PPD file)
257
258 system("killall -HUP cupsd");
259
260 print "\n";
261 print "Done.\n";
262 print "\n";
263 print "Now your printer \"$queue\" will use the new margin and offset settings by\n";
264 print "default. You can turn them off by switching the option \"Page Margins/Offsets\"\n";
265 print "to \"Driver Default\" in kprinter, GTKlp, or XPP.\n";
266 print "\n";
267 print "On the command line (\"lpr\", \"lp\", \"lpoptions\", ...) use the option\n";
268 print "\"-o Margins=Default\" to turn off and \"-o Margins=Custom\" to turn on your\n";
269 print "settings.\n";
270 print "\n";
271
272 exit 0;
273
274 ## END OF FILE #################################################################