#!/bin/bash

#11/21/2010
#dranch@trinnet

# 10/02/11 - added MT63 support
#
# 11/21/10 - added SSB phone support
#
# 11/13/10 - greatly compacted the code using eval
#          - added band QSO counts
#
# 11/06/10 - original version


DEBUG=1

#Any new modes need to be added to the unaccounted list below as well
MODES="CONTESTI CW DATA DOMINO HELL JT65 MFSK16 MT63 OLIVIA PHONE PSK31 PSK63 PSK125 QPSK63 RTTY SSB SSTV THRB THRBX"

BANDS="6m 10m 12m 15m 17m 20m 30m 40m 60m 80m 160m"

CURRFILE="logbook.adif.0"


echo -e "\nFldigi logbook.adif logbook analysis for user: $USER"

#Mainline
#-------------------------------------------------------------------------


if [ ! -d $HOME/.fldigi/logs ]; then
   echo -e "\nERROR: directory $HOME/.fldigi/logs does not exist"
   echo -e "\nAborting\n\n"
   exit 1
fi

cd $HOME/.fldigi/logs

cp logbook.adif $CURRFILE

# Initalize needed global vars
count6m=0
count10m=0
count12m=0
count15m=0
count17m=0
count20m=0
count30m=0
count40m=0
count60m=0
count80m=0
count160m=0

function BANDBREAKDOWN {
  echo -e $COUNTBYMODE
  case $COUNTBYMODE in
    6m) count6m=$(($count6m+1))
    ;;
    10m) count10m=$(($count10m+1))
    ;;
    12m) count12m=$(($count12m+1))
    ;;
    15m) count15m=$(($count15m+1))
    ;;
    17m) count17m=$(($count17m+1))
    ;;
    20m) count20m=$(($count20m+1))
         echo "ding"
    ;;
    30m) count30m=$(($count30m+1))
    ;;
    40m) count40m=$(($count40m+1))
    ;;
    60m) count60m=$(($count60m+1))
    ;;
    80m) count80m=$(($count80m+1))
    ;;
    160m) count160m=$(($count160m+1))
    ;;
  esac
  
  echo -en "\nMode Counts: "
  for B in $BANDS; do
    echo -en "[$B] "
  done
  for B in $BANDS; do
    eval echo -en "\$count${B} "
  done
  echo " "
}

if [ $DEBUG -eq 1 ]; then
   TESTFILE1="logbook.adif-testfile1"
   TESTFILE2="logbook.adif-testfile2"

   BRUTECOUNT=0
   cp logbook.adif $TESTFILE1

   #echo -en "MODES: "
   #for M in $MODES; do
   #   echo -en "[$M] "
   #done
   #for M in $MODES; do
   #done

   #Notes:
   #  Convert PHONE and Phone to SSB
   #  Convert THRBX to DATA
   #  Convert QPSK63 to DATA

   #Strip out header crap
   echo -e "\nBrute force check:"
   grep -v -e "File: logbook.adif" -e "ADIF_VER:5>" -e "PROGRAMID:6>" \
-e "PROGRAMVERSION:7>" -e "DATA CHECKSUM:4>" -e "EOH>" $TESTFILE1 > $TESTFILE2

   BRUTECOUNT="`grep "MODE:8>CONTESTI" $TESTFILE2 | wc --lines`" 
   echo -e "CONTESTI: `grep "MODE:8>CONTESTI" $TESTFILE2 | wc --lines`" 
   grep -v "MODE:8>CONTESTI" $TESTFILE2 > $TESTFILE1
   
   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:2>CW" $TESTFILE1 | wc --lines`))
   echo -e "CW      : `grep "MODE:2>CW" $TESTFILE1 | wc --lines`" 
   grep -v "MODE:2>CW" $TESTFILE1 > $TESTFILE2

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:4>DATA" $TESTFILE1 | wc --lines`))
   echo -e "DATA    : `grep "MODE:4>DATA" $TESTFILE2 | wc --lines`" 
   grep -v "MODE:4>DATA" $TESTFILE2 > $TESTFILE1

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:6>DOMINO" $TESTFILE1 | wc --lines`))
   echo -e "DOMINO  : `grep "MODE:6>DOMINO" $TESTFILE1 | wc --lines`" 
   grep -v "MODE:6>DOMINO" $TESTFILE1 > $TESTFILE2

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:4>HELL" $TESTFILE1 | wc --lines`))
   echo -e "HELL    : `grep "MODE:4>HELL" $TESTFILE2 | wc --lines`" 
   grep -v "MODE:4>HELL" $TESTFILE2 > $TESTFILE1

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:4>JT65" $TESTFILE1 | wc --lines`))
   echo -e "JT65    : `grep "MODE:4>JT65" $TESTFILE1 | wc --lines`" 
   grep -v "MODE:4>JT65" $TESTFILE1 > $TESTFILE2

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:6>MFSK16" $TESTFILE1 | wc --lines`))
   echo -e "MFSK16  : `grep "MODE:6>MFSK16" $TESTFILE2 | wc --lines`" 
   grep -v "MODE:6>MFSK16" $TESTFILE2 > $TESTFILE1

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:4>MT63" $TESTFILE1 | wc --lines`))
   echo -e "MT63    : `grep "MODE:4>MT63" $TESTFILE1 | wc --lines`" 
   grep -v "MODE:4>MT63" $TESTFILE1 > $TESTFILE2

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:6>OLIVIA" $TESTFILE1 | wc --lines`))
   echo -e "OLIVIA  : `grep "MODE:6>OLIVIA" $TESTFILE2 | wc --lines`" 
   grep -v "MODE:6>OLIVIA" $TESTFILE2 > $TESTFILE1

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:5>PHONE" $TESTFILE1 | wc --lines`))
   echo -e "PHONE   : `grep "MODE:5>PHONE" $TESTFILE1 | wc --lines`" 
   grep -v "MODE:5>PHONE" $TESTFILE1 > $TESTFILE2
 
   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:5>PSK31" $TESTFILE1 | wc --lines`))
   echo -e "PSK31   : `grep "MODE:5>PSK31" $TESTFILE2 | wc --lines`" 
   grep -v "MODE:5>PSK31" $TESTFILE2 > $TESTFILE1

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:5>PSK63" $TESTFILE1 | wc --lines`))
   echo -e "PSK63   : `grep "MODE:5>PSK63" $TESTFILE1 | wc --lines`" 
   grep -v "MODE:5>PSK63" $TESTFILE1 > $TESTFILE2

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:6>PSK125" $TESTFILE1 | wc --lines`))
   echo -e "PSK125  : `grep "MODE:6>PSK125" $TESTFILE2 | wc --lines`" 
   grep -v "MODE:6>PSK125" $TESTFILE2 > $TESTFILE1

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:6>QPSK63" $TESTFILE1 | wc --lines`))
   echo -e "QPSK63  : `grep "MODE:6>QPSK63" $TESTFILE1 | wc --lines` -- INVALID to LOTW" 
   grep -v "MODE:6>QPSK63" $TESTFILE1 > $TESTFILE2

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:4>RTTY" $TESTFILE1 | wc --lines`))
   echo -e "RTTY    : `grep "MODE:4>RTTY" $TESTFILE2 | wc --lines`" 
   grep -v "MODE:4>RTTY" $TESTFILE2 > $TESTFILE1

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:3>SSB" $TESTFILE1 | wc --lines`))
   echo -e "SSB     : `grep "MODE:3>SSB" $TESTFILE1 | wc --lines`" 
   grep -v "MODE:3>SSB" $TESTFILE1 > $TESTFILE2

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:4>SSTV" $TESTFILE1 | wc --lines`))
   echo -e "SSTV    : `grep "MODE:4>SSTV" $TESTFILE2 | wc --lines`" 
   grep -v "MODE:4>SSTV" $TESTFILE2 > $TESTFILE1

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:4>THRB" $TESTFILE1 | wc --lines`))
   echo -e "THRB    : `grep "MODE:4>THRB" $TESTFILE1 | wc --lines`" 
   grep -v "MODE:4>THRB" $TESTFILE1 > $TESTFILE2

   BRUTECOUNT=$(($BRUTECOUNT+`grep "MODE:4>THRBX" $TESTFILE1 | wc --lines`))
   echo -e "THRBX   : `grep "MODE:4>THRBX" $TESTFILE2 | wc --lines` -- INVALID TO LOTW" 
   grep -v "MODE:4>THRB" $TESTFILE2 > $TESTFILE1

   echo -e "\nBRUTE COUNT: $BRUTECOUNT"
   echo -e "Check output from Fldigi STDOUT when loading ADIF to confirm"

   echo -e "\nRemaining contents:\n-------------------"
   #cat $TESTFILE1 | grep -v "<EOR>"
   cat $TESTFILE1 | grep "MODE"
fi
echo -en "\nInitial QSO count: "
grep FREQ $CURRFILE | wc --lines

echo -e " "


TOTALCNT=0
PERBANDCNT=0
COUNTSTRING=COUNT
echo -en "\nMode Counts: "
for B in $BANDS; do
  echo -en "[$B] "
done
echo -e "\n------------"
for M in $MODES; do
   #eval echo $M$COUNTSTRING
   eval $M$COUNTSTRING="`grep -i -e \>$M $CURRFILE | grep BAND | awk -F MODE \
'{print $2}' | awk -F '>' '{print $2}' | awk -F '<' '{print $1}' | wc --lines`"

   #This is incomplete.. fix it
   export BANDSBYMODE="`grep -i -e \>$M $CURRFILE | grep BAND | awk -F MODE '{print $2}' | awk -F ":3>" '{print $2}' | awk -F "<" '{print $1}'`"
   #echo "MODE:$M - Band: $COUNTBYMODE"
   #BANDBREAKDOWN 
#eval TOTALCNT=$((TOTALCNT+\$${M}${COUNTSTRING}
   eval echo -e " $M: \$${M}${COUNTSTRING}"
   TOTALCNT=$(($TOTALCNT+`eval echo \${M}${COUNTSTRING}`))
done

echo -e "\nUnaccounted modes:\n----------------\n `cat $CURRFILE | grep ^\<BAND | grep -iv -e CONTESTI -e \>CW\< -e DOMEX8 -e DOMINO -e HELL -e JT65 -e MFSK16 -e MT63 -e OLIVIA -e PHONE -e PSK31 -e PSK63 -e PSK125 -e RTTY -e SSB -e SSTV -e THRB -e TX_PWR`"

echo -e "\nTotal count of QSOs by mode: $TOTALCNT"

#<FREQ:8>7.071757<CALL:6>WA5ETK<MODE:5>PSK31<NAME:4>Gene<QSO_DATE:8>20090913<QSO_DATE_OFF:8>20090913<TIME_OFF:4>0238<TIME_ON:4>0225
#<QTH:8>Amarillo<STATE:2>TX<NOTES:35>Runs a packet station on 7086.5 USB<BAND:3>40m<TX_PWR:3>100<EOR>


BANDSTRING=BAND
K=0
echo -e "\n\nQSOs by Band Count:\n-------------------"
for K in $BANDS; do
   #eval $BANDSTRING$K="`awk -F ">" '{print $2}' logbook.adif.0 | awk -F "<CALL" '{print $1}' | grep -c $K`"
   #eval $BANDSTRING$K="`awk -F "BAND:3>" '{print $2}' logbook.adif.0 | awk -F \< '{print $1}' | grep -v '^$' | grep -c $K`"

   #Fix 160m but this doesn't stop from matching 60m within 160m which is a problem!
   eval $BANDSTRING$K="`awk -F "BAND:" '{print $2}' logbook.adif.0 | awk -F ">" '{print $2}' | awk -F \< '{print $1}' | grep -v '^$' | grep -c $K`"
   eval echo -e " $K: \$${BANDSTRING}${K}"
   TOTALBANDCNT=$(($TOTALBANDCNT+`eval echo ${BANDSTRING}${K}`))
done

echo -e "\nTotal QSOs count by band: $TOTALBANDCNT"

echo -e "\nDone"

