#!/bin/sh

set -eux

DIR=${1:-/usr/share/hamradio-files}

: %%%%% Retrieving VERSION entity from history.txt %%%%%

VERSION=$(sed -ne 's/.*Version entity is \(.*\),.*/\1/p' bigcty/history.txt)
[ "$VERSION" ]

: %%%%% Checking cty.dat %%%%%

# Monaco:                   14:  27:  EU:   43.73:    -7.40:    -1.0:  3A:
#     3A,=3A/4Z5KJ/LH;

cty_dat_lookup ()
{
  pfx="$1"
  # look for prefix, and output the last line before that matching ^xxx:
  sed -ne "1,/\\b$pfx\\b/p" $DIR/cty.dat | egrep -o '^[A-Z][^:]+' | tail -1
}

[ "$(cty_dat_lookup DF)" = "Fed. Rep. of Germany" ]
[ "$(cty_dat_lookup VERSION)" = "$VERSION" ]

: %%%%% Checking cty.csv %%%%%

# 3A,Monaco,260,EU,14,27,43.73,-7.40,-1.0,3A =3A/4Z5KJ/LH;

cty_csv_lookup ()
{
  pfx="$1"
  egrep "\\b$pfx\\b" $DIR/cty.csv | cut -d ',' -f 2
}

[ "$(cty_csv_lookup DF)" = "Fed. Rep. of Germany" ]
[ "$(cty_csv_lookup VERSION)" = "$VERSION" ]

: "Check cty.csv line length"
: "If this becomes too large, xdx and xlog need to be patched"
: "(See #1025276 and #1025290)"
line_length=$(wc --max-line-length bigcty/cty.csv | cut -d ' ' -f 1)
[ "$line_length" -le 120000 ]
