#!/bin/sh
cd "$(dirname "$0")"
. ./.env
. venv/bin/activate
trap 'echo "Caught Ctrl-C"' INT
while [ 1 ] ; do
    LOGFILE=log-$(date +%Y%m%d-%H%M%S).txt
    python doctor.py logs/$LOGFILE
    if [ $(grep -c "JJ:" logs/$LOGFILE) -eq 0 ]; then
	rm logs/$LOGFILE
    elif dialog --title "Save session log?" --clear --defaultno --yesno "Save $LOGFILE?"  5 40 ; then
	# clear uncaught aborted logs
        grep -c "JJ:" logs/*.txt | grep ':0$'| sed 's/:.*//' | xargs rm
        python genlogs.py
        dialog --title "Session saved" --msgbox "Session saved to file $LOGFILE
You may access it via WWW over LAN" 6 60
    else
        rm logs/$LOGFILE
    fi
done
