c7baf0062d13a0cd518f5542cb346522316f71f0
[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 $st_set = $DB->prepare("SELECT id, name, ndisc FROM dvd_set");
30 my $st_disc = $DB->prepare("SELECT disc, path FROM dvd_disc
31 WHERE set_id = ?
32 ORDER BY disc");
33 $st_set->execute;
34
35 SET: for (;;) {
36 my @r = $st_set->fetchrow_array; last SET unless @r;
37 my ($id, $name, $ndisc) = @r;
38 my @path;
39
40 $st_disc->execute($id);
41 DISC: for (;;) {
42 my @r = $st_disc->fetchrow_array; last DISC unless @r;
43 my ($disc, $path) = @r;
44 $disc == @path or die "bad disc sequence for `$name'";
45 push @path, $path;
46 }
47 @path == $ndisc or die "wrong number of discs for `$name'";
48 $set_path{$name} = \@path; $set_id{$name} = $id;
49 }
50
51 for my $name (keys %set_path) {
52 my $allp = 1;
53 for my $path (@{$set_path{$name}}) {
54 if (defined $path && exists $iso{$path}) { delete $iso{$path}; }
55 else { $allp = 0; }
56 }
57 ##if ($allp) { delete $set_path{$name}; }
58 }
59
60 my @iso = sort keys %iso;
61 for my $name (sort { $set_path{$a}[0] cmp $set_path{$b}[0] }
62 keys %set_path) {
63 my $paths = $set_path{$name};
64 my @unk;
65 while (@iso && $iso[0] lt $paths->[0]) { push @unk, shift @iso; }
66 if (@unk) {
67 print "[#UNK: *]\n";
68 for my $path (@unk) { print "\t", $path, "\n"; }
69 }
70 printf "[#%d: %d] %s\n", $set_id{$name}, scalar @$paths, $name;
71 my $i = 0;
72 for my $path (@$paths) {
73 $i++;
74 if (!defined $path) {
75 printf "\t!! (disc %d)\n", $i;
76 } else {
77 my $fn = "$ROOT/$path";
78 if (-f $fn && ! -l $fn) { print "\t" . $path . "\n"; }
79 else { print "\t!! ". $path . "\n"; }
80 }
81 }
82 }
83
84 if (@iso) {
85 print "[#UNK: *]\n";
86 for my $path (@iso) { print "\t", $path, "\n"; }
87 }
88
89 $DB->disconnect;