summaryrefslogtreecommitdiffstats
path: root/recipes-kernel/linux/files/cfg/indexcfgs.pl
diff options
context:
space:
mode:
Diffstat (limited to 'recipes-kernel/linux/files/cfg/indexcfgs.pl')
-rwxr-xr-xrecipes-kernel/linux/files/cfg/indexcfgs.pl167
1 files changed, 167 insertions, 0 deletions
diff --git a/recipes-kernel/linux/files/cfg/indexcfgs.pl b/recipes-kernel/linux/files/cfg/indexcfgs.pl
new file mode 100755
index 0000000..96cd78a
--- /dev/null
+++ b/recipes-kernel/linux/files/cfg/indexcfgs.pl
@@ -0,0 +1,167 @@
1#!/usr/bin/perl -w
2#----------------------------------------------------------------------
3# indexcfgs.pl - utility to rename files in the current directory to an
4# indexed format so that a unique index number is
5# prepended to the current file name. The naming
6# template is:
7#
8# DDDDD-<name-or-short-description.cfg>
9#
10# Usage: create the *.cfg file you need, with any name and .cfg
11# extension. Run this script and it will automatically
12# rename your file to the indexed format above.
13#
14# Author: Daniel BORNAZ <daniel.bornaz@enea.com
15# Date: 2014/04/09
16# Version: 1.0
17#
18#----------------------------------------------------------------------
19
20use strict;
21use warnings;
22use Getopt::Long;
23
24my %args;
25my $counter_indexed=0;
26my $filetype="*.cfg";
27my $counter_queue=0;
28my $max_index=0;
29my $dir="./";
30my @files;
31my @queue;
32my $opt_strip;
33my $opt_help;
34
35
36#--------------------------------------------------------------------
37# strip the index from the file name
38#
39sub removeindex($){
40 my $tmpname=shift;
41 my $newname;
42
43 if($tmpname=~/^\d{5}\-.*cfg/ig){
44 $newname=substr($tmpname,6);
45 system("mv",$tmpname,$newname) == 0
46 or die "Cannot rename $tmpname to $newname: $?";
47 }else{
48 return 0;
49 }
50}
51
52#--------------------------------------------------------------------
53# get the indexed file name, return the index
54#
55sub decodeindex($){
56 my $tmpname=shift;
57
58 if($tmpname=~/^\d{5}\-.*cfg/ig){
59 return substr($tmpname,0,5);
60 }else{
61 return 0;
62 }
63}
64
65#--------------------------------------------------------------------
66# remove index from cfg files in current directory
67#
68sub strip_file_names {
69 print "Start removing index from $filetype files in $dir\n";
70
71 opendir(DIR,$dir) or die $!;
72
73 while( my $file=readdir(DIR)){
74 # retrieve cfg files (*.cfg)
75 if($file=~/cfg$/gi){
76 removeindex($file);
77 }
78 }
79
80 closedir(DIR);
81}
82
83#--------------------------------------------------------------------
84# index current directory cfg files
85#
86sub index_file_names {
87 print "Start indexing $filetype files in $dir\n";
88
89 opendir(DIR,$dir) or die $!;
90
91 while( my $file=readdir(DIR)){
92 # retrieve cfg files (*.cfg)
93 if($file=~/cfg$/gi){
94 @files=(@files,$file);
95 }
96 }
97
98 closedir(DIR);
99
100
101# separate indexed file names from the ones to be processed
102 foreach my $file (@files){
103 if($file=~/^\d{5}\-.*cfg/){
104 my $crt_index=0;
105
106 $crt_index=decodeindex($file);
107
108 if($crt_index > $max_index){
109 $max_index=$crt_index;
110 }
111
112 $counter_indexed++;
113 }else{
114 @queue=($file,@queue);
115 $counter_queue++;
116 }
117 }
118
119# set the next index number
120 $max_index++;
121
122# index the enqueued file names
123 foreach my $file (@queue){
124 my $newname;
125
126 $newname=sprintf("%05d-%s",$max_index++,$file);
127 system("mv",$file,$newname) == 0
128 or die "Cannot rename $file to $newname: $?";
129 }
130
131 printf("$counter_queue files indexed, ".
132 "$counter_indexed files already indexed. Done.\n");
133}
134
135#--------------------------------------------------------------------
136# display usage help
137#
138sub print_usage {
139 print "\n";
140 print " ./indexcfgs.pl [--strip]\n";
141 print " no params: Index the *.cfg file names in current dir\n";
142 print " to DDDDD-<original_file_name.cfg>\n";
143 print " --strip: Strips the index from the *.cfg file names ".
144 "in current dir\n";
145 print "\n";
146
147 exit 0;
148}
149
150
151#--- Program starts here --------------------------------------------
152GetOptions(\%args,
153 "strip" => \$opt_strip,
154 "help" => \$opt_help,
155);
156
157if(defined($opt_help)){
158 print_usage();
159}
160
161if(defined($opt_strip)){
162 strip_file_names();
163}else{
164 index_file_names();
165}
166
167