#!/usr/bin/env bash

#  Using a callsign entered as the param on the command line, or entered at our prompt,
#  look it up on a callsign server out on the web.  (Which changes now and then.)
#  Extract the name, address, and license data.
#  Continue until "73", "qrt", "quit", or "exit" are entered. ( Or, ^C. HI!HI! )

#  Optionally, a second command line parameter of "1" (and only "1") will
#  run the program _once_ and exit.  Any other parm 2 ($2) is ignored.

#  Written to fetch/verify callsigns, name, addresses, and license data.
#  It only supports U.S. callsigns.

#  Requires:  bash, clear, curl, cut, expr, (e)grep, printf, sed
#                      --  possible version and posix sensitivities not known.
#
#  Worked and re-worked to be portable between linux and freeBSD.  Ergo, some
#  of the 'awkwardness' seen in the use of external or internal commands.
#
#  16-Jul-20...Frist light.
#
#     Copyright 2020 Marvin L. Jones
#
#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>
#
#  20-Feb-23  Once again, a new web-based callsign server had to be found.
#
#  Jonesy W3DHJ    email found @ http://W3DHJ.net/    Last Update: 20-Feb-23
#  Copyright 2023 Marvin L. Jones


#  Define text values for 'things' external to the script, and -- so -- which
#  may/will require fiddling with when those external 'things' change:

                                           # U.S. callsign server example
CALL_SRVR="https://callook.info/W1AW/text"
                                           # U.S. callsign server's no hit response.
CALL_ERR="Invalid callsign!"

#UA='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/81.0.4044.138 Chrome/81.0.4044.138 Safari/537.36'
                                           # A UserAgent for `curl` or `lynx` to use.


function callsign_check ()
                        {
                         CALL2=${CALL1^^}         # Uppercase it.

                         # Look for egress request.
                         [  "$CALL2" = "73"      \
                            -o "$CALL2" = "QRT"  \
                            -o "$CALL2" = "QUIT" \
                            -o "$CALL2" = "EXIT" ] && printf "\n" && exit 0

                         # First strip any "/x" suffix. (e.g., /r , /M , /p)
                         CALL2=${CALL2%%/*}

                         # Next locate (1st) numeral position.
                         POSN=$( expr "$CALL2" : "[^0-9]*[0-9]" )

                         # And, extract all non-alpha: Ergo district numeral only(?)
                         DISTRICT=${CALL2//[A-Z]/}

                         # 1: multiple numerals (or other crap):
                         [ ${#DISTRICT} -gt 1 ] && return 1

                         # And, insure the single non-alpha is numeric:
                         DISTRICT=${DISTRICT//[0-9]/}
                         # 2: Non-numeric in 'supposed' numeral position:
                         [ ${#DISTRICT} -gt 0 ] && return 2

                         # 4 Numeral not in position 2 or 3:
                         [ "$POSN" -ne 2 -a "$POSN" -ne 3 ] && return 4

                         # Finally, since we are assured it's only alphnumeric,
                         # double check the suffix length.
                         WORK=${CALL2:$POSN}                   # Extract the suffix
                         # 8: Invalid suffix length
                         [ ${#WORK} -lt 1 -o ${#WORK} -gt 3 ] && return 8

                         return 0
                        }

PARM="$1"                                   # Get callsign - if any...

[ "$2" != "1" ] && clear && printf "\n\n\n" # Prep the 'display'.

while [ 1 ]
 do

  if [ -z $PARM ]; then
    read -p " Enter Callsign: " CALL1       # Prompt for 'raw' callsign.
    CALL1=$( echo $CALL1 | cut -d " " -f1 ) # Strip off on supurios crap.
  else
    CALL1="$PARM"
    printf " Doing Callsign: $CALL1\n\n"    # Callsign on command line.
    PARM=''                                 # null out cmnd line callsign
  fi

  printf " "                                # Park cursor under prompt.

  callsign_check                            # Call with 'raw' $CALL1;
  RC=$?                                     # return with 'good' $CALL2
  if [ $RC -ne 0 ]; then
                                            # Invalid/Garbage callsign
     [ "$2" != "1" ]
     printf "\n $CALL1\n       Invalid callsign!! (rc=$RC)\n\n"

  else

     ADR=$( curl "https://callook.info/$CALL2/text" 2> /dev/null )

     IFS=$'\n'                                 # Newline is our separator.
                                               # Strip unwanted details.
     ADR2=$( echo "$ADR" | egrep -v '(ype|ess|ude|ion):' )

     [ "$2" != "1" ] && clear
     printf "\n$ADR2\n\n"                   # Display results and

     unset IFS
                                            #  double space to prompt for next.
  fi

  [ "$2" = "1" ] && exit 0                  # Exit after 1 time if parm2 == "1".

 done
