forked from rjbs/misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
booksync
executable file
·57 lines (45 loc) · 1.44 KB
/
booksync
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
#!/usr/bin/env perl
use 5.14.1;
use File::Copy;
use File::Find::Rule;
use File::Path qw(make_path remove_tree);
use Path::Class;
use Term::ANSIColor;
my $l_root = dir('/Users/rjbs/Dropbox/books/RPG');
# my $r_root = dir('/Volumes/Kindle/documents/dnd');
my $r_root = dir('/Volumes/HP TOUCHPAD/books/rpg');
my @local = map { file($_)->relative($l_root) }
File::Find::Rule->file->in($l_root);
my @remote = map { file($_)->relative($r_root) }
File::Find::Rule->file->in($r_root);
my @to_cp;
for my $file (@local) {
next if -e $r_root->file($file);
next if $file->basename =~ /^\./; # skip dotfiles
next unless $file =~ /\.(?: pdf )$/x;
print colored("+ $file\n", 'green');
push @to_cp, $file;
}
my @to_rm;
for my $file (@remote) {
next if -e $l_root->file($file);
if ($file->basename =~ /\.pdr$/) {
# Kindle metadata
next if -e $r_root->file($file)->stringify =~ s/pdr$/pdf/r;
}
print colored("- $file\n", 'red');
push @to_rm, $file;
}
print "...press enter to continue...\n";
scalar <STDIN>;
for my $file (@to_cp) {
my $target = $r_root->file( $file );
make_path( $target->dir, { }) unless -d $target->dir;
copy($l_root->file($file), $target->dir) or die "can't copy $file: $!";
}
for my $file (@to_rm) {
my $target = $r_root->file($file);
unlink $target or die "can't unlink $target: $!";
my @files = File::Find::Rule->file->in( $target->dir );
remove_tree($target->dir, { }) unless @files;
}