#!/usr/bin/env ruby

def classify_link(url)
	
	host = ''
	path  = ''
	prot = ''
	
	
	if (url =~ /mailto:/i)
		return "mail"
	end
	
	if (url[0,1] == "/" and url[1,1] != "/")
		return "internal"
	end
	
	if (url =~ /^([^\:]+):*\/\/([^\/]+)(\/*.*)/i)
		prot = $1
		host = $2
		path = $3
	else
		p url
	end
	
	
	if (host =~ /(microsoft|msn|live)\.(net|com)/i)
		return "microsoft"
	end
	
	if (host !~ /\./)
		return "internal"
	end
	
	return "normal"
	
end

inp = ARGV.shift() || exit(0)
out = ARGV.shift() || exit(0)

html = File.open(out, 'w')
html << %Q{
	<html>
		<head>
			<title>Microsoft FWLINK Redirect Mapping</title>
		
			<style type="text/css">
				.normal {
					background: white;
					border-bottom: 1px solid black;
				}	
				.internal {
					background: yellow;
					border-bottom: 1px solid black;
				}
				.microsoft {
					background: gray;
					border-bottom: 1px solid black;
				}
				.mail {
					background: green;
					border-bottom: 1px solid black;
				}
			</style>
			
		</head>
		<body>
		<table width="100%" cellspacing=0 cellpadding=6>
}

list = File.open(inp, "r")
list.each_line do |line|

	flink, plink = line.strip.split(/\t/)
	next if not plink
	
	link_type = classify_link(plink)
	link_id   = 'ERROR'
	
	if(flink =~ /LinkId=(\d+)/)
		link_id = $1.to_i.to_s
	end
	
	next if link_type == "microsoft"

	html << "<tr class='#{link_type}'>"
	html << "<td align='right'><a href='#{flink}'>#{link_id}</td>"
	html << "<td><a href='#{plink}'>#{plink}</td>"
	html << "</tr>\n"
	

end

html << %Q{
		</table>
		</body>
	</html>
}

