Sun, 15 Aug 2021 23:52:22 -0400
add kshenoy/vim-signature
476 | 1 | #!/usr/bin/env perl |
677 | 2 | |
549
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
3 | use 5.014; |
476 | 4 | use warnings; |
5 | use strict; | |
677 | 6 | |
689
840057d4b493
portability improvement
Meredith Howard <mhoward@roomag.org>
parents:
677
diff
changeset
|
7 | use lib glob('~/.lib/perl5'); |
677 | 8 | use Autofetch; |
9 | ||
476 | 10 | use Time::Piece; |
11 | use Path::Tiny; | |
12 | use Getopt::Long::Descriptive; | |
13 | ||
14 | sub main { | |
15 | my ($opt, $usage) = describe_options( | |
16 | 'archive-dir %o <directory> ...', | |
761 | 17 | ['dest|d=s' => 'destination path' => {default => '.'}], |
18 | ['fileinto|F=s' => 'file into, a strftime format', {default => '%Y'}], | |
19 | ['age|a=i' => "minimum age in days for archival", {default => 60}], | |
20 | ['files|f' => "operate on plain files only"], | |
21 | [], | |
22 | ['yes|Y' => "actually move stuff"], | |
23 | ['help|h' => "print usage message and exit", {shortcircuit => 1}], | |
24 | {show_defaults => 1} | |
476 | 25 | ); |
485
a58682115510
print usage on no args too
Meredith Howard <mhoward@roomag.org>
parents:
477
diff
changeset
|
26 | print($usage->text), exit if $opt->help || !@ARGV; |
476 | 27 | |
28 | archive_dir($opt, $_) for @ARGV; | |
29 | } | |
30 | ||
31 | sub archive_dir { | |
32 | my $opt = shift; | |
33 | my $dir = path(shift); | |
761 | 34 | my $destdir = path($opt->dest); |
476 | 35 | $destdir = $dir->child($destdir) if $destdir->is_relative; |
761 | 36 | my $destfmt = $opt->fileinto; |
476 | 37 | my $nowish = time; |
38 | my $age = $opt->age * 24 * 60 * 60; | |
39 | ||
549
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
40 | my @work; |
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
41 | |
476 | 42 | for my $child ($dir->children) { |
477
eeafc178ddc4
add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents:
476
diff
changeset
|
43 | next if $child->is_dir && ( |
eeafc178ddc4
add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents:
476
diff
changeset
|
44 | $opt->files || $child eq $destdir || $child =~ /^(?:\d{4}|\d\d)$/ |
eeafc178ddc4
add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents:
476
diff
changeset
|
45 | ); |
eeafc178ddc4
add -f for only plain files
Meredith Howard <mhoward@roomag.org>
parents:
476
diff
changeset
|
46 | next if $opt->files && $child->basename =~ /^\./; |
476 | 47 | |
48 | my $mtime = $child->stat->mtime; | |
49 | next unless ($nowish - $mtime) >= $age; | |
50 | ||
51 | my $dest = $destdir->child(localtime($mtime)->strftime($destfmt)); | |
549
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
52 | push @work, [$dest, $child]; |
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
53 | } |
476 | 54 | |
549
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
55 | my $lastdest = ''; |
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
56 | for my $work (sort {$a->[0] cmp $b->[0]} @work) { |
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
57 | my ($dest, $child) = @$work; |
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
58 | |
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
59 | say $lastdest = $dest |
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
60 | if $dest ne $lastdest; |
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
61 | |
0bd45054802e
sort and format plan before working
Meredith Howard <mhoward@roomag.org>
parents:
529
diff
changeset
|
62 | say "\t$child"; |
476 | 63 | |
64 | next unless $opt->yes; | |
65 | $dest->mkpath; | |
66 | $child->move($dest . '/' . $child->relative($dir)); | |
67 | } | |
68 | } | |
69 | ||
70 | main(); |