1#!/bin/env ruby
2#
3# Source Code License Guesser.
4# Copyright, 2017 Alexander von Gluck IV. All Rights Reserved
5# Released under the terms of the MIT license.
6#
7# Give a file, and I guess the license.
8#
9# Example Usage:
10# 	
11# 	haiku $ find src -name "*.cpp" -exec ./3rdparty/kallisti5/licenseReport.rb {} \;
12#
13
14@file = ARGV.first
15@licenses = [
16	{"MIT" => ["MIT License", "MIT Licence", "Haiku License", "X11 license"]},
17	{"BSD" => ["express or implied warranties", "provided by the author ``AS IS''", "the software is provided \"AS IS\"", "BSD license", "provided by the author \"as is\""]},
18	{"BeOS Sample Code" => ["be sample code license"]},
19	{"LGPL" => ["GNU Lesser", "GNU L-GPL license"]},
20	{"GPL" => ["terms of the GNU General Public License", "GPL license", "Free Software Foundation"]},
21]
22
23def check_license(filename)
24	license = "unknown"
25	lines = File.foreach(filename).first(30).join("\n")
26	return "empty file" if lines == nil
27	@licenses.each do |entry|
28		entry.values.first.each do |pattern|
29			if lines.downcase.include?(pattern.downcase)
30				license = entry.keys.first
31				break
32			end
33		end
34		break if license != "unknown"
35	end
36	license
37end
38
39puts "#{@file}: #{check_license(@file)}"
40