#! /bin/sh
#
# getxy: extract lat and long from usenet maps, to prepare a hostname file
#	for the "netmap" program.
#
#
# usage: cd /usr/local/lib/news/maps; cat {d,u}.[a-z]* | getxy > mapXYdata
#
#  Brian Reid, as part of netmap, August 1986
#	revised to cope with d.* files, April 1988
#
PATH=:/usr/local/bin:/usr/bin:/usr/ucb:/bin:

egrep '^#N|^#L|^#U|^$|^[0-9a-zA-Z]' $1 |\
	sed -e 's/	/ /g' \
	    -e '/^#N/s/,/ /g' |\
awk '
BEGIN {Nflag=0; Lfield=""; Ufield=""; ncount=0}
/^#N/ { Nflag=1
	Ufield="";
	Lfield="";
	if (NF>1) {
	   for (j=2; j<=NF; j++) {
		ncount++;
		names[ncount] = $j;
	   }
	}
       }

/^#L/ {if (NF>1) {
	Lfield="";
	for (i=2;i<=NF;i++) {
	    if ($i != "city") Lfield=Lfield " " $i;
	}
       }
      }
/^#U/ {if (NF>1) {
	if (length(Ufield)>128) {
	   for (j=1; j<=ncount; j++) {
	    printf "%-13s%-27s%s\n", names[j] " H",Lfield,Ufield
	   }
	   Ufield=""
	}
	for (i=2;i<=NF;i++) {Ufield=Ufield " " $i " U";}
       }
      }

/^#/  {next}

$2 == "=" {
	    if (ncount == 0) {next;}
	    for (j=1; j<=ncount; j++) {
	       if ($1 == names[j]) {
	           for (k=3; k<=NF; k++) {
		      thisField = $k
		      if (substr(thisField,length(thisField)-1,1)==",") {
		          thisField=substr(thisField,0,length(thisField)-1);
		      }
		      ncount++;
		      names[ncount] = thisField;
		      next;
		   }
	       }
	    }
	}

NF == 2 && $2 == names[1]	{
	if (Nflag == 0) next;
	ncount++;
	names[ncount] = $1
	next
	}


/^$/ {if (Nflag!=0) {
	for (j=1; j<=ncount; j++) {
	 printf "%-13s%-27s%s\n", names[j] " H",Lfield,Ufield
	}
      }
      Lfield=""; Ufield=""; name=""; Nflag = 0; ncount=0;
     }
' | sort -f +0 -1 | \
    sed -e 's/West/W/g' \
	-e 's/North/N/g' \
	-e 's/South/S/g' \
	-e 's/East/E/g' \
	-e 's/\([0-9]\)\([NSEW]\) /\1 \2 /g' \
	-e 's/ *(.*)//' \
	-e 's/,/ /g'  -e 's/://g' \
	-e 's/ \. / /g' \
	-e 's/\([0-9]\)\.\([0-9]\)/\1 \2/g' \
	-e 's/[Ll][Aa][Tt]\.*//g' \
	-e 's/[Ll][Oo][Nn][Gg]\.*//g' \
	-e 's/[Dd][Ee][Gg][Rr][Ee][Ee][Ss]//g' \
	-e 's/[Dd][Ee][Gg]//g' \
	-e 's/"//g' \
	-e 's|/| |g' \
	-e "s/\'/ /g" 
