-
Notifications
You must be signed in to change notification settings - Fork 0
/
ips_cleanup
executable file
·54 lines (48 loc) · 2.13 KB
/
ips_cleanup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl -w
#
# cleanups stale (finished) tracking numbers after some time
# Started by Matija Nalis <[email protected]> 2010-12-26, GPLv3+
#
use strict;
my $AGE=$ARGV[0] || 14; # last update must be at lease this many days old
my $DEBUG=$ENV{'DEBUG'} || 1;
my $HOME=$ENV{'HOME'} or die "HOME directory not defined";
if ($HOME =~ m{^(/home/\w+(\w+|\w\.\w|\w/)+)$}) { $HOME = $1 } else { die "invalid chars in homedir $HOME"; }
my $SPOOL_DIR=$HOME . '/.ips_posta_hr'; # must be same as in bin/ips !
die "Usage: $0 [remove_after_x_days]" unless $AGE =~ /^\d+/;
# find all tracked files
opendir DIR, $SPOOL_DIR or die "can't opendir $SPOOL_DIR: $!";
my @files = grep { -f "$SPOOL_DIR/$_" } readdir(DIR);
closedir DIR;
foreach my $t (@files) {
if ($t =~ /^([A-Z0-9]{13})\.txt$/) {
$t = $1;
print "found tracking number $t in spool\n" if $DEBUG > 1;
my $parsed_file = "${SPOOL_DIR}/${t}.txt";
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($parsed_file);
my $diff = int((time()-$mtime)/24/60/60); # time difference in days
if ($diff > $AGE) {
print " it is old enough ($diff > $AGE days)\n" if $DEBUG > 1;
open (T, '<', $parsed_file) or die "can't open $parsed_file: $!";
my $delivered=0;
while (<T>) {
if (/Pošiljka uručena primatelju/i) {
$delivered=1;
print " and it has been delivered: $_" if $DEBUG > 2;
last;
}
}
close (T);
if ($delivered) { # remove old & delivered packages
my $out = `cat ${SPOOL_DIR}/${t}.txt`;
$out =~ s/^/ /gm;
my $cnt = unlink "${SPOOL_DIR}/${t}.txt", "${SPOOL_DIR}/${t}.txt.new", "${SPOOL_DIR}/${t}.raw", "${SPOOL_DIR}/${t}.desc";
print "* Removed $cnt files for tracking number $t\n" if $DEBUG > 0;
print "$out\n\n" if $DEBUG > 1;
}
} else {
print " it is still too young ($diff < $AGE days)\n" if $DEBUG > 1;
}
}
}
exit 0;