#!/bin/bash
###
csvfile="$1"
patfile="$2"


linesincsv="$(wc -l $csvfile)"
wordsincsv="$(wc $csvfile)"
reportfile="./stats/fef/report-$(date "+%Yt%mm%dd").txt"


mkdir ./stats/fef
cp -b $reportfile ./bak/
echo "$csvfile fef analysis ($(date "+%Yt%mm%dd"))" > $reportfile # A Legible summary of the session

echo " --  pre-summary  " >> $reportfile
echo " - lines: $linesincsv " >> $reportfile
echo " - words: $wordsincsv " >> $reportfile
echo " - searching $(jq '. | length' $2) terms" >> $reportfile
echo " --  end pre-summary  " >> $reportfile
#TODO: echo " - oldest, newest comments: " >> $reportfile

queries=$(cat $patfile | jq -r '.[] | @base64')
echo $queries

for query in $queries
do 
    echo "---$query | base64 --decode"

    __jq() {
        echo ${query} | base64 --decode | jq -r ${1}
    }

    name="$(__jq '.name')"
    pattern="$(__jq '.query')"    
    echo "$name :: $pattern"
    #todo : run the grep stuff to a variable instead of grepping 3 separate times
    match="$(cat ./$csvfile | grep -iE "$pattern" )"
    echo "pattern "$name" ("$pattern") matched "$(echo "$match" | wc -l)" out of $linesincsv messages" >> $reportfile
    
    echo "count in board,board" > ./stats/fef/$name-boards.csv
    echo "$match" | csvcut -c 1 | sort | uniq -c | sort -r | sed "s/^ *//g" | sed -r "s/^(\w+) (.+)/\1,\"\2\"/g" >> ./stats/fef/$name-boards.csv
    echo "count in corpus,variant" > ./stats/fef/$name-vars.csv
    echo "$match" | grep -iE -o "$pattern" | tr [[:upper:]] [[:lower:]] | sort | uniq -c | sort -r | sed "s/^ *//g" | sed -r "s/^(\w+) (.+)/\1,\"\2\"/g" >> ./stats/fef/$name-vars.csv
done

echo " -- fef summary:" >> $reportfile
head -n 32 ./stats/fef/*.csv >> $reportfile
echo " -- end fef summary:" >> $reportfile
cat $reportfile