This file is indexed.

/usr/sbin/cups-genppdupdate is in printer-driver-gutenprint 5.2.10~pre2-0ubuntu2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
#! /usr/bin/perl -w
# $Id: cups-genppdupdate.in,v 1.60 2012/02/14 23:11:41 tillkamppeter Exp $
# Update CUPS PPDs for Gutenprint queues.
# Copyright (C) 2002-2003 Roger Leigh (rleigh@debian.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

use strict;
use Getopt::Long;
use Fcntl qw(:mode);
use FileHandle;
use IPC::Open2;

sub parse_options ();
sub get_driver_version ();
sub update_ppd ($); # Original PPD filename
sub get_ppd_fh ($$$$$); # Return contents of desired PPD
sub find_ppd ($$$$); # Gutenprint Filename, driver, language (e.g. en, sv),
		     # region (e.g. GB, DE)
sub get_ppd_data (*$$$$$); # Source PPD FH

our $opt_d; # Debug mode
our $opt_h; # Help
our $opt_n; # No action
our $opt_q; # Quiet mode
our $opt_s; # Source PPD location
our $opt_p; # New PPD location
our $opt_P; # PPD generator location
our $opt_v; # Verbose mode
our $opt_N; # Don't update PPD file options
our $opt_o; # Output directory
our $opt_r; # Gutenprint version
our $opt_i; # Interactive
our $opt_f; # Force upgrade
our $opt_l; # Language

my $debug = 0;
my $verbose = 0;   # Verbose output
my $interactive = 0;
my $quiet = 0;     # No output
my $no_action = 0; # Don't output files
my $reset_defaults = 0;		# Reset options to default settings
my $version = "5.2";
my $micro_version = "5.2.10-pre2";
my $use_static_ppd = "no";
my $file_version = '"5.2.10-pre2"$';

my $ppd_dir = "/etc/cups/ppd"; # Location of in-use CUPS PPDs
my $ppd_root_dir = "/usr/share/cups/model";
my $ppd_base_dir = "$ppd_root_dir/gutenprint/$version"; # Available PPDs
my $ppd_out_dir = "";		# By default output into source directory
my $gzext = ".gz";
my $updated_ppd_count = 0;
my $skipped_ppd_count = 0;
my $failed_ppd_count = 0;
my $exit_after_parse_args = 0;
my @languages = qw(Global C ca cs da de el en_GB es fi fr gl hu it ja nb nl pl pt ru sk sl sv tr uk vi zh_CN zh_TW);

my $serverdir = "/usr/lib/cups";
my $driver_bin = "$serverdir/driver/gutenprint.$version";
my $driver_version = "";
my $server_multicat = 0;
my $server_multicat_initialized = 0;

if (-x $driver_bin) {
    get_driver_version();
}

my @ppd_files; # A list of in-use Gutenprint PPD files

# Used to convert a language name to its two letter code
my %languagemappings = (
			"chinese"    => "cn",
			"danish"     => "da",
			"dutch"      => "nl",
			"english"    => "en",
			"finnish"    => "fi",
			"french"     => "fr",
			"german"     => "de",
			"greek"      => "el",
			"hungarian"  => "hu",
			"italian"    => "it",
			"japanese"   => "jp",
			"norwegian"  => "no",
			"polish"     => "pl",
			"portuguese" => "pt",
			"russian"    => "ru",
			"slovak"     => "sk",
			"spanish"    => "es",
			"swedish"    => "sv",
			"turkish"    => "tr"
);


# Check command-line options...

parse_options();


# Set a secure umask...

umask 0177;


# Find all in-use Gutenprint PPD files...
# For case-insensitive filesystems, use only one of .ppd and .PPD
# (bug 1929738).

if (@ARGV) {
    my $f;
    foreach $f (@ARGV) {
	if (-f $f and ($f =~ /\.ppd$/i or $f =~ /\//)) {
	    if (-f $f) {
		push @ppd_files, $f;
	    } else {
		print STDERR "Cannot find file $f\n";
	    }
	} elsif (-f "$ppd_dir/$f" or
		 -f "$ppd_dir/$f.ppd" or
		 -f "$ppd_dir/$f.PPD") {
	    if (-f "$ppd_dir/$f") {
		push @ppd_files, "$ppd_dir/$f";
	    } elsif (-f "$ppd_dir/$f.ppd") {
		push @ppd_files, "$ppd_dir/$f.ppd";
	    } elsif (-f "$ppd_dir/$f.PPD") {
		push @ppd_files, "$ppd_dir/$f.PPD";
	    }
	}  else {
	    print STDERR "Cannot find file $ppd_dir/$f, $ppd_dir/$f.ppd, or $ppd_dir/$f.PPD\n";
	}
    }
} else {
    my @ppdtmp = glob("$ppd_dir/*.{ppd,PPD}");
    my (%ppd_map);
    map { $ppd_map{$_} = 1 } @ppd_files;
    foreach my $f (@ppdtmp) {
	if ($f =~ /\.PPD$/) {
	    my ($g) = $f;
	    $g =~ s/\.PPD$/.ppd/;
	    if (! $ppd_map{$g}) {
		push @ppd_files, $f;
	    }
	} else {
	    push @ppd_files, $f;
	}
    }
}

# Update each of the Gutenprint PPDs, where possible...

foreach (@ppd_files) {
    my ($status) = update_ppd($_);
    last if ($status == -2);
    $failed_ppd_count++ if ($status == 0);
    $updated_ppd_count++ if ($status == 1);
    $skipped_ppd_count++ if ($status == -1);
}

if (!$quiet || $verbose) {
    if (!@ppd_files) {
	print STDOUT "No Gutenprint PPD files to update.\n";
    } elsif ($updated_ppd_count > 0) {
	my $plural = $updated_ppd_count == 1 ? "" : "s";
	print STDOUT "Updated $updated_ppd_count PPD file${plural}";
	if ($failed_ppd_count > 0) {
	    print STDOUT ", $failed_ppd_count failed";
	}
	if ($skipped_ppd_count > 0) {
	    print STDOUT ", $skipped_ppd_count skipped";
	}
	print STDOUT ".";
	if (!defined $opt_o || $opt_o ne "") {
	    print STDOUT "  Restart cupsd for the changes to take effect.";
	}
	print STDOUT "\n";
    } else {
	if ($failed_ppd_count > 0) {
	    print STDOUT "Failed to update any PPD files\n";
	} else {
	    print STDOUT "Did not update any PPD files\n";
	}
    }
}
exit ($failed_ppd_count > 0);

sub HELP_MESSAGE($;$$$) {
    my ($fh) = @_;
    print $fh "Usage: $0 [OPTION]... [PPD_FILE]...\n";
    print $fh "Update CUPS+Gutenprint PPD files.\n\n";
    print $fh "  -d flags    Enable debugging\n";
    print $fh "  -h          Display this help text\n";
    print $fh "  -n          No-action.  Don't overwrite any PPD files.\n";
    print $fh "  -q          Quiet mode.  No messages except errors.\n";
    print $fh "  -s ppd_dir  Use ppd_dir as the source PPD directory.\n";
    print $fh "  -p ppd_dir  Update PPD files in ppd_dir.\n";
    print $fh "  -P driver   Use the specified driver binary to generate PPD files.\n";
    print $fh "  -v          Verbose messages.\n";
    print $fh "  -N          Reset options to defaults.\n";
    print $fh "  -o out_dir  Output PPD files to out_dir.\n";
    print $fh "  -r version  Use PPD files for Gutenprint major.minor version.\n";
    print $fh "  -f          Ignore new PPD file safety checks.\n";
    print $fh "  -i          Prompt (interactively) for each PPD file.\n";
    print $fh "  -l language Language choice (Gutenprint 5.1 or below).\n";
    print $fh "              Choices: " . join(" ", @languages) . "\n";
    print $fh "              Or -loriginal to preserve original language\n";
    print $fh "                 with Gutenprint 5.2 or above\n";
    exit(0);
}

# Getopt::Std calls VERSION_MESSAGE followed by HELP_MESSAGE if --help
# is passed.  If --version is passed, it calls only VERSION_MESSAGE.
# So we have to make sure to exit, but we want to allow --help to
# print out the help message.
sub VERSION_MESSAGE($;$$$) {
    my ($fh) = @_;
    print "cups-genppdupdate from Gutenprint $micro_version\n";
    $exit_after_parse_args = 1;
}

sub help() {
    HELP_MESSAGE(\*STDOUT);
}
		  
sub check_multicat() {
}

sub get_driver_version() {
    open(DBIN, "$driver_bin org.gutenprint.extensions 2>/dev/null |") or return 0;
    my ($line);
    $server_multicat = 0;
    while ($line = <DBIN>) {
	if ($line =~ /^org.gutenprint.multicat$/) {
	    $server_multicat = 1;
	    last;
	}
    }
    close DBIN;
    $driver_version = `$driver_bin VERSION`;
    chomp $driver_version;
}

sub parse_options () {
    Getopt::Long::Configure("bundling");
    if (!GetOptions("d=i" => \$opt_d,
                    "h" => \$opt_h,
                    "n" => \$opt_n,
                    "q" => \$opt_q,
                    "s=s" => \$opt_s,
                    "p=s" => \$opt_p,
                    "P=s" => \$opt_P,
                    "v" => \$opt_v,
                    "N" => \$opt_N,
                    "o=s" => \$opt_o,
                    "r=s" => \$opt_r,
                    "f" => \$opt_f,
                    "i" => \$opt_i,
                    "l=s" => \$opt_l,
                    "version" => sub { VERSION_MESSAGE(\*STDOUT) },
                    "help" => sub { VERSION_MESSAGE(\*STDOUT); HELP_MESSAGE(\*STDOUT) })) {
	help();
    }
    if ($opt_n) {
	$no_action = 1;
    }
    if ($opt_d) {
	$debug = $opt_d;
    }
    if ($opt_v) {
	$verbose = 1;
	$quiet = 0;
    }
    if ($opt_q) {
	$verbose = 0;
	$quiet = 1;
    }
    if ($opt_N) {
	$reset_defaults = 1;
    }
    if ($opt_o) {
	if (-d $opt_o) {
	    $ppd_out_dir = "$opt_o";
	}
	else {
	    die "$opt_o: invalid directory: $!\n";
	}
    }
    if ($opt_r) {
	if ($version ne $opt_r) {
	    $version = $opt_r;
	    if ($opt_s) {
		if (-d $opt_s) {
		    $ppd_base_dir = "$opt_s";
		    $driver_bin = "";
		    $server_multicat = 0;
		    $use_static_ppd = "yes";
		} else {
		    die "$opt_s: invalid directory: $!\n";
		}
	    } else {
		$ppd_base_dir = "$ppd_root_dir/gutenprint/$version";
		$driver_bin = "$serverdir/driver/gutenprint.$version";
	    }
	    $driver_version = "";
	    # If user specifies version, we're not going to be able to check
	    # for an exact match.
	    $file_version = "\"$version";
	    if (-x $driver_bin) {
		get_driver_version();
		$use_static_ppd = "no";
		$file_version = "\"$driver_version\"\$";
	    } elsif (! -d $ppd_base_dir && ! -l $ppd_base_dir) {
		die "Gutenprint $version does not appear to be installed!\n";
	    }
	}
    }
    if ($opt_s) {
	if (-d $opt_s) {
	    $ppd_base_dir = "$opt_s";
	    $driver_bin = "";
	    $server_multicat = 0;
	    $driver_version = "";
	    $use_static_ppd = "yes";
	}
	else {
	    die "$opt_s: invalid directory: $!\n";
	}
    }
    if ($opt_p) {
	if (-d $opt_p) {
	    $ppd_dir = "$opt_p";
	}
	else {
	    die "$opt_p: invalid directory: $!\n";
	}
    }
    if ($opt_P) {
	if (-x $opt_P) {
	    $driver_bin = "$opt_P";
	    get_driver_version();
	    $use_static_ppd = "no";
	}
	else {
	    die "$opt_P: invalid executable: $!\n";
	}
    }
    if ($opt_h) {
	help();
    }
    if ($opt_l && lc $opt_l ne "original" && ! grep { $_ eq $opt_l } @languages) {
	print STDERR "Unknown language '$opt_l'\n";
	help();
    }
    if ($opt_i) {
	$interactive = 1;
    }
    if ($exit_after_parse_args) {
	exit(0);
    }
    if ($verbose && $driver_version ne "") {
	print STDOUT "Updating PPD files from Gutenprint $driver_version\n";
    }
}

sub get_ppd_fh($$$$$) {
    my ($ppd_source_filename, $filename, $driver, $locale, $region) = @_;

    my $source_data;
    my ($new_ppd_filename);

    if ($use_static_ppd eq "no" && $driver_version ne "") {
	my ($simplified);
	if ($filename =~ m,.*/([^/]*)(.sim)(.ppd)?(.gz)?$,) {
	    $simplified = "simple";
	} else {
	    $simplified = "expert";
	}
	my ($url);
	my (@url_list);
	if (((defined $opt_r && $opt_r < 5.2) ||
	     (defined $opt_l && $opt_l ne "")) &&
	    $locale ne "") {
	    if ($region) {
		push @url_list, "gutenprint.$version://$driver/$simplified/${locale}_${region}";
	    }
	    push @url_list, "gutenprint.$version://$driver/$simplified/${locale}";
	}
	push @url_list, "gutenprint.$version://$driver/$simplified";
	foreach $url (@url_list) {
	    $new_ppd_filename = $url;
	    if ($debug & 8) {
		print "Trying ", (! $server_multicat ? "$driver_bin cat " : ""), "$url for $driver, $simplified, $locale, $region\n";
	    }
	    if ($server_multicat) {
		if (! $server_multicat_initialized) {
		    my ($pid) = open2(*Reader, *Writer, "$driver_bin org.gutenprint.multicat");
		    $server_multicat_initialized = 1;
		}
		print Writer "$url\n";
		return ($new_ppd_filename, \*Reader);
	    }
	    if (open PPD, "$driver_bin cat $url |") {
		return ($new_ppd_filename, \*PPD);
	    }
	}
	# Otherwise fall through and try to find a static PPD
    }

    # Search for a PPD matching our criteria...

    $new_ppd_filename = find_ppd($filename, $driver, $locale, $region);
    if (!defined($new_ppd_filename)) {
        # There wasn't a valid source PPD file, so give up.
        print STDERR "$ppd_source_filename: no valid candidate for replacement.  Skipping\n";
        print STDERR "$ppd_source_filename: please upgrade this PPD manually\n";
	return ("", undef);
    }
    if ($debug & 1) {
	print "Candidate PPD: $new_ppd_filename\n";
    }

    my $suffix = "\\" . $gzext; # Add '\', so m// matches the '.'.
    if ($new_ppd_filename =~ m/.gz$/) { # Decompress input buffer
	open GZIN, "gunzip -c $new_ppd_filename |"
	    or die "$_: can't open for decompression: $!";
	return ($new_ppd_filename, \*GZIN);
    } else {
	open SOURCE, $new_ppd_filename
	    or die "$new_ppd_filename: can't open source file: $!";
	binmode SOURCE;
	return ($new_ppd_filename, \*SOURCE);
    }
}

# Update the named PPD file.
sub update_ppd ($) {
    my $ppd_source_filename = $_;
    my $ppd_dest_filename = $ppd_source_filename;
    if ($ppd_out_dir) {
	$ppd_dest_filename =~ s;(.*)/([^/]+);$2;;
	$ppd_dest_filename = "$ppd_out_dir/$ppd_dest_filename";
    }

    open ORIG, $_ or die "$_: can't open PPD file: $!";
    seek (ORIG, 0, 0) or die "can't seek to start of PPD file: $!";
    my @orig_metadata = stat(ORIG);
    if ($debug & 1) {
	print "Source Filename: $ppd_source_filename\n";
    }
    my ($filename) = "";
    my ($driver) = "";
    my ($gutenprintdriver) = "";
    my ($locale) = "";
    my ($lingo) = "";
    my ($region) = "";
    my ($valid) = 0;
    my ($orig_locale) = "";
    while (<ORIG>) {
	if (/\*StpLocale:/) {
	    ($locale) = m/^\*StpLocale:\s*\"(.*)\"$/;
	    $orig_locale = $locale;
	    $valid = 1;
	} elsif (/^\*LanguageVersion/) {
	    ($lingo) = m/^\*LanguageVersion:\s*(.*)$/;
	} elsif (/^\*StpDriverName:/ ) {
	    ($driver) = m/^\*StpDriverName:\s*\"(.*)\"$/;
	    $valid = 1;
	} elsif (/\*%End of / && $driver eq "") {
	    ($driver) = m/^\*%End of\s*(.*).ppd$/;
	} elsif (/^\*StpPPDLocation:/ ) {
	    ($filename) = m/^\*StpPPDLocation:\s*\"(.*)\"$/;
	    $valid = 1;
	} elsif (/^\*%Gutenprint Filename:/) {
	    $valid = 1;
	}
	if ($filename and $driver and $lingo and $locale) {
	    last;
	}
	if (! $valid && /^\*OpenUI/) {
	    last;
	}
    }
    if (! $valid) {
#	print STDERR "Skipping $ppd_source_filename: not a Gutenprint PPD file\n";
	return -1;
    }
    if (defined $opt_l && $opt_l ne "" && lc $opt_l ne "original") {
	$locale = $opt_l;
	$orig_locale = $locale;
    }
    if ($debug & 2) {
	print "Gutenprint Filename: $filename\n";
	if ($opt_l) {
	    print "Locale: $locale (from -l)\n";
	} else {
	    print "Locale: $locale\n";
	}
	print "Language: $lingo\n";
	print "Driver: $driver\n";
    }
    if ($locale) {
	# Split into the language and territory.
	($locale, $region) = split(/_/, $locale);
    } else {
	# Split into the language and territory.
	($locale, $region) = split(/_/, $lingo);
	# Convert language into language code.
	$locale = $languagemappings{"\L$lingo"};
	if (!defined($locale)) {
	    $locale = "C"; # Fallback if there isn't one.
	}
    }
    if (! defined($region)) {
	$region = "";
    }
    if ($debug & 2) {
	print "Base Locale: $locale\n";
	print "Region: $region\n";
    }

    # Read in the new PPD, decompressing it if needed...

    my ($new_ppd_filename, $source_fd) =
	get_ppd_fh($ppd_source_filename, $filename, $driver, $locale, $region);

    if (! defined $source_fd) {
	print "Unable to retrieve PPD file for $ppd_source_filename!\n";
	close ORIG;
	return 0;
    }

    my ($ndt, $nopt, $nres, $ndef, $source_data) = get_ppd_data($source_fd, 1, 1, 1, 1, 1);
    if (! defined $ndt) {
	print "Unable to retrieve PPD file for $ppd_source_filename!\n";
	close ORIG;
	return 0;
    }

    if ($interactive) {
	print "Update PPD $ppd_source_filename from $new_ppd_filename [nyq]? ";
	my $input = readline(*STDIN);
	if ($input =~ /^q/i) {
	    close $source_fd if !$server_multicat;
	    print "Skipping all...\n";
	    return -2;
	} elsif (! ($input =~ /^y/i)) {
	    close $source_fd if !$server_multicat;
	    print "Skipping...\n";
	    return -1;
	}
    }

    # Extract the default values from the original PPD...

    seek(ORIG, 0, 0);

    my ($odt, $oopt, $ores, $odef) = get_ppd_data(ORIG, 1, 0, 1, 1, 0);

    # Close original and temporary files...

    close ORIG;
    if (! $server_multicat && ! close $source_fd) {
	print "Unable to retrieve new PPD file: $!\n";
	return -1;
    }

    my %orig_default_types = %$odt;
    my %new_default_types = %$ndt;
    my %defaults = %$odef;
    my %new_defaults = %$ndef;
    my %options = %$nopt;
    my %resolution_map = %$nres;
    my %old_resolution_map = reverse %$ores;

    # Store previous language in the PPD file so that -l original works
    # correctly.

    if ($orig_locale ne "") {
	$source_data =~ s/(\*StpLocale:\s*\")(.*)(\")/$1$orig_locale$3/;
    }

    if ($debug & 4) {
	print "Options (Old->New Default Type):\n";
	foreach (sort keys %options) {
	    my ($old_type) = $orig_default_types{$_};
	    my ($new_type) = $new_default_types{$_};
	    if (! defined($old_type)) {
		$old_type = '(New)';
	    }
	    if ($old_type ne $new_type) {
		print "  $_ ($old_type -> $new_type) :  ";
	    } else {
		print "  $_ ($new_type) :  ";
	    }
	    my ($def) = $defaults{"Default$_"};
	    foreach my $opt (@{$options{$_}}) {
		if (defined $def && $def eq $opt) {
		    print "*";
		}
		print "$opt ";
	    }
	    print "\n";
	}
	if (keys %resolution_map) {
	    print "Resolution Map:\n";
	    foreach (sort keys %resolution_map) {
		print "   $_: $resolution_map{$_}\n";
	    }
	}
	if (keys %old_resolution_map) {
	    print "Old Resolution Map:\n";
	    foreach (sort keys %old_resolution_map) {
		print "   $_: $old_resolution_map{$_}\n";
	    }
	}
	print "Non-UI Defaults:\n";
	foreach (sort keys %defaults) {
	    my ($xkey) = $_;
	    $xkey =~ s/^Default//;
	    if (! defined ($options{$xkey})) {
		print "  $_: $defaults{$_}\n";
	    }
	}
	print "Default Types of dropped options:\n";
	foreach (sort keys %orig_default_types) {
	    if (! defined($options{$_})) {
		print "  $_: $orig_default_types{$_}\n";
	    }
	}
    }

    if ($no_action) {
	if (!$quiet || $verbose) {
	    if ($ppd_dest_filename eq $ppd_source_filename) {
		print STDOUT "Would update $ppd_source_filename using $new_ppd_filename\n";
	    } else {
		print STDOUT "Would update $ppd_source_filename to $ppd_dest_filename using $new_ppd_filename\n";
	    }
	}
	return 0;
    }

    if  (! $reset_defaults) {
	# Update source buffer with old defaults...

	# Loop through each default in turn.
default_loop:
	foreach my $default_option (sort keys %defaults) {
	    my $option;
	    my $default_option_value = $defaults{$default_option};
	    ($option = $default_option) =~ s/Default//; # Strip off `Default'
	    # Check method is valid
	    my $orig_method = $orig_default_types{$option};
	    my $new_method = $new_default_types{$option};
	    my $new_default = $new_defaults{$default_option};
	    if ((!defined($orig_method) || !defined($new_method)) ||
		$orig_method ne $new_method) {
		next;
	    }
	    if (defined($new_default) &&
		$default_option_value eq $new_default) {
		if ($verbose) {
		    print "$ppd_source_filename: Preserve *$default_option ($default_option_value)\n";
		}
		next;
	    }
	    if ($new_method eq "PickOne") {
		# Check the old setting is valid
		foreach my $opt (@{$options{$option}}) {
		    my $def_option = $default_option_value;
		    my $odef_option = $def_option;
		    if ($option eq "Resolution" &&
			defined $old_resolution_map{$def_option}) {
			if ($debug & 4) {
			    print "Intermapping old resolution $def_option to $old_resolution_map{$def_option}\n";
			}
			$def_option = $old_resolution_map{$def_option};
		    }
		    my @dopts = ($def_option);
		    if ($def_option ne $odef_option) {
			push @dopts, $odef_option;
		    }

		    foreach my $dopt (@dopts) {
			if (($dopt eq $opt) ||
			    ($option eq "Resolution" &&
			     (defined $resolution_map{$dopt}) &&
			     ($dopt = $resolution_map{$dopt}) eq $opt)) { # Valid option
			    # Set the option in the new PPD
			    $source_data =~ s/^\*($default_option).*/*$1:$dopt/m;
			    if ($verbose) {
				print "$ppd_source_filename: Set *$default_option to $dopt\n";
			    }
			    next default_loop;
			}
		    }
		}
		warn "Warning: $ppd_source_filename: Invalid option: *$default_option: $defaults{$default_option}.  Using default setting $new_defaults{$default_option}.\n";
		next;
	    }
	    warn "Warning: $ppd_source_filename: PPD OpenUI method $new_default_types{$default_option} not understood.\n";
	}
    }

    # Write new PPD...

    my $tmpnew = "${ppd_dest_filename}.new";
    if (! open NEWPPD, "> $tmpnew") {
	warn "Can't create $tmpnew: $!\n";
	return 0;
    }
    print NEWPPD $source_data;
    if (! close NEWPPD) {
	warn "Can't write to $tmpnew: $!\n";
	unlink $tmpnew;
	return 0;
    }

    if (! rename $tmpnew, $ppd_dest_filename) {
	warn "Can't rename $tmpnew to $ppd_dest_filename: $!\n";
	unlink $tmpnew;
	return 0;
    }
    chown($orig_metadata[4], $orig_metadata[5], $ppd_dest_filename);
    chmod(($orig_metadata[2] & 0777), $ppd_dest_filename);

    if (!$quiet || $verbose) {
	if ($ppd_dest_filename eq $ppd_source_filename) {
	    print STDOUT "Updated $ppd_source_filename using $new_ppd_filename\n";
	} else {
	    print STDOUT "Updated $ppd_source_filename to $ppd_dest_filename using $new_ppd_filename\n";
	}
    }
    return 1;
    # All done!
}

# Find a suitable source PPD file
sub find_ppd ($$$$) {
    my($gutenprintfilename, $drivername, $lang, $region) = @_;
    my $file; # filename to return
    my ($key) = '^\\*FileVersion:[ 	]*' . "$file_version";
    my ($lingo, $suffix, $base, $basedir);
    my ($current_best_file, $current_best_time);
    my ($stored_name, $stored_dir, $simplified);
    $stored_name = $gutenprintfilename;
    $stored_name =~ s,.*/([^/]+\.[0-9]+\.[0-9]+)(\.sim)?(\.ppd)?(\.gz)?$,$1,;
    if ($gutenprintfilename =~ m,.*/([^/]*)(\.sim)(\.ppd)?(\.gz)?$,) {
	$simplified = ".sim";
    } else {
	$simplified = "";
    }
    $stored_dir = $gutenprintfilename;
    $stored_dir =~ s,(.*)/([^/]*)$,$1,;

    $current_best_file = "";
    $current_best_time = 0;
    my (@basedirs);
    if ($opt_s) {
	@basedirs = ($opt_s);
    } else {
	@basedirs = ($ppd_base_dir, $stored_dir, $ppd_root_dir);
    }

    my (@lingos);
    if ($region ne "") {
	push @lingos, "${lang}_${region}/";
    }
    push @lingos, "$lang/";
    if ($lang ne "C") {
	push @lingos, "C/";
    }
    push @lingos, "en/", "";
    push @lingos, "Global/";
    my (@bases);
    push @bases, "stp-${drivername}.$version${simplified}";
    push @bases, "${drivername}.$version${simplified}";
    if ($stored_name ne "${drivername}.$version${simplified}" and
	$stored_name ne "stp-${drivername}.$version${simplified}") {
	push @bases, $stored_name;
    }
    push @bases, $drivername;

    # All possible candidates, in order of usefulness and gzippedness
    foreach $lingo (@lingos) {
	foreach $suffix (".ppd$gzext",
			 ".ppd") {
	    foreach $base (@bases) {
		foreach $basedir (@basedirs) {
                    if ($basedir eq "" || $base eq "") { next; }
		    my ($fn) = "$basedir/$lingo$base$suffix";
		    if ($debug & 8) {
                        print "Trying $fn for $gutenprintfilename, $lang, $region\n";
                    }
# Check that it is a regular file, owned by root.root, not writable
# by other, and is readable by root.  i.e. the file is secure.
		    my @sb = stat $fn or next;
		    if ($debug & 8) {
                        print "  Candidate $fn for $gutenprintfilename, $lang, $region\n";
                    }
		    if ($opt_f || (S_ISREG($sb[2]) && ($sb[4] == 0))) {
			# Check that the file is a valid Gutenprint PPD file
			# of the correct version.
			my $new_file_version;
			if ($fn =~ m/\.gz$/) {
			    $new_file_version = `gunzip -c $fn | grep '$key'`;
			} else {
			    $new_file_version = `cat $fn | grep '$key'`;
			}
			if ($new_file_version ne "") {
                            if ($debug & 8) {
			        print "   Format valid: time $sb[9] best $current_best_time prev $current_best_file cur $fn!\n";
			    }
			    if ($sb[9] > $current_best_time) {
				$current_best_time = $sb[9];
				$current_best_file = $fn;
		                if ($debug & 8) {
                                    print STDERR "***current_best_file is $fn\n";
                                }
			    }
			} elsif ($debug & 8) {
			    print "   Format invalid\n";
			}
		    }
		    else {
			$_ = $fn;
			if (! -d $fn && ! /\/$/) {
			    print STDERR "$fn: not a regular file, or insecure ownership and permissions.  Skipped\n";
			}
		    }
		}
	    }
	}
    }
    if ($current_best_file) {
        return $current_best_file;
    }
# Yikes!  Cannot find a valid PPD file!
    return undef;
}

# Return default type, options, resolutions, and default values.
# More efficient since it takes only one pass over the data.
sub get_ppd_data(*$$$$$) {
    my ($fh, $types, $opts, $resolutions, $defaults, $data) = @_;
    my (%options, %defaults, %resolution_map, %default_types);
    my ($fileversion_found) = 0;
    my $cur_opt = "";
    my (@optionlist);
    my ($source_data) = "";
    if ($reset_defaults) {
	$types = 0;
	$opts = 0;
	$resolutions = 0;
	$defaults = 0;
    }

    if ($resolutions || $types || $opts || $defaults || $data) {
	while (<$fh>) {
	    last if $_ eq "*%*%EOFEOF\n";
	    $fileversion_found = 1 if (/^\*FileVersion:/);
	    $source_data .= $_ if ($data);
	    chomp;
	    if (($types || $opts) && m/^\*OpenUI/) {
		my ($key, $value) = /^\*OpenUI\s\*([[:alnum:]]+).*:\s([[:alnum:]]+)/;
		if ($key && $value) {
		    $default_types{$key}=$value;
		    $cur_opt = $key;
		}
	    } elsif ($opts && m/^\*CloseUI/) {
		if ($cur_opt ne "") {
		    $options{$cur_opt} = [ @optionlist ];
		    $cur_opt = "";
		}
		@optionlist = ();
	    } elsif ($opts && m/^\*$cur_opt/) {
		my ($value) = /^\*$cur_opt\s*([[:alnum:]]+)[\/:]/;
		if (defined $value && $value) {
		    push @optionlist, $value;
		}
	    } elsif ($resolutions && m/^\*StpResolutionMap:/) {
		my ($junk, $new, $old) = split;
		$resolution_map{$old} = $new;
	    } elsif ($defaults && m/^\*Default/) {
		my($key, $value) = /^\*([[:alnum:]]+):\s*([[:alnum:]]+)/;
		if ($key && $value) {
		    $defaults{$key}=$value;
		}
	    }
	}
    }
    return (undef, undef, undef, undef, undef) if (! $fileversion_found);
    return (\%default_types, \%options, \%resolution_map, \%defaults, $source_data);
}