1#!/usr/bin/perl
2
3# This file, along with the GNUmakefile, works around a verification
4# error caused by a UFS bug (stripping a multi-link file breaks the link, and
5# sometimes causes the wrong file to be stripped/unstripped).  By using this
6# "strip" perl script, it not only causes the correct file to be stripped, but
7# also preserves the link.
8
9use strict;
10use File::Basename ();
11use File::stat ();
12use File::Temp ();
13use Getopt::Long ();
14use POSIX ();
15
16my %argtype = (
17    A => '',
18    R => '=s',
19    S => '',
20    X => '',
21    c => '',
22    d => '=s',
23    i => '',
24    n => '',
25    o => '=s',
26    r => '',
27    s => '=s',
28    u => '',
29    x => '',
30);
31my $cp = 'cp';
32my $MyName = File::Basename::basename($0);
33my $strip = '/usr/bin/strip';
34
35my %args;
36Getopt::Long::Configure(qw(no_permute bundling_override no_ignore_case));
37Getopt::Long::GetOptions(\%args, map {"$_$argtype{$_}"} keys(%argtype));
38
39shift(@ARGV) if $ARGV[0] eq '-';
40exec($strip) unless scalar(@ARGV) > 0;
41
42my $st;
43foreach my $f (@ARGV) {
44    if(defined($st = File::stat::stat($f)) && POSIX::S_ISREG($st->mode)
45      && $st->nlink > 1) {
46	my $tmp = File::Temp->new(TEMPLATE => 'stripXXXXXX', DIR => '/var/tmp');
47	die "$MyName: Can't create temp file\n" unless defined($tmp);
48	system($cp, $f, $tmp->filename) == 0 or exit($?);
49	system($strip,
50	  (map {$argtype{$_} eq '' ? "-$_" : ("-$_", $args{$_})} keys(%args)),
51	  $tmp->filename
52	) == 0 or exit($?);
53	system($cp, $tmp->filename, $f) == 0 or exit($?);
54    } else {
55	system($strip,
56	  (map {$argtype{$_} eq '' ? "-$_" : ("-$_", $args{$_})} keys(%args)),
57	  $f
58	) == 0 or exit($?);
59    }
60}
61