new automatic way of building the DMG package.
[AutomatorExifMover.git] / main.command
blobd0e8bb81f0934a839e9ab7ea5329f6ad1202f5f5
1 #!/usr/bin/perl
3 # main.command
4 # AutoExifMover
6 # Created by Pierre Andrews on 01/07/2007.
7 # Copyright 2007 Pierre Andrews. All rights reserved.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or any later version.
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.
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
22 # add our 'lib' directory to the include list BEFORE 'use Image::ExifTool'
23 my $exeDir;
24 BEGIN {
25 # get exe directory
26 $exeDir = ($0 =~ /(.*)[\\\/]/) ? $1 : '.';
27 # add lib directory at start of include path
28 unshift @INC, "$exeDir/lib";
31 use Image::ExifTool;
32 use File::Path;
33 use File::Basename;
34 use File::Copy;
36 #my $homedir=`ksh -c "(cd ~ 2>/dev/null && /bin/pwd)"`;
37 #chomp($homedir);
38 $homedir = $ENV{'HOME'};
40 my $exifTool = new Image::ExifTool;
41 my $pattern = $ENV{'pathPattern'};
42 if(!$pattern) {
43 $pattern = "%Y_%m_%d/";
45 $exifTool->Options(DateFormat => $pattern);
47 while(<>) {
48 chomp;
50 if(-e $_) {
52 my $file = $_;
53 my $name;
54 my $dir;
55 my $suffix;
56 my $with_basename=0;
57 ($name,$dir,$suffix) = fileparse($file,qr/\.[^.]*$/);
58 my $destPath = $ENV{'directoryPath'};
59 if(!$destPath) { $destPath = $dir; }
60 my $info = $exifTool->ImageInfo($file, 'DateTimeOriginal');
61 my $path = $$info{'DateTimeOriginal'};
62 if(!$path) {
63 $info = $exifTool->ImageInfo($file, 'FileModifyDate');
64 $path = $$info{'FileModifyDate'};
66 if(!$path && $pattern !~ /%[A-Za-z]/) {
67 $path = $pattern;
69 if($path) {
70 while($path =~ /:([a-zA-Z]+):/g) {
71 $label = $1;
72 if($label =~ /basename/i) {
73 $with_basename=true;
74 $path =~ s/:basename:/$name/g;
75 } elsif($label =~ /ext/i) {
76 $path =~ s/:ext:/$suffix/g;
77 } else {
78 my $info = $exifTool->ImageInfo($_, "$label");
79 if($$info{"$label"}) {
80 my $value = $$info{"$label"};
81 $value =~ s/^\s+//;
82 $value =~ s/\s+$//;
83 $value =~ s/\//_/;
84 chomp($value);
85 $value =~ s/ /_/g;
86 $path =~ s/:$label:/$value/g;
87 } else {
88 $path =~ s/:$label://g;
92 $path =~ s/[^A-Za-z0-9_\/.\-~]/_/g;
93 $path = $destPath.'/'.$path;
94 $path =~ s/^~/$homedir/;
96 ($new_name,$new_dir,$new_suffix) = fileparse($path,qr/\.[^.]*$/);
97 if($new_name && !$with_basename) {
98 $path = $new_dir.'/'.$new_name.$new_suffix;
100 if(!$new_name) {
101 $path .= $name.$suffix;
102 $new_name = $name;
103 $new_suffix = $suffix;
105 if(!$new_suffix || $new_suffix!=$suffix) {
106 $path .= $suffix;
110 if(!$ENV{'test'}) { mkpath($new_dir); }
111 if(!$ENV{'overwrite'}) {
112 if(-e $path) {
113 if($path !~ /:cnt:/i) {
114 $path =~ s/(\.[^.]*)$/_:cnt:$1/;
116 my $local_cnt = 1;
117 $new_path = $path;
118 $new_path =~ s/:cnt:/$local_cnt/g;
119 while(-e $new_path) {
120 $local_cnt++;
121 $new_path = $path;
122 $new_path =~ s/:cnt:/$local_cnt/g;
124 $path = $new_path;
126 $path =~ s/_+/_/g;
127 $path =~ s/_:cnt://g;
128 } else {
129 $path =~ s/:cnt://g;
132 if(!$ENV{'test'}) {
133 if($ENV{'action'} == 1) {
134 move($file,$path);
135 } else {
136 copy($file,$path);
139 print $path."\n";