#!/usr/bin/perl
#
# Design goals:
#
# 1. Transmit in compressed mode with checksum to cope with disturbed
#    propagation path and QSB. 
# 2. Send each Packet three times.
# 3. Keep a buffer and when the buffer runs full, intelligently 
#    drop spots older than say 15 minutes (not yet implemented).
# 4. Filter out any non-HF spots.
# 5. Transmit only DX spots, nothing else.
#
#
# DX de it9dqm:    14257.0  HS0/IK4MRO   QRT !                          2057Z
# IT9PDM^14257.0^HS0/IK4MRO^QRT !^2057^03EF
#
# Last Change: DL6RAI, Sat Sep 11 07:41:35 GMT 1999

$txpipe = "/tmp/psk.fifo";
$rxfile = "/tmp/dx.spots";

if ( ! -p $txpipe ) {
	die "Pipe $txpipe does not exist.\n";
} else {
	open(PIPE,">> $txpipe");
	# select PIPE;
	# $| = 1;
}

if ( ! -e $rxfile ) {
	die "File $rxfile does not exist.\n";
} else {
	open(IN,"tail -5f $rxfile |");
}

while(<IN>) {
	if (/^DX de .*Z$/) {
		# pack it up:
		chop;
		s/DX de //;
		($from,$rest) = split(':',$_,2);
		($freq,$call,$rest) = split(' ',$rest,3);
		next if ($freq > 30000 || $freq < 1800);
		next if ($freq >= 10100 && $freq <= 10150);
		next if ($freq >= 18068 && $freq <= 18168);
		next if ($freq >= 24890 && $freq <= 24990);
		$utc = substr($rest,-5);
		$comment = substr($rest,0,length($rest)-5);
		$comment =~ s/^ +//;
		$comment =~ s/ +$//;
		$spot = sprintf("%s^%s^%s^%s^%s",$from,$freq,$call,
			$comment,$utc);
		$spot = uc($spot);
		$checksum = (unpack("%72C*", $spot) % 255);
		$tx = sprintf("%s^%02x",$spot,$checksum);
		$tx = uc($tx);

		syswrite(PIPE,"$tx\r\n",80);
		$len = ((length($tx)+1)* 3 * 8)/31.25;
		printf ("%-70s %4.1f sec.\n",$tx,$len);

	}
	sleep 1;
}
