Recognizes if input is ogg or not.
[xiph.git] / vorbis-tools / file-replace.pl
blobb61479f4600f1fa0f025ce186454c805e433ba39
1 #!/usr/bin/perl
3 # parse the arguments:
4 # file-replace [options] [--] filename[s]|dir[s]
5 # --in-pattern="search for"
6 # --out-pattern="replace with"
7 # --in-pattern-file="file"
8 # --out-pattern-file="file"
9 # --no-recurse
10 # --no-directories
13 my $search;
14 my $replace;
15 my $argflag;
16 my @infiles;
17 my $norecurse;
18 my $nodirs;
19 my$pwd=`pwd`;
20 chomp($pwd);
22 foreach $arg (@ARGV){
23 if($arg eq "--"){
24 $argflag=1;
25 next;
28 if(!$argflag && $arg=~/--([^=]*)=(.*)/){
29 my$key=$1;
30 my$val=$2;
32 if($key eq "in-pattern"){
33 $search=$val;
34 next;
36 if($key eq "out-pattern"){
37 $replace=$val;
38 next;
40 if($key eq "in-pattern-file"){
41 die "Could not open file $val: $!" unless open(F,"$val");
42 undef $/;
43 $search=<F>;
44 $/="\n";
45 close(F);
46 next;
48 if($key eq "out-pattern-file"){
49 die "Could not open file $val: $!" unless open(F,"$val");
50 undef $/;
51 $replace=<F>;
52 $/="\n";
53 close(F);
54 next;
57 print "Unknown option --$key\n";
58 exit(1);
62 if(!$argflag && $arg=~/--(.*)/){
63 if($key eq "no-recurse"){
64 $norecurse;
65 next;
67 if($key eq "no-directories"){
68 $nodirs=1;
69 next;
71 print "Unknown option --$key\n";
72 exit(1);
75 push @infiles, ($arg);
78 &recursive_doit($pwd,@infiles);
80 sub recursive_doit{
81 my($pwd,@globlist)=@_;
82 my @dirs;
83 my @files;
85 # seperate files from directories
86 foreach $file (@globlist){
87 if(-d $file){
88 push @dirs,($file);
89 next;
91 if(-f $file){
92 push @files,($file);
93 next;
95 print "$pwd/$file is not a plain file or directory.\n";
98 # Are we called on a directory? recurse?
99 if(!$nodirs){
100 # fork into each dir with all but the original path ar
101 foreach $dir (@dirs){
103 if(fork){
104 wait; # don't hose the box ;-)
105 }else{
106 die "Could not chdir to $pwd/$dir: $!\n" unless chdir $dir;
107 $pwd.="/$dir";
108 # open and read the dir
109 die "Could not read directory $pwd: $!\n" unless
110 opendir (D,".");
111 #ignore dotfiles
112 @globlist=grep { /^[^\.]/ && !(-l "$_") } readdir(D);
113 closedir(D);
114 $nodirs=$norecurse;
116 recursive_doit($pwd,@globlist);
117 exit(0);
122 foreach $file (@files){
123 if (open(F,$file)){
124 undef $/;
125 my$body=<F>;
126 $/="\n";
127 close(F);
129 # do the regexp
130 if($body=~s{$search}{$replace}g){
132 print "Performed substitution on $pwd/$file\n";
134 # replace with modified file
135 my$tempfile="file-replace-tmp_$$";
136 die $! unless open(F,">$tempfile");
137 syswrite F,$body;
138 close(F);
139 die "Unable to replace modified file $file: $!\n" unless
140 rename($tempfile,$file);
141 unlink $tempfile;
144 }else{
145 print "Could not open $pwd/$file: $!\n";
148 exit(0);