Make sure the hashbang line works after 'make install'
[rersyncrecent.git] / bin / rrr-init
blobe465f870c26d9fb8464f338302c49a79d9a4e1a4
1 #!/usr/bin/perl
3 =head1 NAME
5 rrr-init - set up RECENT files for a directory tree
7 =head1 SYNOPSIS
9 rrr-init [options] directory
11 =head1 OPTIONS
13 =over 8
15 =cut
17 my @opt = <<'=back' =~ /B<--(\S+)>/g;
19 =item B<--aggregator=s>
21 Comma separated list of aggregator specifications, e.g.
23 --aggregator=1h,6h,1d,1W,1M,1Q,1Y,Z
25 Defaults to C<1h,1d,1M,1Y,Z>
27 =item B<--force|f>
29 Forces an overwrite of an already existing recentfile in the target
30 directory.
32 =item B<--help|h>
34 Prints a brief message and exists.
36 =item B<--verbose|v+>
38 More feedback.
40 =back
42 =head1 DESCRIPTION
44 Walk through a tree and fill all files into initial recentfiles.
46 =cut
49 use strict;
50 use warnings;
52 use File::Find::Rule;
53 use lib "/home/k/sources/rersyncrecent/lib";
54 use File::Rsync::Mirror::Recent;
55 use File::Rsync::Mirror::Recentfile;
56 use File::Spec;
57 use Getopt::Long;
58 use Pod::Usage qw(pod2usage);
59 use Time::HiRes qw(time);
61 our %Opt;
62 GetOptions(\%Opt,
63 @opt,
64 ) or pod2usage(1);
66 if ($Opt{help}) {
67 pod2usage(0);
70 if (@ARGV != 1) {
71 pod2usage(1);
74 my($rootdir) = @ARGV;
75 my $aggregator_string = $Opt{aggregator} || "1h,1d,1M,1Y,Z";
76 my @aggregator = split /\s*,\s*/, $aggregator_string;
77 my $localroot = File::Spec->rel2abs($rootdir);
78 my $rfconstructor = sub {
79 return File::Rsync::Mirror::Recentfile->new
81 aggregator => \@aggregator,
82 interval => $aggregator[0],
83 localroot => $localroot,
84 verbose => $Opt{verbose},
87 my $rf = $rfconstructor->();
88 my $rfilename = File::Spec->catfile
90 $rootdir,
91 $rf->rfilename,
94 if (-e $rfilename) {
95 if ($Opt{force}) {
96 unlink $rfilename or die "Could not unlink '%s': %s", $rfilename, $!;
97 $rf = $rfconstructor->();
98 } else {
99 die sprintf "Alert: Found an already existing file '%s'. Won't overwrite. Either use --force or remove the file before calling me again", $rfilename;
101 } else {
102 warn "Debug: rfilename[$rfilename]";
104 my @t = time;
105 my @batch;
106 foreach my $file ( File::Find::Rule->new->file->in($rootdir) ) {
107 my $path = File::Spec->rel2abs($file);
108 my $epoch = (stat $path)[9];
109 push @batch, {path=>$path,type=>"new",epoch=>$epoch};
111 if ($Opt{verbose}) {
112 $t[1] = time;
113 warn sprintf "Found %d files to register in %.6f s. Writing to %s\n", scalar @batch, $t[1]-$t[0], $rfilename;
115 $rf->batch_update(\@batch);
116 if ($Opt{verbose}) {
117 $t[2] = time;
118 warn sprintf "Registered %d files in %.6f s\n", scalar @batch, $t[2]-$t[1];
121 __END__
124 # Local Variables:
125 # mode: cperl
126 # coding: utf-8
127 # cperl-indent-level: 4
128 # End: