# This work is licensed under the Creative Commons
# Attribution-ShareAlike 3.0 Unported License. To view a copy of this
# license, visit or send
# a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042,
# USA. 
# Reference:
# (C) Robert Tolksdorf,
use List::Util qw(max);
use URI::Escape;
use Data::Dumper;
require "/vhome/";

# generate several external search queries
sub info_links {
    $nonblankkey=~s/ /_/g;
    return "Search info at <a href=\"$safekey\">Wikipedia</a>, ".
	"<a href=\"$safenonblankkey\">dbpedia</a>, ".
	"<a href=\"$safekey%22&sortKeys=holdingscount&recordSchema=BriefVIAF\"></a>, ".
	"<a href=\"$safekey\">Open Corporates Companies</a>, ".
	"<a href=\"$safekey\">Open Corporates Officers</a>, ".
	"<a href=\"$safekey\">Google</a>, ".
	"images at <a href=\"$safenonblankkey\">Wikimedia Commons images</a>, ".
	"<a href=\"$safekey&tbm=isch\">Google images</a>\n";

# load data

# Licence texts
# HTML stuff
# merge aliased entries
foreach $key ( keys %alias) {
    $cast{$alias{$key}}.=" | ".$cast{$key};
    delete $cast{$key};

# next possible id (they are not neccessarily continuously used...
$nextid = (max values %actorid) +1;
# where to store info about an actor

# Generate index.html in respective directory
foreach $key ( keys %cast) {
    # if it is a new actor, we take the next free id
    if (!(exists $actorid{$key})) {
	print $nextid."\n";
	# create dir with name actorid 
    # get the id (old or new one)
    # generate page
    # write info

    print O $licence_comment.$frame_before_title.
	"<title>Mark Lombardi Networks - $key</title>".
	"<h3>$key</h3><p>(preliminary unstable) Actor URI: ".
     	"<h4>Appears in</h4><ul>";

    # get appearances as array of titles
    foreach $n (@network) {
	# remove whitespace at beginning and end
	$n =~ s/^\s+|\s+$//g;
    # print as list
    # iterate through all networks in which the actor appears, sorted by name
    foreach $n (sort {&linktext_of_network($a) cmp &linktext_of_network($b)} @network) {
	print O "<li>$n<br />As related to";
	print O "$snip</li>\n";
    # links for search
    print O "</ul><br /><h4>More Information on $key</h4>".&info_links($key);
    # Info on aliases
    # collect aliases
    foreach $akey ( keys %alias) {
	if ($alias{$akey} eq $key) {
	    $alias_info.="<li>".$akey."<br />".&info_links($akey)."</li>\n";
    # Emit aliases if any
    if (length($alias_info)!=0) {
	print O "<p></p><h4>Also appears as:</h4><ul>$alias_info</ul>\n";
    # close page and proceed
    print O "</ul>".$frame_after;
#<br />".$licence_snippet."</div></body></html>\n";
    close O;

# store the actorids array
print O Dumper(\%actorid);