chiark / gitweb /
expire-8601: wip new version
[chiark-utils.git] / scripts / expire-iso8601.new
index 25a4cd79d8e4da6ac68388aeefd8a0117a7e2efc..6c8ffd947e5379523cd6bddb9d5b6977c1173ff4 100755 (executable)
@@ -44,8 +44,11 @@ exit status:
    16                  catastrophic failure
 END
 
+use POSIX;
 use Carp;
-use DateTime::Format::ISO8601;
+use Data::Dumper;
+use Date::Parse;
+use DateTime::Format::Strptime;
 
 our @files;
 our $rm = 1;
@@ -54,18 +57,32 @@ our $unit = 86400;
 our $slop;
 our @intervals;
 
-sub scan () {
-  my $parser = DateTime::Format::ISO8601->new;
+sub badusage ($) {
+  print STDERR "bad usage: $_[0]\n$usage" or die $!;
+  exit 8;
+}
 
+sub scan () {
+#  my $strp = DateTime::Format::Strptime->new();
   foreach my $f (<[0-9]*>) {
     if ($f  !~ m/^ \d\d\d\d - \d\d - \d\d 
                 (?: T \d\d \: \d\d (?: \: \d\d )?
                   (?: [-+] \d{1,2} \:? \d\d )? )? /x) {
       print STDERR "ignoring $f\n";
     }
-    my $t = $parser->parse_time($f) // confess;
-    my $t = $t->epoch() // confess;
-    push @files, { F => $f, T => $t, U => [] ];
+    my @t = Date::Parse::strptime($f);
+    @t = map { $_ // 0 } @t;
+    my $t = mktime @t;
+#    m
+#    my $t = $strp->parse_datetime($f);
+#    $t = $t->epoch();
+#    my @t = Date::Parse::strptime($f);
+#print STDERR Dumper(\@t);
+#    my $t = mktime(@t);
+#    $!=0; $?=0; my $t = `date -d '$&' +%s`;
+#    die "date(!) failed on $&: $? $!" if $! || $?;
+#    chomp $t or confess;
+    push @files, { F => $f, T => $t, U => [] };
   }
 }
 
@@ -80,6 +97,8 @@ sub precomp () {
   my $newest_t = $files[0]{T};
   $_->{A} = ($newest_t - $_->{T}) / $unit foreach @files;
   $slop /= $unit;
+
+#  print DEBUG Dumper(\@files, \@intervals);
 }
 
 sub flag ($) {
@@ -87,11 +106,14 @@ sub flag ($) {
   my $n = $int->{Number};
   my $d = $int->{Interval};
   my $spec = $int->{Spec};
-  my $start_age = $int->{Number+1) * $interval;
+  my $start_age = ($n+1) * $d;
   my $i = 0;
 
+  print DEBUG "FLAG $spec\n";
+
   # find $i, the youngest which is at least $number x $interval
   for (;;) {
+    print DEBUG "i #$i $files[$i]{A}\n";
     last if $files[$i]{A} > $start_age;
     if ($i == $#files) {
       print STDERR "insufficient for $spec\n";
@@ -100,7 +122,7 @@ sub flag ($) {
   }
 
   for (;;) {
-    push $files[$i]{U}, $spec;
+    push @{ $files[$i]{U} }, $spec;
 
     # find $j, the closest to $i which is at least $d-slop younger
     my $j = $i;
@@ -109,6 +131,7 @@ sub flag ($) {
       last if $j < 0;
       last if $files[$j];
       my $dt = $files[$i]{A} - $files[$j]{A};
+      print DEBUG "j #$j $files[$j]{A} dt=$dt\n";
       last if $dt >= $d - $slop;
     }
     last if $j < 0;
@@ -124,15 +147,17 @@ sub implement () {
   }
   foreach (@files) {
     next if @{$_->{U}};
-    print "remove $_>{F}\n";
+    print "remove $_->{F}\n";
     if ($rm) {
       my $r= system 'rm', ($recurse ? ('-r') : ()), "--", $_->{F};
       die "run rm: $!\n" unless defined($r) && $r >= 0;
-      exit 16 if $r;
+      exit 12 if $r;
     }
   }
 }
 
+open DEBUG, ">/dev/null" or die $!;
+
 while (@ARGV && $ARGV[0] =~ m/^-/) {
   $_ = shift @ARGV;
   last if $_ eq '-' || $_ eq '--';
@@ -140,6 +165,7 @@ while (@ARGV && $ARGV[0] =~ m/^-/) {
     while (m/^-./) {
       if (s/^-n/-/) { $rm=0; }
       elsif (s/-r/-/) { $recurse=1; }
+      elsif (s/-D/-/) { open DEBUG, ">&STDERR" or die $!; }
       elsif (s/-u(\d+)$//) { $unit=$1; }
       elsif (s/-s(\d+)$//) { $slop=$1; }
       else { badusage "unknown short option $_" }
@@ -158,10 +184,10 @@ $slop //= $unit * 0.1;
 
 foreach (@ARGV) {
   m/^(\d+)x(\d+)$/ or badusage "bad <number>x<interval> $_";
-  push @intervals, { Spec => $&, N => $1, I => $2 ];
+  push @intervals, { Spec => $&, N => $1, I => $2 };
 }
 
 scan();
 precomp();
-foreach $int (@intervals) { flag $int }
+foreach (@intervals) { flag $_ }
 implement();