#!/usr/bin/bash
# Last modified by jlc on Jun 28, 2022

usage() {
   cat <<EOF

Usage: `basename $0` [options] <call log> <country code>

Options:
       [-h] [-n]

       -h - show this help

       -n [0123] - North American Number Plan Countries and Territories
          where
              0 = phone number not formatted: 1234567890
              1 = google standard format:     (123) 456-7890
              2 = hyphen format:              123-456-7890 (default)
              3 = dot format:                 123.456.7890

       Produces an updated version of a cidcall.log file on stdout

EOF

   exit 1
}



format_option=" "

# Options on command line
while getopts :hn: opt ; do
    case $opt in
        h) usage;;
        n)  echo $# ;shift 1 ; echo $# ; format_option="-n $OPTARG";;
       \?) echo "Invalid option: -$OPTARG"; usage;;
        *) echo "Invalid option: -$OPTARG"; usage;;
    esac
done  1>&2

echo $0 $1 $2

[ $# != 2 ] && usage

COUNTRY=$2

awk -v COUNTRY="$COUNTRY" -v format_option="$format_option"  '
    BEGIN {FS = "*"}
    {
        if ($12!="FNMBR" && $8=="NMBR" &&
            ($1=="BLK: " || $1=="CID: " || $1=="HUP: " || $1=="NOT: " ||
             $1=="OUT: " || $1=="PID: " || $1=="PUT: " || $1=="RID: " ||
             $1=="WID: " ))
        {
            "ncidnumberinfo " format_option " " $9 " " COUNTRY | \
                getline new_fields;
            close ("ncidnumberinfo " format_option " " $9 " " COUNTRY);
            print $1"*"$2"*"$3"*"$4"*"$5"*"$6"*"$7"*"$8"*"$9"*"$10"*"$(11)new_fields"*"$12"*"$13"*"
        }
        else {print $0}
    }
'  $1

