TVDB: better handling of first run
[nonametv.git] / tools / nonametv-filestore-moveold
blob31b35046f85ac8f93fc5437480e004e90fb70ce6
1 #!/usr/bin/perl -w
3 use strict;
5 use FindBin;
6 use lib "$FindBin::Bin/../lib";
8 use File::Util;
9 use File::Copy qw/copy move/;
11 use NonameTV;
12 use NonameTV::DataStore;
13 use NonameTV::Config qw/ReadConfig/;
14 use NonameTV::Log qw/progress error d p w f/;
16 use Getopt::Long;
18 my $opt = { 'verbose' => 0,
19 'all' => 0,
20 'xmltvid' => "",
21 'leavedays' => "",
24 my $res = GetOptions( $opt, qw/verbose all xmltvid=s leavedays=s/ );
26 if( not $opt->{xmltvid} and not $opt->{all} )
28 print << 'EOHELP';
29 nonametv-filestore-moveold --xmltvid <xmltvid>
31 --xmltvid <xmltvid>
32 Remove old files only for the channel specified
34 --all
35 Remove old files for all channels
37 --leavedays <days>
38 Leave all files that were touched in less then <days> days
40 --verbose
41 Be verbose
43 The tool is used for filestore directories cleanup. It creates 'old' subdirectory
44 in each channel directory inside of filestore and moves there all files
45 that were modified before the date given by leavedays argument.
47 EOHELP
49 exit 1;
52 my( $xmltvid ) = $opt->{xmltvid};
53 my( $leavedays ) = $opt->{leavedays};
55 $leavedays = 90 if not $leavedays;
57 NonameTV::Log::SetVerbosity( $opt->{verbose} );
59 # Read configuration
60 my $conf = ReadConfig();
61 my $ds = NonameTV::DataStore->new( $conf->{DataStore} );
62 my $filestore = $conf->{FileStore};
64 my $now = time();
65 my $date = localtime( $now - ( 86400 * $leavedays ) );
66 print "Moving all files modified before $date\n";
68 print "Reading directories in $filestore\n";
70 my(@fsfiles);
71 if( $opt->{all} ){
72 my($f) = File::Util->new();
73 # option --no-fsdots excludes "." and ".." from the list
74 @fsfiles = $f->list_dir( $filestore, '--no-fsdots' );
75 } elsif( $xmltvid ){
76 push( @fsfiles, $xmltvid );
79 foreach my $fsf (@fsfiles)
81 print "$fsf\n";
83 # check if the directory name is the xmltv name of a valid channel
84 my $xmltvid = $fsf;
85 my $channel_id = $ds->{sa}->Lookup( 'channels', { xmltvid => $xmltvid }, 'id' );
86 if( not $channel_id ){
87 progress( "There is no channel with xmltvid $xmltvid - skipping" );
88 next;
91 # check if there is a directory for this xmltvid
92 if( not File::Util->existent( "$filestore/$fsf" ) ){
93 progress( "The directory for $fsf does not exist. You should create it manually." );
94 next;
97 # skip if the file is not a directory
98 my $fsftype = join(',', File::Util->file_type( "$filestore/$fsf" ) );
99 if( $fsftype !~ /DIRECTORY/ ){
100 progress( "File $fsf is $fsftype - skipping" );
101 next;
104 # check if there is a subdirectory named 'old' inside of the channel directory
105 if( not File::Util->existent( "$filestore/$fsf/old" ) ){
106 progress( "Creating the subdirectory $fsf/old" );
107 File::Util->make_dir( "$filestore/$fsf/old", 0755 );
110 # list files inside of the channel directory and check when they have been touched
111 my( $cf ) = File::Util->new();
112 my( @cffiles ) = $cf->list_dir( "$filestore/$fsf", '--no-fsdots' );
113 foreach my $cff (@cffiles)
115 next if ( $cff =~ /^old$/ );
117 my $cftype = join(',', File::Util->file_type( "$filestore/$fsf/$cff" ) );
119 if( $cftype !~ /PLAIN/ ){
120 progress( "File $cff is $fsftype - skipping" );
121 next;
124 my $modified = File::Util->last_modified( "$filestore/$fsf/$cff" );
126 print " -> $cff (" . gmtime( $modified ) . ") ... ";
128 my $oktomove = isToMove( $modified, $leavedays );
129 if( $oktomove ){
130 move( "$filestore/$fsf/$cff", "$filestore/$fsf/old/$cff" );
131 print "moved to old\n";
132 } else {
133 print "leaving\n";
139 exit;
141 sub isToMove
143 my( $modified , $leavedays ) = @_;
145 return 0 if not $leavedays;
147 my $now = time();
148 my $thedate = $now - ( 86400 * $leavedays );
150 if( $modified lt $thedate ){
151 return 1;
154 return 0;