#!/usr/local/bin/perl
# This is perl V 5
# 
# reads mail from stdin and saves them to a specific directory
# overwriting old entries in that directory
#

$save_dir='/temp/save/';
$dbpath='/temp/news-links';
$datum= `date +'%y/%m/%d'`;

# Threshold in percent
$thres=0;
$thres=$ARGV[$#ARGV];

# Threshold -- links with less articles never get accepted
$cutoff=50;

# read file where locations can be found
&read_loc;

$xmv=10;
$ymv=10;

###################################################
#
#
chdir($save_dir) || die "Can't go to $path ";

opendir(DIR,'.') || die "Can't open dir $path";
@filenames = readdir(DIR);
closedir(DIR);

####dbmopen(%db,$dbpath,0644) || die "Can't open dbm-File $dbpath";

foreach $file (@filenames) {
	next if($file eq "."); 
	next if($file eq "..");
	open(IN,$file);
	$tot=0;
	while(<IN>) {
		chop;
		if ($tot==0) {
			($gaga,$foo)=split;
			if ($gaga eq "Total:") {
				$tot=$foo;
				$t=$tot*$thres / 100;
			}
		} else {
			($to,$from,$num)=split(/!/);
			if($num > $cutoff && $num > $t) {
#			print $to," <- ",$from,"   ..... ",$num,"\n";
				$in=$to."_".$from;
				$db{$in}++;
			}
		}
	}
	close(IN);
}
##dbmclose(%db);

####
# Now that we have all sites in the db, we can produce links as 
# output
####

sleep 5;

####dbmopen(%db,$dbpath,0644) || die "Can't open dbm-File $dbpath";

#foreach $link (sort keys %db) {
#	($to,$from) = split('_',$link);
#	print $to,"<-",$from,"\n";
#}

###
# Try to produce a postscript file
#
###

open(OUT,">/temp/links.ps") || die "Can't create postscript ";
print OUT <<GAGA;
%!PS-Adobe-2.0
%%Title: Map of newsflow
%%Creator: Heiko W.Rupp (hwr\@xlink.net)
%%For: netnews-wg\@ripe.net
%%DocumentFonts: (atend)
%%Pages: 0 0
%%BoundingBox: 0 0 640 400
%%DocumentPaperSizes: a4
%%Orientation: Landscape
%%EndComments

% "arrowhead" takes these arguments:
% lineweight prevX prevY
 /arrowhead { %def
	gsave
		currentpoint
		4 2 roll exch 4 -1 roll exch
		sub 3 1 roll sub
		exch atan rotate dup scale
		-1 2 rlineto
		7 -2 rlineto
		-7 -2 rlineto
		closepath fill
	grestore
	newpath
 } bind def
 /arrowline { %def    % lineto-arrow
	currentlinewidth currentpoint 5 3 roll
	lineto
	currentpoint stroke moveto
	arrowhead
 } bind def

/Helvetica findfont 8 scalefont setfont
10 10 moveto
(\(c\) 1996 Heiko W.Rupp <hwr\@xlink.net> Threshold is $thres Percent printed on $datum) show
/Helvetica findfont 6 scalefont setfont
0.5 setlinewidth
GAGA

foreach $link (sort keys %db) {
	($to,$from) = split('_',$link);
	if ($loc{"x$to"} eq "") {
		$loc{"l$to"}="i";
	}
	if ($loc{"x$from"} eq "") {
		$loc{"l$from"}="i";
	}
	if ($loc{"l$to"} ne "i" && $loc{"l$from"} ne "i" ) {
	 	printf(OUT "%% link from %s to %s\n",$to,$from);
		printf(OUT "%f %f moveto\n",$loc{"x$to"},$loc{"y$to"});
		printf(OUT "%f %f lineto\n",$loc{"x$from"},$loc{"y$from"});
#		printf(OUT "%f %f moveto\n",$loc{"x$from"},$loc{"y$from"});
#		printf(OUT "%f %f arrowline\n",$loc{"x$to"},$loc{"y$to"});
		$li{$to}++;
		$li{$from}++;
	}
}
print OUT "stroke\n";
print OUT "%% now show hostnames if they are not set to be not printed\n";
foreach $l (sort keys %li) {
	if ($loc{"n$l"} eq "p") {
		printf(OUT "%f %f moveto\n(%s) show\n",
			$loc{"x$l"}+2,$loc{"y$l"}+2,$l);
		printf(OUT "%f %f moveto\n",
			$loc{"x$l"},$loc{"y$l"});
		printf(OUT "%f %f 2 0 360 arc stroke\n",
			$loc{"x$l"},$loc{"y$l"});

	}
}
print OUT "showpage\n";
close(OUT);


##########################
#
# Subroutine that reads a file to get location infos
#
# format of file is 
# name!x!y!flags
# with x and y in form of 'hh mm ss D'  (coordinates)
# Not given precision must be set to 0 (e.g. 49 N must be written
# as 49 00 00 N
# and flags:   
#              p- show name on map
#	       i- ignore this site
# If a site only should be ignored then x and y might be empty

sub read_loc
{
	open(LOC,"locations") || die "Can't get coordinates file";
	while(<LOC>) {
		($name,$yr,$xr,$flags)=split(/!/);
# 		ignore all entry
		if ($flags =~ "i") {
			$loc{"l$name"}="i";
			$loc{"n$name"}="i";
			next;
		} else {
#		normal entries
			($h,$m,$s,$L)=split(/ /,$xr);
			$x=$h+($m/60)+($s/3600);
			$x = -$x if($L eq "W");
			&scale_x;
			($h,$m,$s,$L)=split(/ /,$yr);
			$y=$h+($m/60)+($s/3600);
			$y = -$y if($L eq "S");
			&scale_y;
			$loc{"x$name"}=$x;
			$loc{"y$name"}=$y;
			$loc{"l$name"}="i" if ($flags =~ /l/);
			if ($flags =~ /p/) {
				$loc{"n$name"}="p"; 
			} 
		}
	}

}

sub scale_x
{
	#clip large values
	if($x<-20) { $x= -20; }
	if($x>30) {$x= 30; }

	# scale
	if(($x>-5) && ($x <15)) {
		$x=($x-5)*25+350;
	} else {
		if ($x>=15 && $x<20) {
			$x=($x-5)*20+300;
		} else {
			$x=($x-5)*12+300;
		}
	}
	$x=$x+10;
}

sub scale_y
{
	# clip large values
	if($y<30) { $y=30; }
	if($y>70) { $y=70; }

	#scale
	if ($x > -19) {
		if ($y>43 && $y<55 ) {
			$y=($y-50)*27+200;
		} else {
			$y=($y-50)*10+200;
		}
	} else {
		# american sites
		if($y>30 && $y<55) {
#			$y=($y-39)*40+100;
			$y=($y-39)*50+100;
		}
	}
}
		
