updated git and svn scripts
[xrzperl.git] / r-find
blobad32019c2a623837c1732de688ad1465384be181
1 #!/usr/bin/perl -w
2 ###APPNAME: r-find
3 ###APPAUTHOR: duel
4 ###APPDATE: 2008-08-27 12:46:27
5 ###APPVER: 0.1
6 ###APPDESC: extend find to support "-havename,-copy,-move,-from-file"
7 ###APPUSAGE: r-find [Options]
8 ###APPEXAMPLE: r-find
9 ###APPOPTION: -havename [text]:filename match *text*|-copy [Dir]:copy to dir|-move [Dir]:move to dir|-from-file (filename):read options from file
10 use strict;
12 #ENV variable MUST be defined somewhere,
13 #FOR perl to search modules from,
14 #OR nothing will work
15 use lib $ENV{XR_PERL_MODULE_DIR};
17 use MyPlace::Script::Usage qw/help_required help_even_empty/;
18 exit 0 if(help_required($0,@ARGV));
19 #exit 0 if(help_even_empty($0,@ARGV));
21 my $opt_exp = qr/^(-havename|-copy|-move|-from-file)$/;
22 my $file_exp = qr/^-from-file$/;
23 my $name_exp = qr/^-havename$/;
24 my $action_exp = qr/^(-copy|-move)$/;
25 my %actions = (
26 "--copy"=>["-exec","cp","-v","--","{}","NAME_ACTION_DEST",";"],
27 "--move"=>["-exec","mv","-v","--","{}","NAME_ACTION_DEST",";"]
30 my @names;
32 sub convert_opt($$) {
33 my $opt_key = shift;
34 my $opt = shift;
35 my @result;
36 #print STDERR "NAMES = ",join(" ",@names),"\n";
37 if($opt_key =~ $name_exp) {
38 push @result,"-name","*$opt*";
40 elsif($opt_key =~ $action_exp) {
41 my $name_dest = $opt;
42 if(!$opt) {
43 $name_dest = shift @names;
45 elsif($opt =~ m/^-/) {
46 $name_dest = shift @names;
48 $name_dest = shift @names unless($name_dest);
49 #print STDERR "Name_DEST = ",$name_dest,"\n";
50 die("No destionation found for $opt_key...\n") unless($name_dest);
51 mkdir $name_dest unless(-d $name_dest);
52 foreach my $action (@{$actions{$opt_key}}) {
53 my $para = $action;
54 $para =~ s/NAME_ACTION_DEST/$name_dest/g;
55 push @result,$para;
57 push @result,$opt if($opt and $opt =~ m/^-/);
59 else {
60 push @result,$opt_key,$opt;
62 #print STDERR ("key=$opt_key\tvalue=$opt\nresult=",join(" ",@result),"\n");
63 return @result;
66 sub collect_names(@) {
67 my $opt_key="";
68 my @result;
69 foreach my $opt (@_) {
70 if($opt_key) {
71 push @result,$opt;
72 $opt_key = 0;
74 else {
75 if($opt =~ $name_exp) {
76 $opt_key = 1;
80 return @result;
84 sub collect_opt(@) {
85 my $opt_key="";
86 my %result;
87 foreach my $opt (@_) {
88 if($opt_key) {
89 if($opt_key =~ $action_exp and $opt =~ $opt_exp) {
90 push @{$result{opt}},&convert_opt($opt_key,"");
91 $opt_key = $opt;
93 elsif($opt_key =~ $file_exp) {
94 push @{$result{files}},$opt;
95 $opt_key = "";
97 else {
98 push @{$result{opt}},&convert_opt($opt_key,$opt);
99 $opt_key="";
102 else {
103 if($opt =~ $opt_exp) {
104 $opt_key = $opt;
106 else {
107 push @{$result{opt}},$opt;
111 if($opt_key) {
112 push @{$result{opt}},&convert_opt($opt_key,"");
114 return \%result;
116 sub run_find(@) {
117 print STDERR join(" ","find",@_),"\n";
118 system("find",@_);
121 sub process(@) {
122 my @PARGS = @_;
123 # print STDERR "Process :",join(" ",@PARGS),"\n";
124 @names = ();
125 @names = &collect_names(@PARGS);
126 my $options = &collect_opt(@PARGS);
127 my @find_opt = $options->{opt} ? @{$options->{opt}} : ();
128 # print STDERR "Find Options:",join(" ",@find_opt),"\n";
129 my @files = $options->{files} ? @{$options->{files}} : ();
130 if(@files) {
131 foreach my $file(@files) {
132 if(!-f $file) {
133 print STDERR "File not exists:$file...\n";
134 next;
136 if(!open FI,"<",$file) {
137 print STDERR ("File not readable:$file...\n");
138 next;
140 print STDERR "From file : $file\n";
141 my @LINES = <FI>;
142 close FI;
143 foreach(@LINES){
144 chomp;
145 my @args = split(" ",$_);
146 &process(@find_opt,@args);
150 else {
151 &run_find(@find_opt);
155 process(@ARGV);