+ if (-e $outfile && ($script eq 'postrm' || $script eq 'prerm')
+ && !compat(5)) {
+ # Add fragments to top so they run in reverse order when removing.
+ complex_doit("echo \"# Automatically added by ".basename($0)."\"> $outfile.new");
+ complex_doit("sed \"$sed\" $infile >> $outfile.new");
+ complex_doit("echo '# End automatically added section' >> $outfile.new");
+ complex_doit("cat $outfile >> $outfile.new");
+ complex_doit("mv $outfile.new $outfile");
+ }
+ else {
+ complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
+ complex_doit("sed \"$sed\" $infile >> $outfile");
+ complex_doit("echo '# End automatically added section' >> $outfile");
+ }
+}
+
+# Removes a whole substvar line.
+sub delsubstvar {
+ my $package=shift;
+ my $substvar=shift;
+
+ my $ext=pkgext($package);
+ my $substvarfile="debian/${ext}substvars";
+
+ if (-e $substvarfile) {
+ complex_doit("grep -s -v '^${substvar}=' $substvarfile > $substvarfile.new || true");
+ doit("mv", "$substvarfile.new","$substvarfile");
+ }
+}
+
+# Adds a dependency on some package to the specified
+# substvar in a package's substvar's file.
+sub addsubstvar {
+ my $package=shift;
+ my $substvar=shift;
+ my $deppackage=shift;
+ my $verinfo=shift;
+ my $remove=shift;
+
+ my $ext=pkgext($package);
+ my $substvarfile="debian/${ext}substvars";
+ my $str=$deppackage;
+ $str.=" ($verinfo)" if defined $verinfo && length $verinfo;
+
+ # Figure out what the line will look like, based on what's there
+ # now, and what we're to add or remove.
+ my $line="";
+ if (-e $substvarfile) {
+ my %items;
+ open(SUBSTVARS_IN, "$substvarfile") || error "read $substvarfile: $!";
+ while (<SUBSTVARS_IN>) {
+ chomp;
+ if (/^\Q$substvar\E=(.*)/) {
+ %items = map { $_ => 1} split(", ", $1);
+
+ last;
+ }
+ }
+ close SUBSTVARS_IN;
+ if (! $remove) {
+ $items{$str}=1;
+ }
+ else {
+ delete $items{$str};
+ }
+ $line=join(", ", sort keys %items);
+ }
+ elsif (! $remove) {
+ $line=$str;
+ }
+
+ if (length $line) {
+ complex_doit("(grep -s -v ${substvar} $substvarfile; echo ".escape_shell("${substvar}=$line").") > $substvarfile.new");
+ doit("mv", "$substvarfile.new", $substvarfile);
+ }
+ else {
+ delsubstvar($package,$substvar);
+ }