summaryrefslogtreecommitdiff
path: root/scripts/update-priority.pl
blob: e70688a1f9ad51203270b20fc04a9e8831d517e5 (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
70
71
72
73
74
#!/usr/bin/perl

# Author: Arthur Xiong
# Date: 04/15/2010
# Description: Script to automatically update/add priority tag with 
#              some value for each node.def according to the inputs 

# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2006-2010 Vyatta, Inc.
# All Rights Reserved.
#
# **** End License ****

use strict;
use warnings;

die "Usage: update-priority.pl <priority-file> <path-to-template>"
 unless $#ARGV == 1;

my $priority;
my $path;
my $comment;
my $priority_line;
my $node_def;
my $priority_file = $ARGV[0];
my $prefix  = $ARGV[1];

open my $pf, '<', $priority_file or die "$priority_file can't be opened";
while (<$pf>) {    
    chomp;
    next if /^#.*/;
    die "Syntax Error \"$_\"" unless /^(\d+)\s+(\S+)\s+(|#.*)$/;
    $priority = $1;
    $path = $2;
    $comment = $3;

    $priority_line = "";
    $priority_line = "priority: " . $priority . "\t" . $comment . "\n";
    print "priority_line: $priority_line";

    $node_def = "";
    $node_def = $prefix . "/" . $path . "/" . "node.def";
    print "node_def: ", $node_def, "\n";

    open my $nf, '<', $node_def  or die "$node_def can't be opened";
    open my $nfn, '>', "$node_def.new" or die "$node_def.new can't be opened";
    while (<$nf>) {
        print $nfn $_ if /^(tag|multi):/;
        print $nfn $priority_line;
        print $nfn $_ unless /^priority:\s(\d+)/ or /^(tag|multi):/;
        last if $. == 1;
    }
    while (<$nf>) {
        print $nfn $_ unless /^priority:\s(\d+)/;
    }
    close $nfn;
    close $nf;

    rename "$node_def.new", "$node_def"
     or die "$node_def.new can't be renamed to $node_def";

    print "Updated $node_def\n\n";
}
close $pf;