#!/usr/bin/env perl
|
|
|
|
=pod
|
|
|
|
=head1 NAME
|
|
|
|
photocp - copy files from digital camera media to my home dir
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
# try a couple of defaults
|
|
photocp
|
|
|
|
# look in source_directory
|
|
photocp [source_directory]
|
|
|
|
=head1 AUTHOR
|
|
|
|
Brennen Bearnes <bbearnes@gmail.com>
|
|
|
|
=cut
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use 5.10.0;
|
|
use POSIX qw(strftime);
|
|
use File::Basename;
|
|
use File::Copy;
|
|
use File::Find;
|
|
use File::HomeDir;
|
|
|
|
my $home = File::HomeDir->my_home;
|
|
|
|
my @sources = (
|
|
'/media/' . $ENV{'USER'} . '/EOS_DIGITAL',
|
|
'/media/' . $ENV{'USER'} . '/CANON_DC',
|
|
'/media/' . $ENV{'USER'} . '/NIKON D600',
|
|
'/media/CANON_DC',
|
|
'/media/EOS_DIGITAL',
|
|
'/media/disk/DCIM',
|
|
'/media/NIKON D600'
|
|
);
|
|
|
|
# strftime doesn't have a non-zero-padded month/day?
|
|
# am i an idiot?
|
|
my @t = localtime;
|
|
my $daydir = strftime("%Y-%m-%e", @t);
|
|
$daydir =~ s{-[0 ]}{-}g;
|
|
|
|
my $src;
|
|
if ((defined $ARGV[0]) && (-d $ARGV[0])) {
|
|
$src = $ARGV[0];
|
|
} else {
|
|
foreach my $source (@sources) {
|
|
if (-d $source) {
|
|
$src = $source;
|
|
last;
|
|
}
|
|
}
|
|
unless ($src) {
|
|
die 'source not mounted? tried ' . join(' ', @sources);
|
|
}
|
|
}
|
|
|
|
my $dest = $home . '/photos/' . $daydir;
|
|
|
|
if ((-e $dest) && (! -d $dest)) {
|
|
die 'target exists but is not a directory';
|
|
}
|
|
|
|
if (! -d $dest) {
|
|
say "creating $dest";
|
|
mkdir($dest);
|
|
} else {
|
|
say "$dest exists";
|
|
}
|
|
|
|
my $count = 0;
|
|
|
|
find(
|
|
sub {
|
|
if ( /[.] (jpe?g | mpe?g | avi | raw | cr2 | tiff? | bmp | gif | mov) $/ix ) {
|
|
$count++;
|
|
say "copying $File::Find::name to $dest/$_";
|
|
copy($File::Find::name, "$dest/$_")
|
|
or die "Copy failed: $!";
|
|
}
|
|
},
|
|
$src
|
|
);
|
|
|
|
say "$count files copied";
|