Make all scripts executable
[project-aon.git] / common / scripts / convert-ill.pl
1 #!/usr/bin/perl  -w
2 # This script recurses a directory and creates a similar structure
3 # but converting the files from a given format to a different one.
4 #
5 # This program is copyright 2009 by Javier Fernandez-Sanguino <jfs@debian.org>
6 #
7 #    This program is free software; you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation; either version 2 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program; if not, write to the Free Software
19 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 #
21 # For more information please see
22 #  http://www.gnu.org/licenses/licenses.html#GPL
23
24
25 $oldformat="gif";
26 $newformat="png";
27 $sourcedir = `pwd`;
28 chomp($sourcedir);
29
30 convert_recurse_dir($sourcedir."/".$oldformat);
31 exit 0;
32
33 sub convert_recurse_dir ($) {
34     my ($dir) = @_;
35     my $DIRFH;
36     print "Looking $dir\n";
37     opendir $DIRFH, $dir || die ("Cannot open directory $dir: $!");
38
39     while ( $file = readdir($DIRFH) ) {
40         next if $file =~ /^\./; # Skip hidden files and directories
41         $filename = $dir."/".$file;
42         print "Checking $filename\n";
43         if ( -d "$filename" ) {
44             $newdir = $filename;
45             $newdir =~ s/\/$oldformat\//\/$newformat\//;
46             if ( ! -d "$newdir" ) {
47                 print "Creating $newdir\n";
48                 `mkdir $newdir`;
49             }
50             convert_recurse_dir($filename);
51         } 
52         if ( -f "$filename" ) {
53             copy_file($filename);
54         }
55         print "Done with $filename\n";
56     }
57     closedir $DIRFH;
58     return 0;
59 }
60
61 sub copy_file ($) {
62     my ($file) = @_;
63     $newfile = $file;
64     $newfile =~ s/\/$oldformat\//\/$newformat\//;
65     $newfile =~ s/\.$oldformat$/\.$newformat/;
66     if ( ! -e "$newfile" ) {
67         print "Converting $file to $newfile\n";
68         `convert $file $newfile`;
69     }
70     return 0;
71 }
72