#!/bin/bash

# Synfig Crash Monitor script
# Copyright (c) 2009-2010 Konstantin Dmitriev
#
# This file is part of Synfig.
#
# Synfig is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Synfig is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Synfig.  If not, see <https://www.gnu.org/licenses/>.

set -e
trap writelog INT

init()
{
echo "SynfigStudio Crash Monitor is a tool to collect statistics about synfig crashes."
echo "All information is collected locally in ~/.synfig/cph directory."
echo
STARTED=0
RUNTIME=0
VERSION=''
RELEASE=''
BRANCH=''
REVISION_ID=''
CRASH=0
[ ! -d ~/.synfig/cph ] && mkdir -p ~/.synfig/cph || true

# Detect if crash monitor is already started
PDIR=${0%`basename $0`}
LCK_FILE=~/.synfig/cph/`basename $0`.lck
if [ -f "${LCK_FILE}" ]; then
	MYPID=`head -n 1 "${LCK_FILE}"`
	if ! ( ps -p ${MYPID} | grep ${MYPID} >/dev/null ); then
		# The process is not running
		# Echo current PID into lock file
		echo $$ > "${LCK_FILE}"
	else
		echo "`basename $0` is already running [${MYPID}]. Aborting."
		sleep 5
		exit 0
	fi
else
	# The process is not running
	# Echo current PID into lock file
	echo $$ > "${LCK_FILE}"
fi
echo `date +%H:%M` "Synfig Crash Monitor started."
}

writelog()
{
	if [[ $STARTED != 0 ]]; then
		if [[ $CRASH == 0 ]]; then
			echo `date +%H:%M` "Synfig exited normally. Session time: $RUNTIME."
		else
			echo `date +%H:%M` "Crash detected. Version $VERSION.$RELEASE.$BRANCH, session time: $RUNTIME."
		fi
		if [ -e ~/.synfig/cph/log ]; then
			#check if dump needed
			CURRENTDATE=`date +%Y-%m-%d`
			LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
			LOGMODDATE=${LOGMODDATE%% *}
			if [[ $LOGMODDATE != $CURRENTDATE ]]; then
				dumpstats
			fi
		fi
		#write log
		echo $VERSION/$BRANCH/$RELEASE/$REVISION_ID $RUNTIME $CRASH >> ~/.synfig/cph/log
		CRASH=0
		RUNTIME=0
	else
		echo
	fi
}

dumpstats()
{
	echo `date +%H:%M` 'Dumping stats for previous session...'
	LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
	LOGMODDATE=${LOGMODDATE%% *}
	#get versions
	VERSIONS=''
	while read LINE; do
		FOUND=0
		for VER in $VERSIONS; do
			if [[ $VER == ${LINE%% *} ]]; then
				FOUND=1
				break
			fi
		done
		[[ $FOUND == 0 ]] && VERSIONS="$VERSIONS ${LINE%% *}"
	done < ~/.synfig/cph/log
	echo "   Logged versions: ${VERSIONS}"
	for VER in $VERSIONS; do
		#generating random record ID
		ID=$( echo `date` `ps` | md5sum | md5sum )
		ID="${ID:2:16}"
		#summarizing time and counting crashes
		CRASHCOUNT=0
		TIMECOUNT=0
		while read LINE; do
			if [[ ${LINE%% *} == $VER ]]; then
				TIMECOUNT=`expr $TIMECOUNT + $(echo $LINE| cut -f 2 -d ' ')` || true
				CRASHCOUNT=`expr $CRASHCOUNT + $(echo $LINE| cut -f 3 -d ' ')` || true
			fi
		done < ~/.synfig/cph/log
		echo "   $LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT"
		echo "$LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT" >> ~/.synfig/cph/stats
	done
	rm -f ~/.synfig/cph/log
	echo '   Done.'
}

mainloop()
{
	while true; do
		if ( ps --no-headers -f -Csynfigstudio |egrep "^`whoami`" > /dev/null ) ; then
			#synfigstudio process exist
			if [[ $STARTED == 0 ]]; then
				STARTED=1
				RUNTIME=0
				#get version
 				P=$(ps --no-headers -Csynfigstudio -o uid,cmd |egrep "^ *`id -u`" | egrep -v "<defunct>" | tr -s ' ' | sed "s|^ *`id -u` *||" | sed "s|/bin/sh *||" | head -n 1)
				echo 
				if [ ! -e $P ]; then
					P=`which $P`
				fi
				P=`dirname $P`
				VERSION=`$P/synfig --info|head -n 1|cut -d '-' -f 2`
				RELEASE=`$P/synfig --info|egrep "Revision:"|cut -d ' ' -f 2`
				BRANCH=`$P/synfig --info|egrep "Branch:"|cut -d ' ' -f 2-3`
				REVISION_ID=`$P/synfig --info|egrep "Revision ID:"|cut -d ' ' -f 3`
				if [[ $BRANCH == '(no branch)' ]]; then
					BRANCH=$REVISION_ID
				fi
				echo `date +%H:%M` "Synfig $VERSION.$RELEASE.$BRANCH.$REVISION_ID started."
				echo `date +%H:%M` "Assuming Synfig installed in $P."
			else
				let RUNTIME=$RUNTIME+1
			fi
		else
			#no synfigstudio process exist
			if [[ $STARTED == 1 ]]; then
				#detect crash
				if [ -e ~/.synfig/fifo ]; then
					CRASH=1
				fi
				writelog
				CRASH=0
				STARTED=0
			fi
		fi
		sleep 1
	done
}


init
mainloop
