package Ernad::Rifdir; ## functions to use on Rifs use strict; use warnings; use Carp qw(confess); use Ernad::Dates; use Ernad::Constant; ## let's put the dircetory first every sub is_there_of_date { my $dir=shift; my $date=shift; my $do_strict=shift // ''; if(not -d $dir) { if($do_strict) { confess "I don't see your directory '$dir'."; } return 0; } if(not &Ernad::Dates::is($date)) { if($do_strict) { confess "You date '$date' is not a date."; } return 0; } my $rif_ext=$Ernad::Constant::c->{'rif_ext'} // confess "I need a rif extension."; ## first check for .gz my $glob; $glob="$dir/*$date*$rif_ext.gz"; my @files=glob($glob); if(scalar(@files)) { return 1; } $glob="$dir/*$date*$rif_ext"; @files=glob($glob); if(scalar(@files)) { return 1; } return 0; } sub get_latest_from_each_issue { my $dir=shift // ''; my $issues=&list_issues($dir); my $rifs=(); foreach my $issue (keys %$issues) { my $rif=&get_latest_rif($dir,$issue); push(@$rifs,$rif); } return $rifs; } ## used in Erimp.pm sub list_issues { my $dir=shift // ''; opendir(my $dh, $dir) or confess "I can't opendir $dir: $!"; my $issues=(); foreach my $filename (readdir($dh)) { if(not is_rif_name($filename)) { # print "'$filename is not a rif.\n"; next; } my $issuedate=substr($filename,0,10); ## count the number of times we have a file of an issue date from 0 if(not defined($issues->{$issuedate})) { $issues->{$issuedate}=0; } else { $issues->{$issuedate}++; } } return $issues; } ## extends the previous function to the case when there is ## a subdir, used with source sub get_latest_with_issuedate_from_subdir { my $in_dir=shift // confess "I need a dircetrotry here."; my $issuedate=shift or confess "I need an issuedate here."; if(not -d $in_dir) { confess "I need a dirctory here"; } my $latest_tist=0; my $latest_rif=''; opendir(my $in_dh, $in_dir) or confess "I can't opendir $in_dir: $!"; foreach my $subdir (readdir($in_dh)) { my $dir="$in_dir/$subdir"; if(not -d "$in_dir/$subdir") { next; } my @rifs=glob("$dir/$issuedate*"); foreach my $rif (@rifs) { if(not is_rif_name($rif)) { next; } my $tist=&get_rif_tist($rif); if($tist > $latest_tist) { $latest_tist=$tist; $latest_rif=$rif; } } } return $latest_rif; } 1;