#!/usr/bin/perl
#
#
# This tool will be used to excerpt QSL information from
# the dx_data table some time in the future.
# 
# By default, the last 30 days from the dx_data table are scanned.
# We are looking for spots where comment fields contain the string 
# "QSL" case insensitively. 
# 
# The comment field is then modified to contain QSL information in 
# the pattern QSL VIA ... This string is then excerpted, some
# sanity checks are made and if the third word seems to be a callsign, 
# the information is kept in an associative array. 
# 
# If three spots come up with the same information, we believe it is
# good enough to be added to the QSL database. This is done, after
# looking up if that information was not already there. The information
# is added with a source name dx_data so that users still can decide
# if that automatic information will be used by them or not.
# 
# Last Change by DL6RAI: Tue Jul 20 18:22:51 GMT 1999

require 'getopts.pl';

$days = "9999";
$quiet = 0;

&Getopts('qd:');

$days = $opt_d if ($opt_d); 
$quiet = 1 if ($opt_q);

$limit = 3; # number of times info must repeat before we trust it.
$c = 0;
$| = 1;
$now = gmtime();

$db_name = "clx_db";
$psql = "psql $db_name -qtc";
$qry = sprintf("SELECT dx_call,dx_misc,dx_dati FROM dx_data WHERE \
	dx_dati > timemi('now','%s days') AND \
	( dx_misc ~* 'QSL' OR dx_misc ~* 'VIA')",$days);
# $cmd = "awk 'BEGIN{FS=\"\\t\"}{print \$1,\"|\",\$2,\"|\",\$3}' < /usr/local/clx/tmp/tmpdx";
# real life 
$cmd = $psql . " \"" . $qry . "\"" ;

print("$cmd\n") if (! $quiet);
open(DX,"$cmd |");
while (<DX>) {
	$c++;
	($dx,$cmt,$time) = (split(/\|/))[0..2];
	$dx =~ s/ //g;
	if ( "$cmt" ) {

		$cmt = lc($cmt);
		$cmt =~ s/>/ via /;	       # change > into "via"
		$cmt =~ s/\(/ (/g;             # change ( into " ("
		$cmt =~ s/\)/) /g;             # change ) into ") "
		$cmt =~ s/[-=*.>,:;!@\$]/ /g;  # remove any strange characters
		$cmt =~ s/ v / via /i;
		$cmt =~ s/ v. / via /i;
		$cmt =~ s/ to / via /i;
		$cmt =~ s/ info / via /i;
		$cmt =~ s/qsl / QSL VIA /i;    # QSL DL6RAI -> QSL VIA DL6RAI
		$cmt =~ s/via( +)via/VIA/gi;   # now remove via's again
		$cmt =~ s/via( +)via/VIA/gi;   # so that QSL VIA ... remains
		$cmt =~ s/via( +)via/VIA/gi;
		# print "$dx $cmt\n";

		@a = split(' ',$cmt);

		$i = 2;
		$info = "";
		while ($i <= $#a) {
			if ( ($a[$i-2] eq 'QSL') && ($a[$i-1] eq 'VIA') ) {
				$info = $a[$i];
			}
			$i++;
		}
		$info =~ s/ //g;

		next if ($dx =~ /^[0-9]*$/);    # if it has only figures in it
		next if ($dx =~ /^[a-z]*$/);    # if it has only characters in it
		next if ($dx !~ /^[0-9a-z\/]*$/);  # if it contains anything but 0-9, a-z and /
		next if (length($dx) < 3);      # this cannot be a callsign
		next if (length($dx) > 14);     # this cannot be a callsign

		next if ($info =~ /^[0-9]*$/);  # if it has only figures in it
		next if ($info =~ /^[a-z]*$/);  # if it has only characters in it
		next if ($info !~ /^[0-9a-z\/]*$/);# if it contains anything but 0-9, a-z and /
		next if (length($info) < 4);    # this cannot be a regular callsign

		printf("-> %-12s via %s <-\n",$dx,$info) if (! $quiet);
		$k=sprintf("%s-%s",$dx,$info);
		if (defined($q{$k})) {
			$q{$k}++;
		} else {
			$q{$k}=1;
		}

	}
}
close(DX);
# done reading the input, now process the output


$c = 0; $a=0;
foreach $i (sort keys %q) {
	if ( $q{$i} > $limit ) { 

		($dx,$info) = split(/-/,$i); # split this up again
		$c++;

		# see if it's already there
		$qry = sprintf("select distinct stn_mng from qsl_mng \
			where stn_call='%s' and stn_mng = '%s'",$dx,$info);
		$cmd = "$psql \"$qry\"";
		$result = `$cmd`;
		chop($result);
		$result =~ s/ //g;

		if ( $result eq "" ) {
			print "Adding $i to database.\n";
			$qry = sprintf("insert into qsl_mng \
			(stn_call,stn_mng,auth_i,inf_auth,add_time) \
			values('%s','%s','%s','%s','%s')",$dx,$info,
			"5","dx_data",$now);
			$cmd = "$psql \"$qry\"";
			system($cmd);
			$a++;
		}
	}
}
print "Total $c records, $a added to database\n";
