Initial commit.
[dvddb] / report
CommitLineData
14acb11f
MW
1#! /usr/bin/perl -w
2
3use open ":utf8";
4use strict;
5
6use DBI;
7use Encode qw{encode_utf8 decode_utf8};
8use File::Find;
9
10BEGIN { binmode STDOUT, ":utf8"; }
11
12my $ROOT = "/mnt/dvd/archive";
13my $DB = DBI->connect("dbi:Pg:host=roadstar", "", "",
14 { AutoCommit => 0,
15 RaiseError => 1,
16 ReadOnly => 1 });
17
18my %iso = ();
19find(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
27my %set_path;
28my %set_id;
29my $st_set = $DB->prepare("SELECT id, name, ndisc FROM dvd_set");
30my $st_disc = $DB->prepare("SELECT disc, path FROM dvd_disc
31 WHERE set_id = ?
32 ORDER BY disc");
33$st_set->execute;
34
35SET: 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
51for 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
60my @iso = sort keys %iso;
61for 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
84if (@iso) {
85 print "[#UNK: *]\n";
86 for my $path (@iso) { print "\t", $path, "\n"; }
87}
88
89$DB->disconnect;