#!/usr/bin/perl
#
# Extract DX spots from 2 and 6 meters where there seems to be a QTH
# locator in the comment field. Used by DL8EBW to maintain and update
# his VHF locator database.
#
# Last changed: DL6RAI Sun Mar  7 08:59:20 GMT 1999


require 'getopts.pl';
$om = "dl8ebw";


$out = "/tmp/$om.txt";
$days = "60"; # default value
&Getopts('d:');

if (length($opt_d) > 0) {
        $days = $opt_d; 
}

print "Extracting QTH information in comment fields of DX table\nover the last $days days to $out... pse QRX\n";

$db_name = "clx_db";
$psql = "psql $db_name -qtc";
$qry = sprintf("SELECT DISTINCT a.dx_call,a.dx_misc,a.dx_dati::date, \
		a.log_a,a.dx_freq \
	FROM dx_data a, ar_band b WHERE \
	        ( ( a.dx_freq >= b.l_freq AND \
                    a.dx_freq <= b.u_freq ) AND \
                    b.b_symb IN ('6','2') ) AND \
	( dx_dati > timemi('now','%s days') ) AND \
	( dx_misc ~* '[a-r][a-r][0-9][0-9][a-x][a-x]') \
	ORDER BY dx_call,dx_dati",$days);
$cmd = $psql . " \"" . $qry . "\"" ;


$c = 0;
open(OUT,"> $out") || die "Cannot open file $out for writing\n";
open(DX,"$cmd |");
while (<DX>) {
	chop;
	$c++;
	($dx,$cmt,$date,$logger,$freq) = (split(/\|/))[0..4];
	$dx =~ s/ //g;
	$logger =~ s/[\s\t]//g;
	printf OUT ("%-12s %10.1f %12s %-30s <%s>\n",
		$dx,$freq,$date,$cmt,$logger);
}
close(DX);
close(OUT);

print "$c records extracted.\n";
# print "$c records extracted - now sending to $om.\n";

# $cmd = "filesend -s \"Locator-Daten\" -u $om $out";
# system($cmd);

__END__
