slitaz-dev-tools view baba-scripts/renup.pl @ rev 232
bootloader: fix multi initrd case
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Wed Jul 30 12:24:08 2014 +0200 (2014-07-30) |
parents | |
children |
line source
1 #! /usr/bin/perl -w
2 chomp (my $scriptname = `basename "$0"`);
3 chomp (my $workdir = `pwd`);
4 my $help = <<HELP;
5 Usage : $scriptname -h
6 $scriptname [-t] [work-directory]
7 HELP
8 my $testmode = 0;
9 my $prefix = "";
10 #check dependencies
11 die "Execution error: rename.pl needed but not found\n" if (`which rename.pl` eq '');
12 #check parameters
13 die $help if ($ARGV[0] eq '-h' or $ARGV[0] eq '--help');
14 foreach my $a (@ARGV)
15 {
16 if ($a eq '-t') { $testmode = 1; }
17 else
18 {
19 $a = $workdir.'/'.$a if ($a !~ /^\//);
20 if(-d $a and -r $a) { $workdir = $a; }
21 else { die "Syntax error: $a is not a valid directory\n$help"; }
22 }
23 }
24 #asks a pattern for rename.pl if unknown
25 if ($prefix eq '')
26 {
27 print "*** CAUTION: no prefix specified in substitution pattern for rename.pl\n";
28 print "*** Please fill in a prefix once for the whole process\n";
29 print "*** (press ENTER key to validate) : ";
30 chomp ($prefix = <STDIN>);
31 }
32 #look for subdirectories in $workdir
33 print "Scanning subdirectories in $workdir...\n";
34 chdir $workdir;
35 foreach $f (<*>)
36 {
37 my $path = $workdir.'/'.$f;
38 if (-d $path)
39 {
40 chdir $path;
41 my @all = <*>;
42 my @regularfiles = grep { -f } @all;
43 printf "\n> %s : %d files\n", $f, scalar @regularfiles;
44 next if (scalar @regularfiles == 0); #skip if empty
45 #parse numeric part of $path to use in rename.pl's pattern
46 $path =~ /(\d+)/;
47 my $num = $1;
48 system 'rename.pl '.($testmode == 1? '-t ':'').'"s/^/'.$prefix.$num.'_/" *'; #call rename.pl
49 #update files data (changed because of renaming)
50 @all = <*>;
51 @regularfiles = grep { -f } @all;
52 #~ print join ("\n", @regularfiles), "\n";
53 map { rename $_, $workdir.'/'.$_ } @regularfiles unless ($testmode); #move files one level up
54 #check for subdirectory inside
55 if (scalar (grep { -d } @all) > 0) { warn "Caution: subdirectory found in $f => not deleted\n"; next; }
56 chdir $workdir;
57 unless ($testmode) { rmdir $path or die "$!\n"; }
58 }
59 }