aboutsummaryrefslogtreecommitdiff
path: root/install-mib.pl
blob: 56461a86c28b61672eee9892a4e183ba01e9c336 (plain)
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
#! /usr/bin/env perl
#
use strict;
use File::Basename;
use File::Copy;
use autodie;

# install-mib FILE DIR

die "bad number of arguments" unless $#ARGV == 1;

my ($file, $destdir) = @ARGV;

my $mibname;

open(my $fd, '<', $file);
while (<$fd>) {
    s/^\s+//;
    if (/([\S]+)\s+DEFINITIONS\s+::=\s+BEGIN\s*$/) {
	$mibname = $1;
	last;
    }
}
close($fd);

die "no MIB definition in $file" unless defined $mibname;

mkdir($destdir) unless -e $destdir;

copy($file, $destdir);

my $index = "$destdir/.index";
my $tmpname = $index . $$;
open(my $ofd, '>', $tmpname);

my $modified;

$file = basename($file);

if (-e $index) {
    open($fd, '+<', $index);
    while (<$fd>) {
	if (defined($mibname) and /^${mibname}\s+/) {
	    chomp;
	    my @a = split /\s+/;
	    unless ($#a == 1 and $a[1] eq $file) {
		print $ofd "$mibname $file\n";
		$modified = 1;
	    }
	    $mibname = undef;
	} else {
	    print $ofd $_;
	}
    }
    close($fd);
}

if (defined($mibname)) {
    print $ofd "$mibname $file\n";
    $modified = 1;
}
close $ofd;

if ($modified) {
    unlink $index if -e $index;
    rename $tmpname, $index;
} else {
    unlink $tmpname;
}

Return to:

Send suggestions and report system problems to the System administrator.