move ganerated stuff to local_data
[spider.git] / perl / create_prefix.pl
index 9c98a46c81ebdf0bfd617fd686d9bd4382360be0..daf4a09e409171cb1433b068aa9aa9a8c1d218c6 100755 (executable)
@@ -1,9 +1,9 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
 # a program to create a prefix file from a wpxloc.raw file
 #
 # Copyright (c) - Dirk Koopman G1TLH
 #
-# $Id$
+#
 #
 
 require 5.004;
@@ -13,13 +13,18 @@ BEGIN {
        # root of directory tree for this system
        $root = "/spider"; 
        $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
-       
+
+       mkdir "$root/local_data", 02777 unless -d "$root/local_data";
+
        unshift @INC, "$root/perl";     # this IS the right way round!
        unshift @INC, "$root/local";
 }
 
-use DXVars;
+use SysVar;
+
 use Data::Dumper;
+use DXUtil;
+
 use strict;
 
 my %loc = ();                                          # the location unique hash
@@ -28,9 +33,17 @@ my %locn = ();                                               # the inverse of the above
 my %pre = ();                                          # the prefix hash
 my %pren = ();                                         # the inverse
 
+if (@ARGV && $ARVG[0] =~ /^--system$/) {
+       $prefix = $main::data;
+       shift;
+} else {
+       $prefix = $main:local_data;
+}
+
 # open the input file
 my $ifn = $ARGV[0] if $ARGV[0];
-$ifn = "$main::data/wpxloc.raw" if !$ifn;
+
+$ifn = "$prefix/wpxloc.raw" if !$ifn;
 open (IN, $ifn) or die "can't open $ifn ($!)";
 
 # first pass, find all the 'master' location records
@@ -90,33 +103,42 @@ close(IN);
 
 #print Data::Dumper->Dump([\%pre, \%locn], [qw(pre locn)]);
 
-# now open the rsgb.cty file and process that again the prefix file we have
-open(IN, "$main::data/rsgb.cty") or die "Can't open $main::data/rsgb.cty ($!)";
+# now open the cty.dat file if it is there
+my @f;
+my @a;
 $line = 0;
-while (<IN>) {
-       $line++;
-       next if /^\s*#/;
-       next if /^\s*$/;
-       my $l = $_;
-       chomp;
-       my @f = split /:\s+|;/;
-       my $p = uc $f[4];
-       my $ref = $pre{$p};
-       if ($ref) {
-               # split up the alias string
-               my @alias = split /=/, $f[5];
-               my $a;
-               foreach $a (@alias) {
-                       next if $a eq $p;       # ignore if we have it already
-                       my $nref = $pre{$a};
-                       $pre{$a} = $ref if !$nref; # copy the original ref if new 
+if (open(IN, "$prefix/cty.dat")) {
+       my $state = 0;
+       while (<IN>) {
+               $line++;
+               s/\r$//;
+               next if /^\s*\#/;
+               next if /^\s*$/;
+               chomp;
+               if ($state == 0) {
+                       s/:$//;
+                       @f = split /:\s+/;
+                       @a = ();
+                       $state = 1;
+               } elsif ($state == 1) {
+                       s/^\s+//;
+                       if (/;$/) {
+                               $state = 0;
+                               s/[,;]$//;
+                               push @a, split /\s*,/;
+                               $f[7] =~ s/^\*\s*//;   # remove any preceeding '*' before a callsign
+                               ct($_, uc $f[7], @a) if @a;
+                       } else {
+                               s/,$//;
+                               push @a, split /\s*,/;
+                       }
                }
-       } else {
-               print "line $line: unknown prefix '$p' on $l in rsgb.cty\n";
        }
 }
+close IN;
 
-open(OUT, ">$main::data/prefix_data.pl") or die "Can't open $main::data/prefix_data.pl ($!)";
+
+open(OUT, ">$prefix/prefix_data.pl") or die "Can't open $prefix/prefix_data.pl ($!)";
 
 print OUT "\%pre = (\n";
 foreach my $k (sort keys %pre) {
@@ -169,6 +191,34 @@ sub printpre
        return $out;
 }
 
+sub ct
+{
+       my $l = shift;
+       my $p = shift; 
+       my @a = @_;
+       my $ref = $pre{$p};
+       if ($ref) {
+               my $a;
+               foreach $a (@a) {
+                       # for now remove (nn) [nn]
+                       my ($itu) = $a =~ /(\(\d+\))/; $a =~ s/(\(\d+\))//g;
+                       my ($cq) = $a =~ /(\[\d+\])/; $a =~ s/(\[\d+\])//g;
+                       my ($lat, $long) = $a =~ m{(<[-+\d.]+/[-+\d.]+>)}; $a =~ s{(<[-+\d.]+/[-+\d.]+>)}{}g;
+                       my ($cont) = $a =~ /(\{[A-Z]{2}\})/; $a =~ s/(\{[A-Z]{2}\})//g;
+
+                       unless ($a) {
+                               print "line $line: blank prefix on $l in cty.dat\n";
+                               next;
+                       }
+                       next if $a eq $p;       # ignore if we have it already
+                       my $nref = $pre{$a};
+                       $pre{$a} = $ref if !$nref; # copy the original ref if new 
+               }
+       } else {
+               print "line $line: unknown prefix '$p' on $l in cty.dat\n";
+       }
+}
+
 sub addloc
 {
        my $locstr = shift;
@@ -177,3 +227,4 @@ sub addloc
        $locn{$loc} = $locstr;
        return $loc;
 }
+