/usr/bin/gawk '

function randint(n)
{
	return int(n * rand())
}

function v_name(class)
{
	if (class == 1)
		{ return "router" }
	else if (class == 2)
		{ return "network" }
	else
		{ return "???" }
}

function v_check_ref(key, class)
{
	if ((v_class[key] != class) && (v_class[key] != 3))
	{
		print "Invalid " v_name(class) " " key;
		return 0;
	}
	return 1;
}

function v_add(key, id, class)
{
	v_key_id[key] = id;
	v_id_key[id] = key;
	v_class[key] = class;
	if (!v_x[key] || !v_y[key])
	{
		v_x[key] = randint(1800)+100;
		v_y[key] = randint(800)+100;
		v_new[key] = 1;
	}
}

function v_get_key(id, class)
{
	k = v_id_key[id];
	if (!k)
	{
		k = id;
		print "New " v_name(class) " " id;
		v_add(k, id, class);
		v_new[k] = 1;
	}

	return k;
}

function e_make_key(k1, k2)
{
	if (k1 < k2)
		{ return k1 "+++" k2; }
	else
		{ return k2 "+++" k1; }
}

function e_add(k1, k2)
{
	lid = e_make_key(k1, k2);
	e_count[lid]++;
}

function e_get_key(k1, k2)
{
	kk = e_make_key(k1, k2);
	if (!e_count[kk])
	{
		print "New link " k1 " - " k2;
		e_count[kk] = 1;
		e_new[kk] = 1;
	}
	return kk;
}

function e_found(k1, k2)
{
	if (k1 == k2)
	{
		grp_found[k1]++;
		return;
	}

	kl = e_get_key(k1, k2);
	if (k1 < k2)
		{ e_found_d1[kl]++; }
	else
		{ e_found_d2[kl]++; }
}

function print_vert(key, state)
{
	x = v_x[key];
	y = v_y[key];
	dx = 48;
	dy = 12;
#	printf "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"white\" stroke=\"black\" stroke-width=\"2\" />\n",
#		x-dx, y-dy, 2*dx, 2*dy >> output
#	printf "<text x=\"%d\" y=\"%d\" text-anchor=\"middle\" dominant-baseline=\"middle\" >%s</text>\n\n",
#		x, y, key >> output
	printf "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" class=\"%s\" />\n",
		x-dx, y-dy, 2*dx, 2*dy, state >> output
	printf "<text x=\"%d\" y=\"%d\" class=\"%s\">%s</text>\n\n",
		x, y, state, key >> output
#	print "Vert " v_name(v_class[key]) " " key " " state;
}

function print_edge(k1, k2, state)
{
	printf "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" class=\"%s\" />\n", 
		v_x[k1], v_y[k1], v_x[k2], v_y[k2], state >> output
}

BEGIN{
	src_spec="src_spec";
	src_coords="src_coords";
	src_state="topo";
	src_header="src_header";
	output="aaa.svg"

	printf "" > output;
	while (getline < src_header)
	{
		print $0 >> output;
	}

	while (getline < src_coords)
	{
		if ($1)
		{
			v_x[$1] = $2;
			v_y[$1] = $3;
		}
	}

	while (getline < src_spec)
	{
		if ($1 == "rt")
		{
			v_add($2, $3, 1);
		}

		if ($1 == "lnk" && v_check_ref($2, 1) && v_check_ref($3, 1))
		{
			e_add($2, $3);
		}

		if ($1 == "net")
		{
			v_add($2, $3, 2);
			for(i = 4; i <= NF; i++)
			{
				if (v_check_ref($i, 1))
				{
					e_add($2, $i);
				}
			}
		}

		if ($1 == "grp")
		{
			v_add($2, $3, 3);
			grp_count[$2]=NF-3;
			for(i = 4; i <= NF; i++)
			{
				v_id_key[$i] = $2;
			}
		}
	}

	FS=" +";
	state = 0;
	while (getline < src_state)
	{
		if ($1 == "\trouter")
		{
			state = 1;
			key = v_get_key($2, 1);
			v_found[key]++;
		}

		if ($1 == "\tnetwork")
		{
			state = 2;
			key = v_get_key($2, 2);
			v_found[key]++;
		}
		
		if ($1 == "")
		{ state = 0; }
		
		if ($1 == "\t\trouter" && state == 1)
		{
			k2 = v_get_key($2, 1);
			e_found(key, k2);
		}

		if ($1 == "\t\tnetwork" && state == 1)
		{
			k2 = v_get_key($2, 2);
			e_found(key, k2);
		}

		if ($1 == "\t\trouter" && state == 2)
		{
			k2 = v_get_key($2, 1);
			e_found(key, k2);
		}
	}

	for (key in e_count)
	{
		split(key, kp, "+++");
		k1 = kp[1];
		k2 = kp[2];
		
		if (e_new[key])
			{ e_count[key] = e_found_d1[key] }

		if (e_found_d1[key] == e_count[key] &&
		    e_found_d2[key] == e_count[key])
		{
			print_edge(k1, k2, e_new[key] ? "new" : "good");
		}
		else
		{
			print_edge(k1, k2, (v_found[k1] || v_found[k2]) ?
				   "bad" : "unreach");
		}
	}

	print "" >> output;

	for (key in v_key_id)
	{
		if (v_new[key])
			{ print_vert(key, "new"); }
		else if (!v_found[key])
			{ print_vert(key, "unreach"); }
		else if ((v_class[key] == 3) &&
			 ((v_found[key] != (grp_count[key] + 1)) ||
			  (grp_found[key] != (grp_count[key] * 2))))
			{ print_vert(key, "bad"); }
		else
			{ print_vert(key, "good"); }
	}

	print "</svg>" >> output;
}'
