#!/usr/bin/perl -w

use strict;

use File::Basename;
use Data::Dumper;

my @sections = (1, 2, 3, 4, 5, 6, 7, 8);

my $verbose = 0;

my %copyrights;

foreach my $s (@sections) {
   print "Processing man$s...\n" if $verbose;
   open FOUT, "man$s.copyright" or die "Can't write to man$s.copyright";
   foreach my $f (<manpages/man$s/*>) {
      next unless -f $f;

      open MANP, $f or die "Can't open $f!";
      my $currentcopyright = "";
      while (<MANP>) {
         last if !/\.\\"/;
         last if /\.\\" Modified/;
         last if /\.\\"199/;      # Changelog years
         last if /\.\\"200/;
         last if /\.\\"\s*@/;     # This kind of mark appears in some manpages
         $_ = "" if /\$Id/;
         $_ = "" if /-\*-/o;
         s/\.\\" ?//o;
         $currentcopyright .= $_;
      }
      close MANP;

      # Massage copyright a little
      $currentcopyright =~ s/^\n+//go;
      $currentcopyright =~ s/\n+$//go;

      # Ignore files with "blank copyright notices"
      next if $currentcopyright =~ /^\s*$/;

      $copyrights{$currentcopyright} = []
            unless exists $copyrights{$currentcopyright};
      push @{$copyrights{$currentcopyright}}, basename $f;
   }
   close FOUT;
}

foreach my $c (keys %copyrights) {
   print STDERR scalar @{$copyrights{$c}}, "\n";  # Copyright notice frequency
   print $c, "\n\n";
   print "Manpages: ", join(", ", @{$copyrights{$c}}), "\n\n\n";
   print "-------------------------------------------------------------\n";
}
