#/usr/bin/perl
# FILE: mathgen.pl
# generates math genealogy for mathematicians, outputs g.pdf file with genealogy graph
#
# Usage: goto http://genealogy.math.ndsu.nodak.edu
# find the name and node number of the person
# start with 
# perl mathgen.pl <node number> <name>
# for example
# perl mathgen.pl 76086 "Remco Bouckeart"
# perl mathgen.pl 91944 "Steffen Klaere"
# perl mathgen.pl 112199 "David Bryant"
# Requirements: needs perl and dot
#
# Author: Remco Bouckaert remco@cs.auckland.ac.nz
# License: GPL

$id[0]="76086";
$name[0]="Remco Bouckaert";
if ($#ARGV!=-1) {
	$id[0] = $ARGV[0];
	$name[0] = $ARGV[1];
}


$base="http://genealogy.math.ndsu.nodak.edu/id.php?id=";
$i = 0;

$nColor=0;
$color[0] = "red";
$color[1] = "black";
$color[2] = "blue";
$color[3] = "yellow";
$color[4] = "green";
$color[5] = "grey";
$color[6] = "#ff00ff";
$color[7] = "#00ffff";
$color[8] = "#ff00ff";
$color[9] = "#ff00ff";
$color[10] = "#ff00ff";
$color[11] = "#8080ff";
$color{Germany}="red";
$color{Greece}="grey";
$color{Netherlands}="blue";
$color{France}="green";
$color{UnitedKingdom}="yellow";
$color{Italy}="#8080ff";
$color{Belgium}="#ff8080";
$color{Switzerland}="#80ff80";
while ($i <= $#id) {
	if (!($done[$id[$i]] == 1)) { 
#		`wget -O $id[$i].htm -o log $base$id[$i]`;
		$done[$id[$i]] = 1;
		$map{$id[$i]} = $i;
		$line = 0;
		open(FIN,"$id[$i].htm") or die "Cannot open file $id[$i].htm";
		$n = 0;
		while ($s = <FIN>) {
			if ($s =~/flags\/(.*).gif/) {
				$country[$i] = $1;
				$s = $s[$line-2];
				$s =~ />([^<]*)<\/span>\s*$/;
				$year[$i] = $1;
				 $c = $country[$i];
				 $c =~ s/-.*//;
				if ($color{$c} eq "") {
				 $color{$c} = $color[$ncolor++];
				}
			}
			while ($s=~/Advisor[^<]*<a href="id.php\?id=([0-9]+)">([^<]*)<\/a>/) {
				$id[$#id+1] = $1;
				$name[$#id] = $2;
				$edge[$#edge+1] = "$id[$#id] -> $id[$i]\n";
				$s=~s/Advisor[^<]*<a href="id.php\?id=([0-9]+)">([^<]*)<\/a>//;
			}
		$s[$line] = $s;
		$line++;
		}
	}
	$i++;
}

open(FOUT,">g.dot");
print FOUT "digraph G{\n";
for ($i = $#edge; $i>=0;$i--) {
	print FOUT $edge[$i];
}
foreach $id (sort (keys(%map))) {
	 $c = $country[$map{$id}];
	 $c =~ s/-.*//;
	$name = $name[$map{$id}];
	$name =~ s/ /_/g;
#	print FOUT "$id [label=\"$name[$map{$id}] [$country[$map{$id}]] $year[$map{$id}]\" color=\"$color{$c}\" URL=\"http://en.wikipedia.org/wiki/$name\"]\n";
	print FOUT "$id [label=\"$name[$map{$id}] [$country[$map{$id}]] $year[$map{$id}]\" URL=\"http://en.wikipedia.org/wiki/$name\"]\n";
}
print FOUT "}\n";
close FOUT;
print `dot -Tpdf g.dot > g.pdf`;
print `dot -Tsvg g.dot > g.svg`;
open(FIN, "g.svg");
open(FOUT, ">g2.svg");
while ($s = <FIN>) {
	if ($s =~ /<text text-anchor="middle" x="([^"]*)" y="([^"]*)".*>.*\[(.*)\].*</) {
		$x = $1;
		$y = $2;
		$cs = $3;
		$x -= 100;
		$y -= 35;
		foreach $c (split('&#45;',$cs)) {
			print FOUT "<image x='$x' y='$y' width='30' height='20' xlink:href='$c.png'/>\n";
			$x += 30;
		}
		$s =~ s/\[.*\]//;
	}
	
	print FOUT $s;
}
close FOUT;
close FIN;

