<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -w

use strict;

sub Usage() {
  print "Usage:\n  attribute_rm.pl attribute_name files...\n";
  exit(1);
}

my $backup = 0;

sub run($$) {
    my $attr = shift;
    my $file = shift;

    lstat($file);
    if (! -f _) {
	print("$file: not a regular file\n");
	return -1;
    }
    if (!open(FH, "$file")) {
	print("$file: cannot open file\n");
	return -1;
    }

    my @line = &lt;FH&gt;;
    close(FH);
    if (@line == 0) {
	print("$file: empty file\n");
	return -1;
    } elsif (@line &lt; 3) {
	print("$file: to small file to contain an attribute\n");
	return -1;
    } elsif ($line[0] !~ m/^v \d{8} \d/) {
	print("$file: file does not begin with a version line\n");
	return -1;
    }

    my @new = ();
    for (my $ix = 2; $ix &lt; @line; $ix++) {
	if ($line[$ix] =~ m/^$attr=/) {
	    if ($line[$ix-1] !~ m/^T /) {
		print("$file: missing T line before attribute line\n");
		return -1;
	    }
	    @new = @line[0 .. $ix-2];
	    push @new, @line[$ix+1 .. @line-1];
	    last;
	}
    }
    if (@new == 0) {
	print("$file: skip\n");
	return 0;
    }

    if ($backup) {
	return 0 unless open(FH, "&gt;$file.new");
	print FH @new;
	close(FH);
	`mv $file $file.old`;
	`mv $file.new $file`;
    } else {
	return 0 unless open(FH, "&gt;$file");
	print FH @new;
	close(FH);
    }
}

sub main() {
    if (@ARGV &lt; 1) {
	Usage();
    }
    my $attr = shift @ARGV;

    for my $file (@ARGV) {
	run($attr, $file);
    }
}

main();
</pre></body></html>