File: check-titles

package info (click to toggle)
chronicle 4.4-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 656 kB
  • ctags: 108
  • sloc: perl: 2,323; makefile: 132; sh: 9
file content (96 lines) | stat: -rwxr-xr-x 1,540 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/perl -w
#
#  A simple utility which will process each "*.txt" file inside a named
# directory.
#
#  Any entries with a duplicate title will be alerted.
#
# Steve
#

use strict;
use warnings;


#
#  Get the directory and ensure it exists.
#
my $dir = shift;
die "Usage: $0 directory" unless ( defined($dir) );
die "Not a directory : $dir" unless ( -d $dir );


#
#  The hash of arrays
#
my $titles;


#
#  Find the files.
#
foreach my $file ( sort( glob( $dir . "/*" ) ) )
{

    #
    #  Skip non-files
    #
    next if ( -d $file );
    next if ( $file =~ /~$/ );

    #
    #  Title for this entry.
    #
    my $title = undef;

    #
    #  Read each file, and look for the title.
    #
    open my $handle, "<", $file
      or die "Failed to read $file - $!";
    foreach my $line (<$handle>)
    {
        $title = $2
          if ( !defined($title)
               && ( $line =~ /^(Subject|Title):(.*)/i ) );

    }
    close($handle);

    #
    #  Ignore entries with no title.
    #
    next if ( !$title );

    #
    #  Get current entries we might have found with
    # this title; and add on the new one.
    #
    my $a = $titles->{ $title };
    push( @$a, $file );
    $titles->{ $title } = $a;
}


#
#  Now look for dupes
#
my $fail = 0;
foreach my $title ( keys %$titles )
{
    my $a = $titles->{ $title };

    if ( ( scalar @$a ) > 1 )
    {
        print "Title: $title\n";
        foreach my $file (@$a)
        {
            print "\t" . $file . "\n";
        }

        $fail = 1;
    }
}


exit($fail);