#!/bin/bash
if [ "$#" -lt 2 ]; then echo "${0} [-v] encrypt.log decrypt.log"; exit 1; fi

export let iden=0;
export let diff=0;

if [ ${1} = "-v" ];
then
  export verbose=1;
  export log1="${2}";
  export log2="${3}";
else
  export verbose=0;
  export log1="${1}";
  export log2="${2}";
fi
> /tmp/idenlog.txt
> /tmp/difflog.txt

function log     { echo -e "${1}"; }
function idenlog { echo -e "${1}" >> /tmp/idenlog.txt; if [ ${verbose} -eq 1 ]; then echo -e "${1}"; fi }
function difflog { echo -e "${1}" >> /tmp/difflog.txt; echo -e "${1}"; }

export let line=1;
export let maxline=`grep -E "^EN|DN" "${log1}" | wc -l`;

export let log1encrypt=`grep "Started encrypting" ${log1} | wc -l`;
export let log1decrypt=`grep "Started decrypting" ${log1} | wc -l`;
export let log2encrypt=`grep "Started encrypting" ${log2} | wc -l`;
export let log2decrypt=`grep "Started decrypting" ${log2} | wc -l`;

echo;
log "Comparing: ${log1} `if [ ${log1encrypt} -eq 1 ]; then log "(encrypting)"; fi``if [ ${log1decrypt} -eq 1 ]; then log "(decrypting)"; fi` with ${log2} `if [ ${log2encrypt} -eq 1 ]; then log "(encrypting)"; fi``if [ ${log2decrypt} -eq 1 ]; then log "(decrypting)"; fi`...\r"
echo;

grep -E "^EN|DN" "${log1}" | while IFS='"' read log1_a log1_file log1_c log1_sum1 log1_e log1_sum2 log1_g;
do
    export log1_line="${log1_a} | ${log1_file} | ${log1_c} | ${log1_sum1} | ${log1_e} | ${log1_sum2}";
    if [ ${verbose} -eq 0 ]; then echo -ne "\r${line}/${maxline}"; else echo "${line}/${maxline}"; fi

    grep -E "\"${log1_file}\"|\"${log1_file}.bit\"|\"${log1_file%.bit}\"" "${log2}" | while IFS='"' read log2_a log2_file log2_c log2_sum1 log2_e log2_sum2 log2_g;
    do
        export log2_line="${log2_a} | ${log2_file} | ${log2_c} | ${log2_sum1} | ${log2_e} | ${log2_sum2}";

        if      [ ${log1encrypt} -eq 0 ] && [ ${log1decrypt} -eq 1 ] && [ ${log2encrypt} -eq 0 ] && [ ${log2decrypt} -eq 1 ] # decrypt decrypt
        then
          if [ "${log1_sum2}" = "${log2_sum2}" ]
          then
            idenlog "X 1 IDEN1 ${log1_line}"; idenlog "X 1 IDEN2 ${log2_line}"; idenlog "";
          else
            difflog "X 1 DIFF1 ${log1_line}"; difflog "X 1 DIFF2 ${log2_line}"; difflog "";
          fi
        elif    [ ${log1encrypt} -eq 0 ] && [ ${log1decrypt} -eq 1 ] && [ ${log2encrypt} -eq 1 ] && [ ${log2decrypt} -eq 0 ] # decrypt encrypt
        then
          if [ "${log1_sum2}" = "${log2_sum1}" ]
          then
            idenlog "X 1 IDEN1 ${log1_line}"; idenlog "1 X IDEN2 ${log2_line}"; idenlog "";
          else
            difflog "X 1 DIFF1 ${log1_line}"; difflog "1 X DIFF2 ${log2_line}"; difflog "";
          fi
        elif    [[ ${log1encrypt} -eq 1 ]] && [[ ${log1decrypt} -eq 0 ]] && [[ ${log2encrypt} -eq 0 ]] && [[ ${log2decrypt} -eq 1 ]] # encrypt decrypt
        then
          if [[ "${log1_sum1}" = "${log2_sum2}" ]]
          then
            idenlog "1 X IDEN1 ${log1_line}"; idenlog "X 1 IDEN2 ${log2_line}"; idenlog "";
          else
            difflog "1 X DIFF1 ${log1_line}"; difflog "X 1 DIFF2 ${log2_line}"; difflog "";
          fi
        elif    [ ${log1encrypt} -eq 1 ] && [ ${log1decrypt} -eq 0 ] && [ ${log2encrypt} -eq 1 ] && [ ${log2decrypt} -eq 0 ] # encrypt encrypt
        then
          if [ "${log1_sum1}" = "${log2_sum1}" ]
          then
            idenlog "1 X IDEN1 ${log1_line}"; idenlog "1 X IDEN2 ${log2_line}"; idenlog "";
          else
            difflog "1 X DIFF1 ${log1_line}"; difflog "1 X DIFF2 ${log2_line}"; difflog "";
          fi
        else
          echo "Logfiles containing invalid modes"
          exit 1;
        fi

        break 2;
    done
    # difflog "1 X DIFF1 ${log1_line}"; difflog "1 X DIFF2 ${log2_line}"; difflog "";

#    echo "-------------------------------------------------------------------------------------------------------------------------------"
    line=$((${line}+1));
done

#echo -ne "\r                                             "
#echo "==============================================================================================================================="
if [ ${verbose} -eq 0 ] && [ ${line} -lt ${maxline} ]; then echo -ne "\r                                                                                    "; fi
echo
echo "Finished comparison"
echo;
echo "Results: Identical: $((`wc -l  /tmp/idenlog.txt | awk '{ print $1 }'`/3)) Different: $((`wc -l  /tmp/difflog.txt | awk '{ print $1 }'`/3))"
echo;
