#!/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.
# 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, Tue Aug 24 19:17:06 GMT 1999

$txpipe = "/tmp/psk.fifo";
$rxfile = "/tmp/psk.log";


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

$dummy = <STDIN>;
($mycall,$destination) = @ARGV;
# print("*** Connected to $destination\n");

#ea5arc^21025.0^3C0R^up to 39 puff 5/9+^0819Z^5e
#ea5arc^21025.0^3C0R^up to 39 puff 5/9+^0819Z^5e
while (1) {
	while(<IN>) {
		chop;
		# Kleinbuchstaben ausfiltern, es kommen nur groe
		tr /a-z\015//d; 
		# Auspacken
		$rx = $_;
		$tx_checksum = substr($rx,-2);
		$spot = substr($rx,0,-3);
		$rx_checksum = (unpack("%72C*",$spot) % 255);
		if ($rx_checksum == hex($tx_checksum)) {
			($from,$freq,$call,$comment,$utc) = 
				split(/\^/,$spot);
			$from .= ':';
			printf("DX de %-9s %8s  %-12s %-29s %6s\n",
	      	                	$from,,$freq,$call,$comment,$utc);
	
		} else {
			print "Checksum does not match, sorry\n";
			print "TX: $tx_checksum\n";
			print "RX: $rx_checksum\n";
		}
	}

	sleep 1;
	seek(IN,0,1);
}
close(IN);
