summaryrefslogtreecommitdiff
path: root/gsv-eval.pl
blob: c575dfb26a6b7d02089730443b6b4fffb610654d (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
#!/usr/bin/env perl
#    Copyright (C) 2014 Assaf Gordon <assafgordon@gmail.com>
#
#    This file 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, either version 3 of the License, or
#    (at your option) any later version.
#
#    This file 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 file. If not, see <http://www.gnu.org/licenses/>.
#
#    Written by Assaf Gordon
#

=pod
This is a hodgepodge of code fragments trying to heuristically detect
copyright and license statements, together with other requirements
for GNU Savannah Hosting.

Official Requirement list:
   https://savannah.gnu.org/register/requirements.php

Unofficial Expansions:
   http://savannah.gnu.org/maintenance/HowToGetYourProjectApprovedQuickly/

Don't use this script directly.
Instead, call 'gsv-eval-local.sh' or 'gsv-eval-remote.sh' .
=cut

use strict;
use warnings;
use Carp;
use Getopt::Long;
use Data::Dumper;
use File::Find ();
use IPC::Open2;
use File::Basename qw/basename/;
use Cwd;
use List::Util qw/max/;
use bytes; # Disable any non-ascii stuff

# The version should be increment for every new feature/improvement.
my $version = "0.1.1";

sub parse_commandline;
sub get_file_list;
sub get_files_mime_type;
sub find_problematic_filenames;
sub find_binary_obj_files;
sub find_binary_data_files;
sub find_common_files;
sub analyze_source_code_files;
sub check_file_copyright;
sub check_file_license;
sub check_file_misuse_linux;
sub check_file_misuse_opensource;
sub check_file_newlines;
sub find_files_missing_both;
sub print_markdown_report;
sub detect_license;

## Global options
my $debug;
my $project_name;
my $input_source;	# directory or file to examine
my $no_file_limit;      # don't limit files to 5000
my $file_limit_warning; # if defined, will be added to the output report
my $vcs_id;
my $vcs_type;
my $vcs_origin;
my @files;	# all files to examine
# Files with potentially problematic charcaters (whitespace, quotes, etc.)
my @problematic_filenames;
my @problematic_newlines;
my %mimetypes;	# hash of filename => mimetype
my %linecount;  # hash of filename => #lines (for text files)
my @binary_obj_files;	# object files, shared-libraires, executables.
my @binary_data_files;	# images,sounds,etc.
my $have_plain_text_license;
my $have_other_license_file;
my $have_readme;
my @small_source_code_files;
my @files_missing_both;
my @files_missing_copyright;
my @files_missing_license;
my %files_shady_copyright;
my %files_shady_license;
my @files_with_linux_term;
my @files_with_opensource_term;
my %copyright_holder; # hash of filename => detected name of copyright holder (if any), or a warning
my %licenses; # hash of filename => (possibly) detected license

# Mimetypes of compiled binary objects
my @mimetypes_bin_obj = qw|
                            application/x-sharedlib
                            application/x-object
                            application/x-archive
                            application/x-executable
                            application/macbinhex40
                            application/x-msdownload
                          |;
my %mimetypes_bin_obj = map { $_ => 1 } @mimetypes_bin_obj;
# NOTE: don't skip 'makefile`, as many packages write it manually
#       (not auto-generated)
my @skip_linux_term_files = qw|
                            configure
                            Makefile.in
                            config.status
                            config.guess
                            config.log
                            config.rpath
                            config.sub
                            ltmain.sh
                            |;
my %skip_linux_term_files = map { $_ => 1 } @skip_linux_term_files;

##
## Program Start
##

parse_commandline();
if (-f $input_source) {
    push @files, $input_source;
} elsif (-d $input_source) {
    chdir $input_source or
	die "failed to change directory to '$input_source': $!\n";
    get_file_list;
} else {
    die "bad source ($input_source), expecting file or directory\n";
}
find_problematic_filenames;
get_files_mime_type;
find_binary_obj_files;
find_binary_data_files;
find_common_files;
analyze_source_code_files;
find_files_missing_both;

if ($debug) {
	print '@files = ', Dumper(\@files),"\n";
	print '@problematic_filenames = ', Dumper(\@problematic_filenames),"\n";
	print '@problematic_newlines = ', Dumper(\@problematic_newlines),"\n";
	print '%mimetypes = ', Dumper(\%mimetypes),"\n";
	print '@binary_obj_files = ', Dumper(\@binary_obj_files),"\n";
	print '@binary_data_files = ', Dumper(\@binary_data_files),"\n";
	print "\$have_plain_text_license = $have_plain_text_license\n";
	print "\$have_other_license_file = $have_other_license_file\n";
	print "\$have_readme = $have_readme\n";
	print '@small_source_code_files = ', Dumper(\@small_source_code_files),"\n";
	print '@files_with_linux_term = ', Dumper(\@files_with_linux_term),"\n";
	print '@files_with_opensource_term = ', Dumper(\@files_with_opensource_term),"\n";
	print '@files_missing_both = ', Dumper(\@files_missing_both), "\n";
	print '@files_missing_copyright = ', Dumper(\@files_missing_copyright), "\n";
	print '@files_missing_license = ', Dumper(\@files_missing_license), "\n";
	print '%files_shady_copyright = ', Dumper(\%files_shady_copyright), "\n";
	print '%files_shady_license = ', Dumper(\%files_shady_license), "\n";
	print '%licenses = ', Dumper(\%licenses),"\n";
}

print_markdown_report;

##
## Program End
##

sub parse_commandline
{
	GetOptions("debug" => \$debug,
		   "project=s" => \$project_name,
                   "no-file-limit" => \$no_file_limit,
		) or die "Error in command line arguments\n";
	my $dir;
	if (scalar(@ARGV)==0) {
		$dir = getcwd;
	} elsif (scalar(@ARGV)==1) {
		$dir = shift @ARGV;
	} else {
		die "extra operand: '" . $ARGV[1] . "'\n";
	}
	$input_source = $dir;
}


=pod
xrun - execute a shell command, ensuring the exit code was zero.
If not zero, terminate with an error message
returns STDOUT of the command, similar to using backticks.

Example:
   instead of:
       my $result = `foo --bar` ;
   do
       my $result = xrun 'foo --bar';
=cut
sub xrun
{
	my $cmd = shift or croak "missing command to execute";
	my $result = `$cmd`;
	my $i = $?;
	die "internal error: failed to execute command '$cmd'\n" if $i == -1;
	if ($i & 127) {
		my $msg =  "cmd '$cmd' died with signal " . ($i & 127). "\n";
		die $msg;
	}
	my $code = $i >> 8;
	die "internal error: command '$cmd' returned exit code $code\n"
		unless $code == 0;
	return $result;
}

# Sets the global @files variable with list of files to examine.
#
# NOTE:
# * If directory contains a ".git" directory, will use 'git ls-files'.
# * If directory contains a ".hg" directory, will use "hg status -C".
# * Otherwise, returns all files in given directory.
# * File names might contain whitespace or other problematic charracters.
sub get_file_list
{
	my $list;
	if ( -d "./.git" ) {
		## A GIT repository
		$list = xrun "git ls-files -z";
                $vcs_type = 'git';
                $vcs_id = xrun 'git describe --always';
                my $first_remote = xrun 'git remote show -n | head -n1';
                $first_remote =~ tr/A-Za-z0-9\-\_//cd;
                my $info = xrun "git remote show -n '$first_remote'";
                ($vcs_origin) = $info =~ / URL: (.+)$/m;
	} elsif ( -d "./.hg" ) {
		## HG repository
		$list = xrun 'hg statuc -C -0';
                $vcs_id = xrun 'hg identify --id';
                $vcs_type = 'hg';
                my $first_path = xrun 'hg paths -q | head -n1';
                $first_path =~ tr/A-Za-z0-9\-\_//cd;
                $vcs_origin = xrun "hg paths  '$first_path'";
        } elsif ( -d "./.svn") {
                ## SVN Repository -
                ##  list recursively, without prompting the user for any password.
                ##  (svn will still try to connect to the remote server
                ## SVN doesn't support null-terminated strings, so fake it.
                $list = xrun 'svn list --recursive --trust-server-cert ' .
                              '--non-interactive --password "" | tr "\n" "\0"';
                my $info = xrun 'svn info';
                $vcs_type = 'svn';
                ($vcs_id) = $info =~ /^Revision: (.+)$/m;
                ($vcs_origin) = $info =~ /^URL: (.+)$/m;

	} else {
		## Last resort - list all files
		$list = xrun "find . -type f -print0";
	}

	@files = split /\x00/, $list;
	@files = map { s|^\./||; $_ } @files ; # remove "./" prefix, if any

        if (scalar(@files)>4000 && !$no_file_limit) {
            @files = @files[0 .. 4000];
            $file_limit_warning = 1;
        }
}

# Sets '@problematic_filenames' based on '@files';
# The filtering is based on strict and minimal charcter set.
# many characters outside this set are valid, but could be potentially
# problematic in different cirumcetences (e.g. '>' in HTML generation,
# single/double quotes for shell-escapting, semi-colons in SQL, etc.)
sub find_problematic_filenames
{
	@problematic_filenames =
		grep { $_ !~ /^[a-zA-Z0-9_\/\.\@\-\+\~]+$/ } @files;
}


# For each file in '@files', check the mimetype (using 'file -i').
# Sets the global '%mimetypes' variable.
#
# NOTE:
#  Uses 'xargs -0' and 'file -i --print0' to avoid shell-escaping problems.
#  This is not POSIX-standard but common enough to be used.
sub get_files_mime_type
{
	# List of files, NUL-separated (for xargs -0).
	my $filelist = join("\x00", @files) . "\x00";

	my ($chld_out, $chld_in);
	my $pid = open2($chld_out,$chld_in,"xargs -n 500 -0 file -i --print0");
	if (fork==0) {
            # Feed list to xargs
            print $chld_in $filelist ;
	    close $chld_in or die "internal error: failed to close chld_in pipe to xrgs: $!";
            exit(0);
        }
        close $chld_in;

	# Get STDOUT from xargs/file -i
	my $mimetype_result ;
        while ($_ = <$chld_out>) {
            $mimetype_result .= $_;
        }
	close $chld_out or die "internal error: failed to close chld_out pipe: $!";
	waitpid ($pid,0);
	my $exit_code = $? >> 8;
	die "Internal error: file -i failed, exit code $exit_code" if $exit_code != 0;

	# Parse resulting list of mime types.
	# The output format of 'file -i --print0' is:
	#    filename<NUL>:<space>mime-type
	foreach ( split /\n/, $mimetype_result ) {
		my ($file, $mimetype) = m/^(.+)\x00:\s+(.+)$/
			or die "internal error: 'file -i' returned unrecognized format: '$_'";
		$mimetype =~ s/; +.+$//; # remove extra information, such as 'charset'
		$mimetypes{$file} = $mimetype;
	}
}


# Based on the %mimetypes, find a list of files which are
# cmpiled object/libraries/binaries.
sub find_binary_obj_files
{
	@binary_obj_files =
		sort
		grep { exists $mimetypes_bin_obj{$mimetypes{$_}} }
			keys %mimetypes;
}

# Based on the %mimetypes, find a list of binary (non object) files.
# The check is based on mime-type NOT starting with "application/" or "text/"
# or "inode/" (inode or commonly 'inode/directory' and 'inode/x-empty').
sub find_binary_data_files
{
	@binary_data_files =
		sort
		grep { $mimetypes{$_} !~ m;^(text|inode)/; &&
                       !(exists $mimetypes_bin_obj{$mimetypes{$_}}) &&
                        # Compiled 'info' (from .texi) files are mis-detected as binary - don't flag them as binary data files.
                       ( ! ( $mimetypes{$_} eq "application/octet-stream"
                           &&
                           $_ =~ m|doc/[a-zA-Z\/\-\.]+\.info$| ) )
                     } keys %mimetypes;
}

# Based on '@files', search for few commonly accepted file names.
sub find_common_files
{
	$have_readme = grep { m/^README(\.(txt|md|rst))?$/i } @files ;
	$have_plain_text_license = grep { m/^(COPYING|LICENSE)$/ } @files;
	$have_other_license_file = grep { m/^(COPYING|LICENSE|GPL|GPL2|GPL3|MIT|BSD|BSD3)(\.txt)$/i } @files;
}



# For each file '@source_code_files' :
#   read the file. If it is 10 or more lines, then:
#   check for copyright, license, and misused terms.
sub analyze_source_code_files
{
	# all in lower case
	my @skip_files = qw/changelog
			readme
			readme.md
			readme.txt
			readme.rst
			authors
			news
			copying
			gpl3.txt
			gpl2.txt
			gpl.txt
			hacking
			hacking.md
			thanks
			todo
			license
			license.txt
                        about-nls
			.gitignore
			.gitattributtes
			.gitmodules
			.prev-version
			/;
	my %skip_files = map { $_ => 1 } @skip_files;

	foreach my $filename (@files) {

		my $type = $mimetypes{$filename};
		if ($type !~ /^text\//) {
			$licenses{$filename} = ["skipped (not-text)"];
			$copyright_holder{$filename} = "skipped (not-text)";
			next;
		}

		my $base = lc(basename($filename));
		if (exists $skip_files{$base}) {
			$licenses{$filename} = ["skipped (non-code)"];
			$copyright_holder{$filename} = "skipped (non-code)";
			next;
		}

		my $fh;
		## Read entire file
		open $fh, "<", $filename or
			die "internal error: failed to open '$filename': $!";
		my @lines = (<$fh>);
		@lines = map { chomp; $_ } @lines;
		close $fh or
			die "internal error: failed to lose '$filename': $!";

		$linecount{$filename} = scalar(@lines);

		check_file_misuse_linux($filename, @lines);

		check_file_misuse_opensource($filename, @lines);

                check_file_newlines($filename, @lines);

		## Skip files shorter than 10 lines
		if (scalar(@lines)<10) {
			$licenses{$filename} = ["skipped (too-short)"];
			$copyright_holder{$filename} = "skipped (too-short)";
			next;
		}

		check_file_copyright($filename, @lines);

		check_file_license($filename, @lines);
	}
}

sub check_file_copyright
{
	my ($filename, @lines) = @_;

	my @found_clear_copyright =
		grep {
	/[Cc]opyright[ \t]+(\([Cc]\))?[ \t]*(19|20)[0-9][0-9][0-9\t ,\-]+[A-Za-z\(< \t]{4}/
	||
	/[Cc]opyright[ \t]+(\([Cc]\))?[ \t]*(19|20)[0-9][0-9][0-9\t ,\-]{16,}+$/
	||
	/[Cc]opyright[ \t]+\@copyright\{\}[ \t]+(19|20)[0-9][0-9]/
		} @lines;

	# Have valid copyright - stop now
	if (@found_clear_copyright) {
                # Extrac the copyright holder's name
                my @holders = map {
                                s/^.*[Cc]opyright //;
                                s/^ *\([Cc]\) *//;
                                $_; } @found_clear_copyright;
                my $holder_text = join (",", @holders);
                if (length($holder_text)>100) {
                    $holder_text = substr($holder_text,0,100) . " [trimmed]";
                }
                $holder_text =~ s/[\r\n]//gs;
                $holder_text =~ s/[^[:print:]]//g;
                $copyright_holder{$filename} = $holder_text;
		return;
	}

        # Not a recognizable copyright statement, try harder
	my @found_copyright = grep { /copyright/i } @lines ;

	# Found the word "copyright" but not a clearly recognizable copyright
	if (@found_copyright) {
                my $text = join(" ", @found_copyright);
                if (length($text)>100) {
                    $text = substr($text,0,100) . " [trimmed]";
                }
                $text =~ s/[\r\n]//sg;
                $text =~ s/[^[:print:]]//g;
		$files_shady_copyright{$filename} = $text;
		$copyright_holder{$filename} = "(possible copyright) $text";
		return;
	}

	# For small-ish files without a copyright, it's acceptable.
	# Put them in a different category
	if (scalar(@lines)<25) {
		$copyright_holder{$filename} = "(no copyright, perhaps too-small?)";
		push @small_source_code_files, $filename;
		return ;
	}

	# Otherwise, warn about this file
	push @files_missing_copyright, $filename;
}

sub detect_license
{
	my ($filename, @lines) = @_;
	# Remove most characters, leaving mostly alphabets and few punctuations.
	my $text = join (' ',
		map {	s/^dnl //; # dnl is the comment prefix in autotools
                        s|^ *//||; # C++ style comments
			s/[^A-Za-z0-9\;\,\:\/ \.\_]+/ /g;
                        $_
			} @lines);
	$text =~ s/  */ /g;

	my $possible_gpl = (
		$text =~ /is free software[:;\.] you can redistribute it and\/or modify it under the terms of the GNU (Lesser|Affero)? *General Public License/i
		||
		$text =~ /is free software\. You may redistribute copies of it under the terms of the GNU General Public License/i
	);
	my $possible_gpl_v2 = $possible_gpl && ($text =~ / version 2[, ]/i);
	my $possible_gpl_v21 = $possible_gpl && ($text =~ / version 2\.1[, ]/i);
	my $possible_gpl_v3 = $possible_gpl && ($text =~ / version 3[, ]/i);
	my $possible_lesser = $possible_gpl && ($text =~ /lesser general public license/i);
	my $possible_affero = $possible_gpl && ($text =~ /affero general public license/i);
	my $possible_or_later = $possible_gpl && ($text =~ /any later version/i);

	my $possible_gpl_final ;
	if ( $possible_affero && $possible_gpl_v3 ) {
		$possible_gpl_final = "AGPLv3";
	} elsif ($possible_affero && $possible_gpl_v2 ) {
		$possible_gpl_final = "AGPLv2";
	} elsif ($possible_lesser && $possible_gpl_v3 ) {
		$possible_gpl_final = "LGPLv3" ;
	} elsif ($possible_lesser && $possible_gpl_v21 ) {
		$possible_gpl_final = "LGPLv21" ;
	} elsif ($possible_lesser && $possible_gpl_v2 ) {
		$possible_gpl_final = "LGPLv2" ;
	} elsif ($possible_gpl_v3) {
		$possible_gpl_final = "GPLv3" ;
	} elsif ($possible_gpl_v2) {
		$possible_gpl_final = "GPLv2" ;
	} elsif ($possible_gpl) {
		$possible_gpl_final = "GPL(unknown type)";
	}
	$possible_gpl_final .= "-or-later" if $possible_or_later;

	## This very permissive license statement is recommended by FSF/GNU
	## for non-core files, such as "Makefile.am"
	my $possible_special_exception = $text =~ /is free software; as a special exception the author gives unlimited permission/;

	## This very permissive license statement appears in some small FSF/GNU files
	## (e.g GNU Hello's ./tests/greeting-1)
	my $possible_gnu_all_permissive = $text =~ /Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved/i;

	my $gnu_fdl = (
		$text =~ /GNU Free Documentation License/
		&&
		$filename =~ /\.texi$/ );

	my $possible_bsd_prefix =
          $text =~ /Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and\/or other materials provided with the distribution/;

	my $possible_bsd_item1 =
	  $text =~ /Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer/;

        my $possible_bsd_item2 =
          $text =~ /Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and\/or other materials provided with the distribution/;

        # The 3rd ('no endorsement') clause in "Clear BSD"
        my $possible_bsd_item3_clear =
          $text =~ /Neither the name of .{4,30} nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission\./;

        # The 3rd ('no endosrsment') clause in 'Modified BSD (3-clause)'
        my $possible_bsd_item3_modified =
          $text =~ /The name of .{4,30} may not be used to endorse or promote products derived from this software without specific prior written permission/;

        my $possible_clearbsd_no_patents =
          $text =~ /PATENT/i;

        my $possible_bsd;
        if ($possible_bsd_prefix
            && $possible_bsd_item1
            && $possible_bsd_item2
            && $possible_bsd_item3_clear
            && $possible_clearbsd_no_patents) {
            $possible_bsd = "Clear-BSD(w/patents!)";
        } elsif ($possible_bsd_prefix
            && $possible_bsd_item1
            && $possible_bsd_item2
            && $possible_bsd_item3_clear
            && !$possible_clearbsd_no_patents) {
            $possible_bsd = "Clear-BSD(wo/patents)";
        } elsif ($possible_bsd_prefix
            && $possible_bsd_item1
            && $possible_bsd_item2
            && $possible_bsd_item3_modified
            && !$possible_clearbsd_no_patents) {
            $possible_bsd = "Modified-BSD(3-clause)";
        } elsif ($possible_bsd_prefix
            && $possible_bsd_item1
            && $possible_bsd_item2
            && !$possible_bsd_item3_clear
            && !$possible_bsd_item3_modified
            && !$possible_clearbsd_no_patents) {
            $possible_bsd = "Free-BSD(2-clause)";
        }


	my $possible_zlib = (
          $text =~ /Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:/ &&
	  $text =~ /The origin of this software must not be misrepresented/ &&
	  $text =~ /Altered source versions must be plainly marked as such/ &&
	  $text =~ /This notice may not be removed or altered from any source distribution/
	  );

	my $possible_x11 = (
	  $text =~ /Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files/ &&
	  $text =~ /The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software/
	  );

	my $possible_perl = $text =~ /This library is free software; you can redistribute it and\/or modify it under the same terms as Perl itself/;

	my $possible_apache2 = (
	$text =~ /Licensed under the Apache License, Version 2.0/
	&&
	$text =~ /you may not use this file except in compliance with the License/
	);

        # This syntax appears in many autotool files (M4/configure/Makefile/Makefile.in).
        # The exact wording has few variations
        my $possible_permissive_autotools = !$possible_gpl_final && (
            $text =~ /This [A-Za-z \.]{4,25} is free software[:;] the Free Software Foundation gives unlimited permission/i
            && (
                $text =~ /the Free Software Foundation gives unlimited permission to copy, distribute and modify it, with or without modifications, as long as this notice is preserved/i
                ||
                $text =~ /the Free Software Foundation gives unlimited permission to copy and\/or distribute it, with or without modifications, as long as this notice is preserved/i
                ||
                # This short form appears in some autogenerated 'configure' files.
                $text =~ /the Free Software Foundation gives unlimited permission to copy, distribute and modify it\./i
               )
            );

        # This syntax appears on some openBSD files
        my $possible_permissive_openbsd =
            $text =~ /Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies/i;

        my $possible_artistic =
            $text =~ /You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the README file\./;

        my $possible_boost_v1_link = (
            $text =~ /Use, modification, and distribution are subject to the Boost Software License, Version 1\.0/i
            &&
            $text =~ m|http://www\.boost\.org/LICENSE_1_0\.txt|
            );

        my $possible_mpl2_link = (
            $text =~ /This Source Code Form is subject to the terms of the Mozilla Public License, v. 2\.0\./i
            &&
            $text =~ m|If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla\.org/MPL/2\.0|
        );

        my @licenses;

	## If any of the above licenses found, stop now.
	push @licenses, $possible_gpl_final if $possible_gpl_final;
	push @licenses, "GFDL" if $gnu_fdl;;
	push @licenses, "special exception" if $possible_special_exception;
	push @licenses, $possible_bsd if $possible_bsd;
	push @licenses, "zlib" if $possible_zlib;
	push @licenses, "X11/MIT" if $possible_x11;
	push @licenses, "Perl" if $possible_perl;
	push @licenses, "Apache2.0" if $possible_apache2;
	push @licenses, "All permissive (autotools)" if $possible_permissive_autotools;
	push @licenses, "All permissive (openbsd)" if $possible_permissive_openbsd;
	push @licenses, "All permissive (GNU)" if $possible_gnu_all_permissive;
        push @licenses, "Artistic/GPL(dual)" if $possible_artistic;
        push @licenses, "Boost-1.0(link)" if $possible_boost_v1_link;
        push @licenses, "MPL-2.0(link)" if $possible_mpl2_link;

        return @licenses;
}

## TODO:
##  in future, check which license each file has, and test for incompatabilities.
##
sub check_file_license
{
	my ($filename, @lines) = @_;

	my $max_line = (scalar(@lines))>40?40:(scalar(@lines)-1);

	my @top_lines = @lines[0 .. $max_line];

        my @detected_license = detect_license($filename, @top_lines);

        if (@detected_license) {
            push @{$licenses{$filename}}, @detected_license;
            return ;
        }

	## If not, look for common phrases the hint at a less-than-standard
	## wording of a license
	my @license_hints = grep {
                /General Public License/i ||
                /and is under GPL/i ||
                /licensed under /i ||
                /public domain/i ||
                /is under the same license as/i ||
                /is distributed under the same license/i ||
                /without any warranty/i } @lines;

	# Found some hints of a license, but not recognizable wording.
	if (@license_hints) {
                my $text = join(" ", @license_hints);
                if (length($text)>100) {
                    $text = substr($text,0,100) . " [trimmed]";
                }
                $text =~ s/[\r\n]//gs;
                $text =~ s/[^[:print:]]//g;
		$files_shady_license{$filename} = $text;
		return ;
	}

	# For small-ish files without a copyright, it's acceptable.
	# Put them in a different category
	if (scalar(@lines)<25) {
		push @small_source_code_files, $filename;
		return ;
	}

	# Otherwise, warn about this file
	push @files_missing_license, $filename;
}

sub check_file_misuse_linux
{
	my ($filename, @lines) = @_;


        # The following files are not checked for 'linux' term,
        # as the usage in them is usually technical and correct
        # (such as host/build/target triplets in all sorts of variations).
        return if $filename =~ /^m4\/[\w\-]+\.m4$/ ||
                  exists $skip_linux_term_files{basename($filename)};

	my @lines_with_linux = grep { /\blinux\b/i } @lines ;

	@lines_with_linux = grep { !(
					/kernel/i ||
					/[a-z]+-linux-[a-z]+/ ||
                                        /\w+-\*linux\*\)/ ||
					/linux[ \-][0-9]\.[0-9]/i ||
					m;/usr/[a-zA-Z0-9\/\_\-]+linux; ||
					m;include[a-zA-Z0-9\/\-]+linux; ||
                                        m;#include +["<]linux/; ||
					/gnu.linux/i
					) } @lines_with_linux;
        @lines_with_linux = map { s/[\r\n]//gs; $_ } @lines_with_linux;
	push @files_with_linux_term, [ $filename, \@lines_with_linux ] if @lines_with_linux;
}

sub check_file_misuse_opensource
{
	my ($filename, @lines) = @_;

	my @lines_with_os = grep { /open.source/i &&
				   !/free and open source software/ &&
				   !/XOPEN_SOURCE/i } @lines ;

        @lines_with_os = map { s/[\r\n]//gs; $_ } @lines_with_os;
	push @files_with_opensource_term, [$filename, \@lines_with_os] if @lines_with_os;
}

sub check_file_newlines
{
	my ($filename, @lines) = @_;
        my $bad_newlines = grep { /\r/ } @lines;
        if ($bad_newlines) {
            push @problematic_newlines, $filename;
        }
}


## File files which are missing both copyright and license, and extract
## them to a new list ( @files_missing_both ).
sub find_files_missing_both
{
    my %mis_copyr = map { $_ => 1 } @files_missing_copyright;

    my %mis_both = map { $_ => 1 }
                    grep { exists $mis_copyr{$_} }
                        @files_missing_license;

    @files_missing_copyright = grep { !(exists $mis_both{$_}) } @files_missing_copyright;
    @files_missing_license = grep { !(exists $mis_both{$_}) } @files_missing_license;

    @files_missing_both = sort keys %mis_both;
}



sub print_markdown_report
{
	print "# GNU Savannah Evaluation" ;
	print " for $project_name" if $project_name;
	print "\n\n";

	print "Generated on ", "" . gmtime, "  \n";
	print "Source: \`$input_source\`  \n";
        print "Detection script version: $version  \n";
        if ($vcs_type) {
            print<<"EOF";
$vcs_type URL: \`$vcs_origin\`  
$vcs_type revision: \`$vcs_id\`  
EOF
        }
        if ($file_limit_warning) {
            print "**File Limit WARNING:** directory contained too many files. Only the first 5000 files were analyzed.\n";
        }

        print<<'EOF';

> NOTE:  
> This auto-generated report is in alpha-stage, and may contain incorrectly detected items.  
> Information here should be considered as informal suggestions and guidelines.  
> The official GNU Savannah Hosting Requirements can be found here:
> <https://savannah.gnu.org/register/requirements.php>

EOF

	if (@files_missing_both) {
		print<<'EOF';

## Files without Copyright and License statements

The following files are missing *both* copyright and license statement.

A copyright statement should be of form:  
  `Copyright YEARS NAME`  
An optional "(C)" and/or contact addresses can be added if you wish, such as:  
  `Copyright (C) YEARS NAME (email)`

1. If these are source code files for your project, consider using the standard
license text at the beginning of your file. For GNU licenses, See
[How to GNU licenses for your own software](http://www.gnu.org/licenses/gpl-howto.html)
2. If these are supporting/auxiliary files (such as `Makefile`, `ini` files),
consider adding the [GNU All Permissive License](http://www.gnu.org/prep/maintain/html_node/License-Notices-for-Other-Files.html).

Projects hosted on GNU Savannah must carry a copyright and license statements.

> NOTE:  
> If these files contain a legally acceptable copyright/license statement, they are
> acceptable for hosting on GNU Savannah (even if this auto-generated report failed
> to detect it).  
> Revising is not a strict requirement, but is highly recommended.  
> A recognizable copyright and license statements will speed-up project approval on GNU Savannah.

EOF
		print join("", map { "    $_\n" } @files_missing_both),"\n";
        }

	if (@files_missing_copyright) {
		print<<'EOF';
## Files without Copyright statement

A recognizable copyright statement was not detected in the following files.  
A copyright statement should be of form:  
  `Copyright YEARS NAME`  
An optional "(C)" and/or contact addresses can be added if you wish, such as:  
  `Copyright (C) YEARS NAME (email)`

Projects hosted on GNU Savannah must carry a copyright statement.

For more information, see [How to Apply GPL License to your source code](http://www.gnu.org/licenses/gpl-howto.html).

> NOTE:  
> If these files contain a legally acceptable copyright statement, they are
> acceptable for hosting on GNU Savannah (even if this auto-generated report failed
> to detect it).  
> Revising is not a strict requirement, but is highly recommended.  
> A recognizable copyright statement will speed-up project approval on GNU Savannah.

EOF
		print join("", map { "    $_\n" } @files_missing_copyright),"\n";
	}

	if (%files_shady_copyright) {
		print<<'EOF';
## Files without canonical copyright statement

The following files possibly contain a copyright statement in non-canonical form.  
A recognizable copyright statement in this script should be of form:  
  `Copyright (C) YEAR NAME (email)`  
It is possible these files contain a valid yet unrecognizable form of the
copyright statement (usually due to different formatting).

Please consider revising the copyright statement, to make it easy for future
users and contributers of your project.

> NOTE:  
> If these files contain a legally acceptable copyright statement, they are
> acceptable for hosting on GNU Savannah (even if this auto-generated report failed
> to detect it).  
> Revising is not a strict requirement, but is highly recommended.  
> A recognizable copyright statement will speed-up project approval on GNU Savannah.

EOF
		print join("",
                        map { "    $_: " . $files_shady_copyright{$_} . "\n" }
                            sort
                            keys %files_shady_copyright)
                      ,"\n";
	}

	if (@files_missing_license) {
		print<<'EOF';
## Files without License Statement

A license statement was not detected in the following files.

It is possible these files contain a valid yet unrecognizable form of the
license statement (usually due to different formatting).

1. If these are source code files for your project, consider using the standard
license text at the beginning of your file. For GNU licenses, See
[How to GNU licenses for your own software](http://www.gnu.org/licenses/gpl-howto.html)
2. If these are supporting/auxiliary files (such as `Makefile`, `ini` files),
consider adding the [GNU All Permissive License](http://www.gnu.org/prep/maintain/html_node/License-Notices-for-Other-Files.html).

Projects hosted on GNU Savannah must carry a license statement.

> NOTE:  
> If these files contain any legally acceptable license statement, they are
> acceptable for hosting on GNU Savannah (even if this auto-generated report
> failed to detect it).  
> Revising is not a strict requirement, but is highly recommended.  
> Using a standard license statement will speed-up project approval on GNU Savannah.

EOF
		print join("", map { "    $_\n" } @files_missing_license),"\n";
	}

	if (%files_shady_license) {
		print<<'EOF';
## Files without canonical license statement

The following files contains statement that is possibly a valid license,
but is not the caninocal statement text. For example:  
`This file is part of PACKAGE and is under the same license as PACKAGE`  
While this is legally a valid statement, it is better
to include one of the canonical full license statements.

Another common reason for mis-detection is putting the license statement at the
bottom of the file (it is recommended to put the license statement at the **top
of the source code file**, right after the copyright statement).

Projects hosted on GNU Savannah must carry a license statement.

Please consider revising the license statement, to make it easy for future
users and contributers of your project.

1. If these are source code files for your project, consider using the standard
license text at the beginning of your file. For GNU licenses, See
[How to GNU licenses for your own software](http://www.gnu.org/licenses/gpl-howto.html)
2. If these are supporting/auxiliary files (such as `Makefile`, `ini` files),
consider adding the [GNU All Permissive License](http://www.gnu.org/prep/maintain/html_node/License-Notices-for-Other-Files.html).

> NOTE:  
> If these files contain any legally acceptable license statement, they are
> acceptable for hosting on GNU Savannah (even if this auto-generated report
> failed to detect it).  
> Revising is not a strict requirement, but is highly recommended.  
> Using a standard license statement will speed-up project approval on GNU Savannah.

EOF
		print join("",
                        map { "    $_: " . $files_shady_license{$_} . "\n" }
                            sort
                            keys %files_shady_license)
                      ,"\n";
	}

	## Ugly hack: small files might be listed twice (added by both copyright-check
	## and license-check) - so remove duplicates
	my %tmp = map { $_ => 1 } @small_source_code_files;
	@small_source_code_files = sort keys %tmp;
	if (@small_source_code_files) {
		print<<'EOF';
## Small Source Code files without Copyright/License

The following small files were missing a copyright or license statement.
Because these files are small (25 lines or less), it is possible that such a statement
is not required, or a mention in the `README` file is sufficient.

Please review the list.  
If these are source code files, please add a valid license and copyright statement.  
If these are auxiliary files (such as `Makefile`s), consider adding the short
[GNU All Permissive License](http://www.gnu.org/prep/maintain/html_node/License-Notices-for-Other-Files.html).

EOF
		print join("", map { "    $_\n" } @small_source_code_files),"\n";
	}

	if (@files_with_linux_term) {
		print<<'EOF';
## Possible misue of *Linux* vs *GNU/Linux*

The following files use the term *Linux*.

*Linux* is a Kernel, not a whole operating system.
Please consider using the correct term **GNU/Linux** if the context is
operating system and not Kernel-specific topic.

For more information, see
 [Why GNU/Linux](http://www.gnu.org/gnu/why-gnu-linux.html),
 [Linux and GNU](http://www.gnu.org/gnu/linux-and-gnu.html), and
 [Words to Avoid](https://www.gnu.org/philosophy/words-to-avoid.html).

EOF
		foreach my $ref (@files_with_linux_term) {
			my ($file, $lines_ref) = @$ref;
			print join('', map{ "    " . $file . ": ". $_ . "\n" } @$lines_ref);
		}
		print "\n";
	}

	if (@files_with_opensource_term) {
		print<<'EOF';
## Possible misue of *Open Source* vs *Free Software*

The following files use the term *Open Source*.

Please use **Free Software** to refer to software which respects [Your Freedom](http://www.gnu.org/philosophy/free-sw.html).

For more information, see
 [Why 'Open Source' misses the point of Free Software](http://www.gnu.org/philosophy/open-source-misses-the-point.html), and
 [Words to Avoid](https://www.gnu.org/philosophy/words-to-avoid.html).

EOF
		foreach my $ref (@files_with_opensource_term) {
			my ($file, $lines_ref) = @$ref;
			print join('', map{ "    " . $file . ": ". $_ . "\n" } @$lines_ref);
		}
		print "\n";
	}

	if (@problematic_filenames) {
		print<<'EOF';
## Problematic Filenames

The following file names contains potentially problematic characters
(such as space, tab, single and double quotes, or other non-printable characters).  
Such files tend to cause problems on many computer systems, and are best avoided.

> NOTE:  
> Renaming these files is not a strict requirement, but is highly recommended.

EOF
		print join("", map { "    $_\n" } @problematic_filenames),"\n";
	}

	if (@problematic_newlines) {
		print<<'EOF';
## Problematic New-Line characters

The following file names contains potentially problematic new-line characters/
(such as CR+LF or just CR).
Such files tend to cause problems on many computer systems, and are best avoided.
Standard Unix files should use LF (\\n) as a new-line character.

> NOTE:  
> Revising these files is not a strict requirement, but is highly recommended.

EOF
		print join("", map { "    $_\n" } @problematic_newlines),"\n";
	}

	if (@binary_obj_files) {
		print<<'EOF';
## Compiled Binary Files

The following files binary application files
(compiled objects, shared-libraries, executables, etc.).  
Such files are usually created when compiling
the source code of your package, and there's no need to include them in your package.

If the compiled files are not created from the source code of your project,
they **can not** be hosted on GNU Savannah (GNU Savanna can only host binaries
for which a complete and Free source code is available).

In either case, it is best not to include compiled binaries in your project submission.

> NOTE:  
> Removing these files is not a strict requirement, but is highly recommended.  
> Keeping compiled binary files in your project might delay the GNU Savannah evaluation.  

EOF
		print join("", map { "    $_\n" } @binary_obj_files),"\n";
	}

	if (@binary_data_files) {
		print<<'EOF';
## Binary Data Files

The following files are binary data files (not textual source code files).

Some binary formats allow adding copyright and license information inside the
file. For others, it is recommended to specify the copyright and license information
in the `README` file.

1. If you authored these files, please add copyright and license information
in each file, or mention them in the `README` file.
2. If you obtained these files from another source, **you must verify** and state
their copyright and license information.
3. Often times files that are free to download (as in 'no cost') are not Free to use
(as in respect your freedom to usage, modify and distribute the files). Project
hosted on GNU Savannah **must not** include non-free files.

> NOTE:  
> This auto-generated report cannot detect if the README already mention these files.  
> If all data files in your project are properly copyrighted and licenses, please
> disregard this item.

Projects hosted on GNU Savannah must have a valid copyright and statement information
for all data files.

EOF
		print join("", map { "    $_\n" } @binary_data_files),"\n";
	}

	if (scalar(@files)>1 && !$have_plain_text_license) {
		print<<'EOF';
## Missing plain-text license file

Please include a plain-text license file,
named either `COPYING` (for GPL-license) or `LICENSE` (for other licenses, such as BSD/MIT).

These are the commonly used file names, and using them will help future users and contributors
for your project. If your project uses a non-standard name
(e.g. `license.gpl3` or `bsd.txt`, please consider renaming these files).

> NOTE:  
> This is not a strict requirement, but is highly recommended.  
> Using a standard file name will speed-up project approval on GNU Savannah.

EOF
	}

	if (scalar(@files)>1 && !$have_readme) {
		print<<'EOF';
## Missing plain-text README file

Please include a plain-text `README` file (or `README.TXT`, `README.md`, `README.rst`).

These are the commonly used file names, and using the will help future users and contributors
for your project.  If your project uses a non-standard name
(e.g. `info.txt` or `read-me-now`, please consider renaming these files).

> NOTE:  
> This is not a strict requirement, but highly recommended.  
> Using a standard file name will speed-up project approval on GNU Savannah.

EOF
	}

        ## Find width of each field, for proper alignment in textmode markdown.
        my $filename_width = 0;
        my $mimetype_width = 0 ;
        my $license_width = 0;
        my $copyright_holder_width = 0 ;
        my $lines_width = 0 ;
	foreach my $filename (@files) {
		my $mimetype = $mimetypes{$filename} || "";
		my $lines = $linecount{$filename} || "";
		my $copyright_holder = $copyright_holder{$filename} || "";
		my $license_ref = $licenses{$filename};
		my $license = ($license_ref)?(join(",",@$license_ref)):"";

                # Prevent underscore from messing-up Markdown rendering
                my $md_filename = $filename;
                $md_filename =~ s/_/\\_/g ;

                $filename_width = max($filename_width, length($md_filename));
                $mimetype_width = max($mimetype_width, length($mimetype));
                $lines_width = max($lines_width, length($lines));
                $copyright_holder_width = max($copyright_holder_width, length($copyright_holder));
                $license_width = max($license_width, length($license));
        }

        ## Summarize stats of licenses
        # Count number of files for each license
        my %lic_counts ;
        foreach my $lic_ref ( values %licenses ) {
            # Each file has a list of detected licenses
            my $text = join(",", @$lic_ref );
            $lic_counts{$text}++;
        }

        # Print Markdown-compatible text table
        my $lic_not_detected = "no license detected";
        my $lic_width = max ( length($lic_not_detected),
                              map { length($_) } keys %lic_counts );
        my $lic_format_string = "%-${lic_width}s|%s\n";
        print "## Summary of Licenses\n\n";
        printf $lic_format_string, "License Type", "no. files";
        printf $lic_format_string, '-' x $lic_width, "--------" ;
        foreach my $lic ( sort keys %lic_counts ) {
            printf $lic_format_string, $lic, $lic_counts{$lic};
        }
        my $lic_missing_count = scalar(@files) - scalar(keys %licenses);
        printf $lic_format_string, $lic_not_detected, $lic_missing_count;
        print "\n\n";

	## List all files
	print<<'EOF';
## Summary of Analyzed Files

EOF

        my $format_string = sprintf "%%-%ds|%%-%ds|%%-%ds|%%-%ds|%%s\n",
                                $filename_width, $mimetype_width, $lines_width,
                                $license_width; #, $copyright_holder_width;

        ## Table header
        printf $format_string, "File", "Type", "no. lines", "Detected License", "Detected Copyright";
        ## Markdown-compatble table header line
        printf $format_string, "-" x $filename_width,
                                "-" x $mimetype_width,
                                "-" x ($lines_width-1) . ":",
                                "-" x $license_width,
                                "-" x $copyright_holder_width;


#File | type     | Detected License               | Detected Copyright | no. lines
#:----|:---------|:-------------------------------|-------------------:|----------:
#EOF
	foreach my $filename (@files) {
		my $mimetype = $mimetypes{$filename} || "";
		my $lines = $linecount{$filename} || "";
		my $copyright_holder = $copyright_holder{$filename} || "";
		my $license_ref = $licenses{$filename};
		my $license = ($license_ref)?(join(",",@$license_ref)):"";

                # Prevent underscore from messing-up Markdown rendering
                my $md_filename = $filename;
                $md_filename =~ s/_/\\_/g ;

                $copyright_holder = quotemeta $copyright_holder;

		printf $format_string, $md_filename, $mimetype, $lines,
                        $license, $copyright_holder;
	}

	print "\n\n## END OF EVALUATION REPORT\n\n"
}