#!/bin/bash

#This script reviews Flsidi (and maybe other) ADIF logs and tells you
# what entries won't be accepted by either EQSL or LOTW

#10/02/11
#dranch@trinnet.net

#100211 - added support for PSK125R for eqsl and lotw
#112810 - added LOTW change for THRBX

#Setup
DEBUG=0
NEEDEQSLFIX=0
NEEDLOTWFIX=0

if [ $DEBUG -eq 1 ]; then
  echo -e "\nDEBUG:"
  echo -e "  command line: $1"
fi

if [ -z $1 ]; then
  echo -e "\n\nERROR: logfile not specified"
  echo -e "\nabort\n\n"
  exit 1
fi
if [ -n "`echo $1 | grep -i adi | grep -v grep`" ]; then
  echo -e "\n\nERROR: do not specify .adi suffix"
  echo -e "\nabort\n\n"
  exit 1
fi

LOGFILE="$1"
echo -e "\nChecking for issues with eQSL.cc"
echo -e   "--------------------------------"
grep -ie "MFSK11" -e "JT65A" -e "PSK125R" $LOGFILE.adi
if [ -n "`grep -ie "MFSK11" -e "JT65A" -e "PSK125R" $LOGFILE.adi`" ]; then
   NEEDEQSLFIX=1
fi

echo -e "\nChecking for issues with LOTW"
echo -e   "-----------------------------"
grep -ie "Domino" -e "QPSK63" -e "CONTESTI" -e "THRBX" -e "PSK125R" $LOGFILE.adi
if [ -n "`grep -ie "DOMINO" -e "QPSK63" -e "CONTESTI" -e "THRBX" -e "PSK125R" $LOGFILE.adi`" ]; then
   NEEDLOTWFIX=1
fi

# --------------------------------------

if [ "$1" = "logbook.adi" ]; then
   echo -e "\nChecking FLdigi original file only - no cleaning will be done"
  else
   #Cleaning files as needed
   if [ $NEEDEQSLFIX -eq 1 ]; then
      echo -e "\n   ---> cleaning logfile for eQSL - newfile: $1.eqsl.adi"
      cat $1.adi | sed -e "s/7>PSK125R/4>DATA/" > $1.eqsl.adi
   fi

   if [ $NEEDLOTWFIX -eq 1 ]; then
      echo -e "\n   ---> cleaning logfile for LOTW - newfile: $1.lotw.adi"
      cat $1.adi | sed -e "s/6>DOMINO/4>DATA/" -e "s/QPSK63/DATA/" \
-e "s/8>CONTESTI/4>DATA/" -e "s/5>THRBX/4>DATA/" -e "s/7>PSK125R/4>DATA/" > $1.lotw.adi
   fi
fi

echo -e "\ndone\n"
