#!/usr/bin/perl
# 
# Converts wpxloc.raw to location.dat. This file is used for 
# SHOW/SUN and SHOW/HEADING
#
# Last Change: Thu Apr  9 19:40:58 GMT 1998

$fmt = "%-6s %-32s %3d %3d %1s %3d %3d %1s %5.1f\n";
$home = (getpwnam('clx_us'))[7];
$w = "$home/config/wpxloc.raw";
$l = "$home/config/location.dat";

if ( ! -e $w ) {
	print "$w does not exist.\n";
	exit 1;
}


if ( -e $l ) {
	if ( (stat($l))[9] > (stat($w))[9] ) {
		# $l is newer than $w -> exit quietly
		exit 0;
	}
}

print "Generating new $l file.\n";

open(IN,"$w") || die "Cannot open $w for reading\n";
open(OUT,"| sort > $l") || die "Cannot open $l for writing\n";

while (<IN>) {
	next if /^\!.*/;
	next if /^[\s\t]*$/;
	($pfx_list,$name,$nr,$itu,$waz,$td,
	 $lat_deg,$lat_min,$lat_sec,$ns,
	 $lon_deg,$lon_min,$lon_sec,$ew) = split;
	@prefixes = split (/,/,$pfx_list);
	foreach $i (@prefixes) {
		printf(OUT $fmt,$i,$name,$lat_deg,$lat_min,
			$ns,$lon_deg,$lon_min,$ew,$td);
	}
}

close(OUT);
close(IN);
