71513: Card tokenization [upgrade implemented]
[freeside.git] / bin / rotate-cdrs
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Fcntl qw(:flock);
5 use IO::File;
6
7 my $dir = '/usr/local/etc/freeside/export/cdr';
8 #chdir $dir;
9
10 #XXX glob might not handle lots of args at some point...
11 foreach my $file ( glob("$dir/*/CDR*-spool.CSV") ) {
12
13   $file =~ m{(\d+)/CDR(\d+)-spool.CSV$}
14     or die "guru meditation #54: can't parse filename: $file\n";
15   my($custnum, $date) = ($1, $2);
16
17
18   my $alpha = 'A';
19   while ( -e "$dir/$custnum/CDR$date$alpha.CSV" ) {
20     $alpha++; # A -> Z -> AA etc.
21   }
22   my $newfile = "$dir/$custnum/CDR$date$alpha.CSV";
23
24   rename $file, $newfile
25     or die "$! moving $file to $newfile\n";
26
27   use IO::File;
28   my $lock = new IO::File ">>$newfile"
29     or die "can't open $newfile: $!\n";
30   sleep 1; #just in case.  i guess there's still a *remotely* possible
31            #race condition, but i'm not losing any sleep over it...  (rimshot)
32   flock($lock, LOCK_EX)
33     or die "can't lock $newfile: $!\n";
34   #okay we've got the lock, any pending write should be done...
35
36   print "$custnum: $newfile\n";
37
38 }