package Ernad::FileInfo; use strict; use warnings; use Carp qw(confess); use File::Basename; use Ernad::Dates; use Ernad::Store; use Krichel::Shoti; ## FixMe: should be set in Ernad::Dates, but that did not work on 2018-06-08 my $date_regex='\d{4}-\d{2}-\d{2}'; sub issuedate { my $file = shift // confess "I need a file name here"; my $do_strict= shift // ''; my $rif_ext=$Ernad::Constant::c->{'rif_ext'} or die; my $bana=basename($file); ## for advert files if($bana=~m|^(\d{4}-\d{2}-\d{2})\.xml$|) { return $1; } if((not $bana=~m|$rif_ext$|) and (not $bana=~m|$rif_ext\.gz$|)) { warn "I warn: file $bana is not a rif."; } if($bana=~m|^($date_regex)_$date_regex|) { my $first_date=$1; ## FixMe: nasty issue with old bims namf, this condition should ## not be there if((&Ernad::Dates::compare_dates('2017-12-23',$first_date)) > 0) { my $issuedate=&Ernad::Dates::get_shift($first_date,1); return $issuedate; } else { return $first_date; } } $bana=~m|^($date_regex)|; my $issuedate=$1; if($do_strict and not $issuedate) { confess "I don't see the issuedate on '$file'."; } return $issuedate; } sub shoti { my $file = shift; my $do_strict= shift // ''; my $rif_ext=$Ernad::Constant::c->{'rif_ext'} or die; $file=basename($file); if((not $file=~m|$rif_ext$|) and (not $file=~m|$rif_ext\.gz$|)) { #warn "file $file is not a rif"; } #my $date_regex=$Ernad::Dates::regex; ## Xmas 2017 if($file=~m|^($date_regex)_(\d{10})|) { confess "I should not have such a file $file around."; my $tist=$2; my $shoti=&Krichel::Shoti::make($tist); return $shoti; } if($file=~m|^($date_regex)_([0-9a-z]{6})|) { my $shoti=$2; return $shoti; } if($do_strict) { confess "I can't find the time on file '$file'"; } return 0; } sub stage { my $file = shift; my $do_strict=shift // ''; my @stages=('source','created','filtered', 'ordered', 'selected', 'sent', 'namf'); foreach my $stage (@stages) { if($file=~m|/$stage/|) { return $stage; } } if($do_strict) { confess "I can't find the time on file '$file'"; } return 0; } sub repcode { my $file = shift; my $do_strict=shift // ''; my $stage=&stage($file); if($file=~m|/([^-/]+-[^-/]+)/$stage/|) { return $1; } ## case of test files for typing if($file=~m|/([^/]+)/([^/]+)_[a-z0-9]+\.test$|) { if(Ernad::Dates::is($2)) { return $1; } } if($do_strict) { confess "I can't find the repocde in file $file"; } return ''; } sub impna { my $file=shift or confess "I need a file name here."; my $home_dir=$ENV{'HOME'} // '/home/ernad'; my $ernad_dir="$home_dir/ernad"; my $etc_dir="$ernad_dir/etc"; ## use the etc dir ## we don't check if the file is there ... foreach my $part (reverse split('/',$file)) { if(-d "$etc_dir/$part") { return $part; } } return ''; } 1;