-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppic.pl
executable file
·125 lines (94 loc) · 2.35 KB
/
cppic.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#! /usr/bin/perl -w
=head1 NAME
cppic
=head1 SYNOPSIS
cppic I<SOURCE> I<DESTINATION> I<OPTIONS>
=head1 OPTIONS
=over
=item from I<N>
Copy pictures starting from number I<N>.
=item to I<N>
Copy pictures ending with number I<N>.
=item rotate
After copying, call I<jhead> to rotate the images.
=item freshen
Copy pictures starting from the last image already on I<DESTINATION>, to add
new pictures without replacing images that have been edited out.
=item move
Move the files instead of copying them.
=back
=cut
use strict;
use Getopt::Long;
use File::Copy;
umask 033;
my ($from,$to);
my $rotate=1;
my $freshen;
my $move;
my $downcase=1;
my $prefix = 'dsc';
my $suffix = 'jpg';
my $test;
GetOptions('from=i'=>\$from, 'to=i'=>\$to, 'rotate!'=>\$rotate,
'freshen!'=>\$freshen, 'move!'=>\$move, 'test!'=>\$test);
my $src = shift;
my $dst = shift;
my $template="${prefix}_%.4i.$suffix";
my @copied;
if ($freshen) {
my @glob = sort <$dst/${prefix}_*.$suffix>;
die "Can't freshen: no files found!" unless @glob;
unless (defined $from) {
#my $start = shift @glob;
my $start = pop @glob;
$start =~ m:/${prefix}_([0-9]+)\.$suffix:;
$from = $1+1;
}
}
#downcase after we've read dst.
if ($downcase) {
$prefix = uc $prefix;
$suffix = uc $suffix;
}
unless (defined($to) and defined($from)) {
my @glob = sort <$src/${prefix}_*.$suffix>;
#print "@glob\n";
die "No photos found in $src" unless @glob;
unless (defined $to) {
my $end = $glob[$#glob];
#warn "$end => ${prefix}_([0-9]+)\.$suffix";
$end =~ m:${prefix}_([0-9]+)\.$suffix:;
$to = $1;
};
unless (defined $from) {
my $start = $glob[0];
$start =~ m:${prefix}_([0-9]+)\.$suffix:;
$from = $1;
}
warn "copy from $from to $to\n";
};
foreach my $fileno ($from .. $to) {
my $file = sprintf $template, $fileno;
# my $srcf = "$src/$file";
my $srcf = "$src/".($downcase?uc($file):$file);
if (-r $srcf) {
my $dstf = $downcase?lc("$dst/$file"):"$dst/$file";
if ($test) {
#print "test $file -> $dstf\n";
} elsif ($move) {
move ($srcf, $dstf) or die "$file -> $dstf: $!\n";
} else {
copy ($srcf, $dstf) or die "$file -> $dstf: $!\n";
}
push @copied, $dstf;
warn "copy $file\n";
} else {
warn "skip $srcf\n";
}
}
#do auto rotation
if ($rotate and @copied) {
print "rotating images...\n";
system 'jhead', '-autorot', @copied;
};