#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

echo Starting

FILES=./*.html
for f in $FILES

do

REMOTE=`grep -o "Skype chat with .*<" $f | sed 's/Skype chat with //' | tr -d '<'`
LOCAL="You"

echo The chatting party is: $REMOTE
echo The local party is: $LOCAL

echo Getting message metadata - timestamps
cat $f | awk "NR>`grep -n '<table id=\"content_table"' $f|grep -o [0-9]*`" | grep "td class" | grep timestamp |  grep -o "[0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*:[0-9]*" | sed  's/[:alnum:-]/ /g' | awk '{print "[" $3"."$2"."$1" " $4 ":" $5 ":" $6"]"}' > timestamps

echo Getting message metadata - talking party

cat $f | awk "NR>`grep -n '<table id=\"content_table"' $f|grep -o [0-9]*`" | grep "td class" | grep author | sed "s/.*local.*/${LOCAL}\:/;s/.*remote.*/${REMOTE}\:/" > talkingparty

echo Getting message data - this may take a long time

cat $f |  awk "NR>`grep -n '<table id=\"content_table"' $f|grep -o [0-9]*`" | sed -n '/<div>/,/<\/div>/p' | grep -v "<\/div><\/td>"| grep -v "<td class=\"message_content\"><div>" > messages

echo Converting message data - workaround for actions

grep -vE "span class=\"[rl][eo][mc][oa][tl]e*\"" messages > messages_fixed
cat messages_fixed > messages
rm -f messages_fixed

echo Checking event count - the three values must be equal
echo `wc -l timestamps`
echo `wc -l talkingparty`
echo `wc -l messages`

echo Writing header

echo "###" > ${f}_output.txt
echo "### Full History Log" >> ${f}_output.txt
echo "### $LOCAL ($LOCAL: $LOCAL) - $REMOTE ($REMOTE: $REMOTE)" >> ${f}_output.txt
echo "### (generated by history++ plugin)"  >> ${f}_output.txt
echo "###"  >> ${f}_output.txt
printf "\n" >> ${f}_output.txt

EVENTS=`wc -l timestamps|grep -o [0-9]*`

echo Writing chat history

while true; do
read -r lineA <&3
read -r lineB <&4
read -r lineC <&5
if [ -z "$lineA" -o -z "$lineB" ]; then
break
fi
echo $lineA $lineB >> ${f}_output.txt
#Remove formatting, IFS workaround
echo $lineC | sed -r 's/^[[:space:]]{2,4}//g' | sed -re $'s/<br \/>/\\\n/g'|sed -e 's/<[^>]*>//g'>> ${f}_output.txt
printf "\n" >> ${f}_output.txt
done 3<timestamps 4<talkingparty 5<messages


echo Converting message data - workaround for html ltgt
sed -i 's/\&lt;/</g' ${f}_output.txt
sed -i 's/\&gt;/>/g' ${f}_output.txt


echo Final format conversion
#I am so sorry for the lines below, plz don't judge :D

unix2dos ${f}_output.txt
iconv -f UTF-8 -t UTF-16 ${f}_output.txt -o ${f}_tmp.txt
rm -f ${f}_output.txt
mv ${f}_tmp.txt ${f}_output.txt

echo Cleanup
rm -fv messages timestamps talkingparty
done

echo Conversion Complete

exit 0