#!/usr/bin/perl
#
# Webcluster-Client DB0LJ-6
#
# (C) Hans-J. Barthen, DL5DI
#     Franz-Josef-Str. 20
#     D-56642 Kruft
#
# 07.02.2008  1.Beta-Version
# 08.02.2008  1.active version at dx.db0lj.de
# 10.02.2008  URL generation for qrz.com added
# 19.02.2008  1st release for download on the web
# 24.02.2008  automatic creation of cnt-file
# 25.02.2008  convert special characters to html-codes
# 09.03.2008  show newest records first
# 07.01.2010  show date in WCY and WWV datas to recognize entries older than a day
# 30.06.2010  typo in timestamp of WWV/WCY brought up wrong month - fixed
# 04.01.2013  url of qrz.com-database changed
#

$| = 1;		# flush print and write
$rev = "20130104";

# clear content of variables
$spot[1] = ""; $spot[2] = ""; $spot[3] = ""; $spot[4] = "";
$spot[5] = ""; $spot[6] = ""; $spot[7] = ""; $spot[8] = "";
$spot[9] = ""; $spot[10] = ""; $spot[11] = ""; $spot[12] = "";
$spot[13] = ""; $spot[14] = ""; $spot[15] = ""; $spot[16] = "";
$spot[17] = ""; $spot[18] = ""; $spot[19] = ""; $spot[20] = "";
$ann[1] = ""; $ann[2] = ""; $ann[3] = "";
$wcy[1] = ""; $wcy[2] = ""; $wcy[3] = "";
$wwv[1] = ""; $wwv[2] = ""; $wwv[3] = "";

# get actual date and time to create the dxspider directory and filenames
($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst)=gmtime(time);
$mon++;
$yday++;
$year+=1900;

# save a copy for the timestamp in the bottom of the page
$stamp=sprintf("%2.2d:%2.2d:%2.2d UTC",$hour,$min,$sec);

# create filenames
$spotfile=sprintf("/spider/data/spots/%4.4d/%3.3d.dat",$year,$yday);
$annfile=sprintf("/spider/data/log/%4.4d/%2.2d.dat",$year,$mon);
$wcyfile=sprintf("/spider/data/wcy/%4.4d/%2.2d.dat",$year,$mon);
$wwvfile=sprintf("/spider/data/wwv/%4.4d/%2.2d.dat",$year,$mon);
$dxccfile=sprintf("/spider/data/dxcc.list");

# read and increase hit counter
if (open(CNTFILE, "/var/local/httpd/cgi-bin/webcluster.cnt")) {
    $cnt = <CNTFILE>;
    close(CNTFILE);
    chop($cnt);
    $cnt++;
} else {
    $cnt = 1;
}    

# write counter back to file
if (open(CNTFILE, ">/var/local/httpd/cgi-bin/webcluster.cnt")) {
    printf(CNTFILE "$cnt\n");
    close(CNTFILE);
}

##################### Number of spots, min, max, default ##############

    if ($ARGV[0] > 0) { # min
	$n = $ARGV[0];
	if($n > 50) {
	    $n == 50;	# max
	}
    } else {
	$n = 20;	# default
    }

###################### HTML-Header ########################

    print("<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">");
    print("<html>");
    print("<head>");
    print("   <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
    print("   <meta name=\"Author\" content=\"Hans-Juergen Barthen, DL5DI\">");
    print("   <meta name=\"GENERATOR\" content=\"webcluster.pl DL5DI 2/2008\">");
    print("   <META HTTP-EQUIV=\"Expires\" CONTENT=\"1800\">");
    print("   <META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">");
    print("   <META HTTP-EQUIV=\"Refresh\" CONTENT=\"120\">");
    print("   <title>DX-Cluster Paris - F6BVP-3</title>");
    print("</head>");
    print("<body>");
    print("<div ALIGN=right><table WIDTH=\"100%\" >");
    print("<tr>");
    print("<td><img SRC=\"/flag-fr.png\" BORDER=0 height=30 width=50></td>");
    print("<td ALIGN=CENTER><b><font size=+2>DX Cluster Paris - F6BVP-3</font></b></td>");
    print("<td ALIGN=RIGHT><img SRC=\"/eu.jpg\" BORDER=0 height=30 width=45></td>");
    print("</tr>");
    print("</table></div>");

    print("<table BORDER WIDTH=\"100\%\" >");


######################################################################
#
# Create dynamic tables / be careful with changes here!
#
######################################################################


#################### DX-Spots ##########################

if(open(SPOTS, $spotfile)) {


    while($line = <SPOTS>) {
	chop($line);
	
	for($i = $n;$i > 1; $i--) {
	    $spot[$i] = $spot[$i-1];
	}
	$spot[1] = $line;
    }

    $i = 1;

# Table start
    print("<table BORDER CELLSPACING=0 CELLPADDING=0 WIDTH=\"100%\" BGCOLOR=\"#33CCFF\" >");


    while($i <= $n) {
	($qrg,$call,$zeit,$text,$spotter,$dxcc,$dummy,$dummy,$itu,$cq,$dummy) = split(/\^/,$spot[$i],11);

	# fix some special chars which may destroy the table structure
	$text =~ s/\;/:/g;
	$text =~ s|\\|\/|g;
	$text =~ s/\&/&amp;/g;
	$text =~ s/\$/&sect;/g;
	$text =~ s/\|/&brvbar;/g;
	$text =~ s/</&lt;/g;
	$text =~ s/>/&gt;/g;
	$text =~ s/\"/&quot;/g;
	$text =~ s/ä/&auml;/g;
	$text =~ s/ö/&ouml;/g;
	$text =~ s/ü/&uuml;/g;

	$dbi = 1;
	if(open(DXCC, $dxccfile)) {
	    while($dbi < $dxcc) {
		$dbline = <DXCC>;
		$dbi++;
	    }
	    $pfx = <DXCC>;
	    close(DXCC);
	}

	if((length($spotter) > 0)) {
	    print("<tr>");
	    printf("<td>DX de %s:</td>",$spotter);
	    printf("<td>%10.1f</td>",$qrg);
	    if(index($call,'/') > 0){
		($teil1,$teil2) = split(/\//,$call,2);
		if(length($teil2) < length($teil1)) {
		    $qrzcall = $teil1;
		} else {
		    ($qrzcall,$dummy) = split(/\//,$teil2,2);
		}
	    } else {
		$qrzcall = $call;
	    }

	    $tmp = $qrzcall;
	    $num = $tmp =~ tr/0-9//;
	    $tmp = $qrzcall;
	    $abc = $tmp =~ tr/A-Za-z//;
	    
	    if((index($qrzcall,'/') > 0) || ($num == 0) || ($abc < 2)) {
		printf("<td>%15.15s</td>",$call);
	    } else {
		printf("<td><a href=\"http://www.qrz.com/db/%s\" target=\"QRZ.com\">%15.15s</a></td>",$qrzcall,$call);
	    }
	    printf("<td>%30.30s</td>",$text);
	    printf("<td>%2.2s</td>",$pfx);
#	    printf("<td>%2.2s</td>",$itu);
	    printf("<td>%2.2s</td>",$cq);
	    ($sec,$min,$std,$mtag,$jahr,$wtag,$jtag,$somzeit) = gmtime($zeit);
	    printf("<td>%2.2d%2.2dZ</td>",$std,$min);
	    print("</tr>");
	}
	$i++;

    }    
    
    close(SPOTS);

# Table end

    print("</table>");
}

#################### WCY-Datas ##########################

if(open(WCYS, $wcyfile)) {


    while($line = <WCYS>) {
	chop($line);
	$wcy[3] = $wcy[2];
	$wcy[2] = $wcy[1];
	$wcy[1] = $line;
    }
    $i = 1;

# Table start

    print("<table BORDER CELLSPACING=0 CELLPADDING=0 WIDTH=\"100%\" BGCOLOR=\"#FFFF00\" >");

    while($i <= 3) {

	($zeit,$sfi,$a,$k,$expk,$r,$sa,$gmf,$aurora,$spotter,$dummy) = split(/\^/,$wcy[$i],11);
	if($sa eq "qui") {
	    $sa = "quiet";
	} elsif ($sa eq "act") {
	    $sa = "active";
	}
	
	if($gmf eq "qui") {
	    $gmf = "quiet";
	} elsif ($gmf eq "act") {
	    $gmf = "active";
	}
	($sec,$min,$std,$mtag,$monat,$jahr,$wtag,$jtag,$somzeit) = gmtime($zeit);

	if((length($spotter) > 0)) {
	    print("<tr>");
	    printf("<td>WCY de %s</td>",$spotter);
	    printf("<td>%4.4d/%2.2d/%2.2d %2.2d%2.2dZ</td>",$jahr+1900,$monat+1,$mtag,$std,$min);
	    printf("<td>SFI=%d</td>",$sfi);
	    printf("<td>A=%d</td>",$a);
	    printf("<td>K=%d</td>",$k);
	    printf("<td>expK=%1d</td>",$expk);
	    printf("<td>R=%d</td>",$r);
	    printf("<td>SA: %s</td>",$sa);
	    printf("<td>GMF: %s</td>",$gmf);
	    if($aurora eq "yes") {
		printf("<td>Aurora: <b>!YES!</b></td>");
	    } else {	    
	        printf("<td>Aurora: %s</td>",$aurora);
	    }
	    print("</tr>");
	}
	$i++;
    }    
    
    close(WCYS);

# Table end

    print("</table>");
}


#################### WWV-Datas ##########################

if(open(WWVS, $wwvfile)) {


    while($line = <WWVS>) {
	chop($line);
	$wwv[3] = $wwv[2];
	$wwv[2] = $wwv[1];
	$wwv[1] = $line;
    }
    $i = 1;

# Table start

    print("<table BORDER CELLSPACING=0 CELLPADDING=0 WIDTH=\"100%\" BGCOLOR=\"#FF9900\" >");

    while($i <= 3) {
#	print "$wwv[$i]\n";
	($spotter,$zeit,$sfi,$a,$k,$text,$dummy) = split(/\^/,$wwv[$i],7);
	($sec,$min,$std,$mtag,$monat,$jahr,$wtag,$jtag,$somzeit) = gmtime($zeit);

	if((length($spotter) > 0)) {
	    print("<tr>");
	    printf("<td>WWV de %s</td>",$spotter);
	    printf("<td>%4.4d/%2.2d/%2.2d %2.2d%2.2dZ</td>",$jahr+1900,$monat+1,$mtag,$std,$min);
	    printf("<td>SFI=%d</td>",$sfi);
	    printf("<td>A=%d</td>",$a);
	    printf("<td>K=%d</td>",$k);
	    $text =~ tr/<>/ /;
	    printf("<td>%s</td>",$text);
	    print("</tr>");
	}
	$i++;

    }    
    
    close(WWVS);

# Table end

    print("</table>");
}


#################### Announcements ##########################

if(open(ANNS, $annfile)) {

# Table start

    print("<table BORDER CELLSPACING=0 CELLPADDING=0 WIDTH=\"100%\" BGCOLOR=\"#33CC33\" >");

    while($line = <ANNS>) {
	chop($line);
	($zeit,$flag,$dummy) = split(/\^/,$line,3);
	if($flag eq 'ann') {
	    $ann[3] = $ann[2];
	    $ann[2] = $ann[1];
	    $ann[1] = $line;
	}
    }
    $i = 1;
    while($i <= 3) {    
	($zeit,$flag,$dest,$spotter,$text,$dummy) = split(/\^/,$ann[$i],6);
	($sec,$min,$std,$mtag,$jahr,$wtag,$jtag,$somzeit) = gmtime($zeit);
	if((length($spotter) > 0)) {
	    print("<tr>");
	    printf("<td>To %s de %s</td>",$dest,$spotter);
	    printf("<td>%2.2d%2.2dZ</td>",$std,$min);
	    printf("<td>%s</td>",$text);
	    print("</tr>");
	}
	$i++;
    }
    
    close(ANNS);

# Table end

    print("</table>");
}


###################### HTML-Footer ################################
#
# Place some URLs or other information here.
# The update interval, the timestamp of the last update ($stamp) 
# and the hit-counter ($cnt) can be placed here
#

print("<table WIDTH=\"100%\" >");
print("<tr>");
print"<td ALIGN=left>QTH : <a href=\"http://f6bvp.org\" target=\"DX-Cluster\">http://f6bvp.org</a></td>";
print"<td ALIGN=center>$stamp (Refresh 30s)</td>";
print"<td ALIGN=right><i><font size=-1><a href=\"http://download.prgm.org/dl5di-soft/dxspider-webcluster/\">Web Interface by DL5DI, $rev</font></i></td>";
print("</tr>");
print("</table></div>");
print("<table WIDTH=\"100%\" >");
print("<tr>");
print"<td ALIGN=right><i><font size=-2>$cnt</font></i></td>";
print("</tr>");
print("</table></div>");
print("</body>");
print"</html>";

exit;
