chiark / gitweb /
update: Actually update the last entry in the file.
[dvddb] / report
1 #! /usr/bin/perl -w
2
3 use open ":utf8";
4 use strict;
5
6 use DBI;
7 use Encode qw{encode_utf8 decode_utf8};
8 use File::Find;
9
10 BEGIN { binmode STDOUT, ":utf8"; }
11
12 my $ROOT = "/mnt/dvd/archive";
13 my $DB = DBI->connect("dbi:Pg:host=roadstar", "", "",
14                       { AutoCommit => 0,
15                         RaiseError => 1,
16                         ReadOnly => 1 });
17
18 my %iso = ();
19 find(sub {
20        if (/\.iso$/ && ! -l && -f) {
21          my $fn = decode_utf8 $File::Find::name;
22          $fn =~ s:^$ROOT/::;
23          $iso{$fn} = 1;
24        }
25      }, $ROOT);
26
27 my %set_path;
28 my %set_id;
29 my %box;
30 my $st_set = $DB->prepare("SELECT id, name, n_disc FROM dvd_set");
31 my $st_disc = $DB->prepare
32   ("SELECT disc, path, box FROM dvd_disc WHERE set_id = ? ORDER BY disc");
33 $st_set->execute;
34
35 while (my @r = $st_set->fetchrow_array) {
36   my ($id, $name, $ndisc) = @r;
37   my @path;
38
39   $st_disc->execute($id);
40   while (my @r = $st_disc->fetchrow_array) {
41     my ($disc, $path, $box) = @r;
42     $disc == @path or die "bad disc sequence for `$name'";
43     $box{$path} = $box if defined $box;
44     push @path, $path;
45   }
46   @path == $ndisc or die "wrong number of discs for `$name'";
47   $set_path{$name} = \@path; $set_id{$name} = $id;
48 }
49
50 for my $name (keys %set_path) {
51   my $allp = 1;
52   for my $path (@{$set_path{$name}}) {
53     if (defined $path && exists $iso{$path}) { delete $iso{$path}; }
54     else { $allp = 0; }
55   }
56   ##if ($allp) { delete $set_path{$name}; }
57 }
58
59 my @iso = sort keys %iso;
60 my $lastbox = "#nil";
61 sub set_box ($) {
62   my ($box) = @_;
63   $box //= "#nil";
64   if ($box ne $lastbox) { print "!box $box\n"; $lastbox = $box; }
65 }
66
67 sub path_key ($) {
68   my ($p) = @_;
69   if ($p =~ m{^ ([a-z]) / ([^/]+) ((?: / .*)?) $}ix) {
70     my $init = $1;
71     my $focus = $2;
72     my $tail = $3;
73     my $head = undef;
74     if ($focus =~ /^ (the | a ) \s+ (\S .*) $/x)
75       { $head = $1; $focus = $2; }
76     if (lc $init ne lc substr($focus, 0, 1) &&
77         $focus =~ /^ (.*) \s+ ($init .*) $/x)
78       { $head .= (defined $head && " ") . $1; $focus = $2; }
79     if (defined $head) { return "$init/$focus, $head$tail"; }
80   }
81   return $p;
82 }
83
84 for my $name (sort { my $pa = $set_path{$a}[0]; my $ba = $box{$pa} // "~~~";
85                      my $pb = $set_path{$b}[0]; my $bb = $box{$pb} // "~~~";
86                      $ba cmp $bb || path_key $pa cmp path_key $pb }
87                 keys %set_path) {
88   my $paths = $set_path{$name};
89   my @unk;
90   set_box $box{$paths->[0]};
91   while (@iso && $iso[0] lt $paths->[0]) { push @unk, shift @iso; }
92   if (@unk) {
93     print "[#UNK: *]\n";
94     for my $path (@unk) { print "\t", $path, "\n"; }
95   }
96   printf "[#%d: %d] %s\n", $set_id{$name}, scalar @$paths, $name;
97   my $i = 0;
98   for my $path (@$paths) {
99     $i++;
100     if (!defined $path) {
101       printf "\t!! (disc %d)\n", $i;
102     } else {
103       my $fn = "$ROOT/$path";
104       set_box $box{$path};
105       if (-f $fn && ! -l $fn) { print "\t" . $path . "\n"; }
106       else { print "\t!! ". $path . "\n"; }
107     }
108   }
109 }
110
111 if (@iso) {
112   print "[#UNK: *]\n";
113   for my $path (@iso) { print "\t", $path, "\n"; }
114 }
115
116 $DB->disconnect;