#! /usr/bin/perl

#   Copyright (c) 2003,4 Martin Schulze <joey@infodrom.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 dated June, 1991.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.

use strict;
use Text::Wrap;

my %config = (
	      'template' => 'debian/copyright',
	      'debian' => 'debian',
	      'width' => 72,
	      );

# regular expressions that intend to identify a particular license
#
my %licenses = (
		'BSD' => '(Redistributions of source code must retain the above copyright|BSD copyright)',
#btowc
		'Linux1' => 'Formatted or processed versions of this manual, if unaccompanied',
		'Linux2' => 'Permission is granted to copy and distribute (translations|modified versions) of this',
		'GPLv2' => 'Free Software Foundation; (either )?version 2',
#		'GPLv2' => '(Free Software Foundation; (either )?version 2|Gnu Public License, Version 2)',
		'GPL' => '(([Dd]istributed|Licensed) under (the )?(GPL|the GNU General Public License)|terms of the GNU General|according to the GNU General Public License)',
		'PD' => 'This page is in the public domain',
		'MIT' => 'Permission is hereby granted, free of charge, to any person',
		'ak' => 'Permission is granted to distribute possibly modified copies',
		);

my %exception = (
		 'ioctl_list.2' => 'GPLv2',
		 'clone.2' => 'GPL',
		 'capget.2' => 'GPL',
		 'netlink.3' => 'GPL',
		 'nfsservctl.2' => 'PD',
		 'stdin.3' => 'PD',
		 'boot.7' => 'GPL',
		 'hosts.equiv.5' => 'GPL',
		 '__setfpucw.3' => 'GPL',
		 'bindresvport.3' => 'BSD',
		 'futex.2' => 'PD',
		 'futex.7' => 'MIT',
		 'getrpcent.3' => 'BSD',
		 'getrpcport.3' => 'BSD',
		 'ld.so.8' => 'PD',
		 'pivot_root.2' => 'GPL',
		 'rpc.3' => 'BSD',
		 'rpc.5' => 'BSD',
		 'xdr.3' => 'BSD',
		 'netlink.7' => 'GPL',
		 'tzfile.5' => 'PD',
		 'wavelan.4' => 'nolicense',
		 'sync.8' => 'GPL',
		 'fcloseall.3' => 'Linux1',
		 'fallocate.2' => 'GPLv2',
		 'getdtablesize.2' => 'Linux2',
		 'gai.conf.5' => 'GPLv2',
		 'nss.5' => 'GPLv2',
		 'cciss.4' => 'GPLv2',
		 'hpsa.4' => 'GPLv2'
		 );

my %licensetext = (
		   'getitimer.2' => 'May be freely distributed',
		   'getpt.3' => 'Redistribute and modify at will.',
		   'pts.4' => 'Redistribute and revise at will.',
		   'pciconfig_read.2' => 'May be freely distributed.',
		   'sysinfo.2' => 'Permission is granted to freely distribute or modify this file
for the purpose of improving Linux or its documentation efforts.
If you modify this file, please put a date stamp and HOW you
changed this file.  Thanks.',
		   'dir_colors.5' => 'This file may be copied under the conditions described
in the LDP GENERAL PUBLIC LICENSE, Version 1, September 1998
that should have been distributed together with this file.',
		   'mailaddr.7' => 'Redistribution and use in source and binary forms are permitted
provided that the above copyright notice and this paragraph are
duplicated in all such forms and that any documentation,
advertising materials, and other materials related to such
distribution and use acknowledge that the software was developed
by the University of California, Berkeley.  The name of the
University may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS\'\' AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.',
		   'regex.7' => 'This software is not subject to any license of the American Telephone
and Telegraph Company or of the Regents of the University of California.
Permission is granted to anyone to use this software for any purpose
on any computer system, and to alter it and redistribute it, subject
to the following restrictions:
1. The author is not responsible for the consequences of use of this
   software, no matter how awful, even if they arise from flaws in it.
2. The origin of this software must not be misrepresented, either by
   explicit claim or by omission.  Since few users ever read sources,
   credits must appear in the documentation.
3. Altered versions must be plainly marked as such, and must not be
   misrepresented as being the original software.  Since few users
   ever read sources, credits must appear in the documentation.
4. This notice may not be removed or altered.',
		 'resolv.conf.5' => 'Redistribution and use in source and binary forms are permitted
provided that the above copyright notice and this paragraph are
duplicated in all such forms and that any documentation,
advertising materials, and other materials related to such
distribution and use acknowledge that the software was developed
by the University of California, Berkeley.  The name of the
University may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS\'\' AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.',
		   );

# hash of arrays with each element being a manpage
#
my %manpages = ();

die "E: make-copyright root-dir output-file-rel" if ($#ARGV lt 1);

my $dir = $ARGV[0];
my $output = $ARGV[1];

opendir(D, $dir) || die "Cannot open $dir";
my @files = sort (grep (!/^\.\.?.*$/,readdir(D)));
closedir(D);

my $blurb = '';
my $license;
my $l;
my $c = 0;
my @unknown;
foreach my $file (@files) {
    if (open (F, $dir."/".$file)) {
	$blurb =  join ("", <F>);
	close (F);
	$license = '';
	if (exists $exception{$file}) {
	    $license = $exception{$file};
	} elsif (!exists $licensetext{$file}) {
	    $license = '';
	    foreach $l (keys %licenses) {
		if ($blurb =~ /$licenses{$l}/) {
		    $license = $l;
		    last;
		}
	    }
	}
	if ($license) {
	    $file =~ /(.*)\.(\d)/;
	    push (@{$manpages{$license}}, "$1($2)");
	} elsif (!exists $licensetext{$file}) {
	    printf STDERR "%s: Unknown license (%d)\n", $file, ++$c;
	    push (@unknown, $file);
	}
    }
}

$Text::Wrap::columns = $config{width};
open (OUT, ">$output") || die "Cannot open $output for writing.";

if (-r $config{template}) {
    if (open (F, $config{template})) {
	print OUT while (<F>);
	close (F);
    }
}
print OUT "\n";

foreach $l (keys %licenses) {
    if (exists $manpages{$l}) {
	print OUT "=" x $config{width} . "\n\n";
	print OUT "The following license covers these manpages:\n\n";

	my $foo = $manpages{$l};
	my @foo = @$foo;
	printf OUT "%s\n\n", Text::Wrap::fill ('   ', '   ', join (", ", sort (@foo)));

	if (open (F, "$config{debian}/license.$l")) {
	    print OUT while (<F>);
	    close (F);
	    print OUT "\n";
	}
    }
}

foreach $l (keys %licensetext) {
    print OUT "=" x $config{width} . "\n\n";
    print OUT "The following license covers these manpages:\n\n";
    $l =~ /(.*)\.(\d)/;
    printf OUT "   %s(%d)\n\n", $1, $2;
    printf OUT "%s\n\n", $licensetext{$l};
}

close (OUT);

if ($#unknown > -1) {
    printf STDERR "Unknown licenses found for %s\n", join (", ", @unknown);
    exit (1);
}