#!/bin/bash

# Copyright (C) 2011 Pâris Quentin

# This program 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.

# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 

# This script is called when POL/POM runs a program.
[ "$PLAYONLINUX" = "" ] && exit 0
source "$PLAYONLINUX/lib/sources"


debug_menu()
{
	POL_SetupWindow_menu_num "$PACKAGE $(eval_gettext "crashed.") $(eval_gettext "What do you want to do ?")" "$TITLE" "$(eval_gettext "Show me the log file")~$(eval_gettext "Show me the full report")~$(eval_gettext "Send to pastebin")~$(eval_gettext "Nothing")" "~"
	ACTION="$APP_ANSWER"
	
	
	if [ "$ACTION" = "0" ]
	then
		debug_log
	fi
	if [ "$ACTION" = "1" ]
	then
		debug_fulllog
	fi
	if [ "$ACTION" = "2" ]
	then
		contenu="$(cat "$LOGFILE")"
		PACKAGE="$(POL_Website_urlparse "$PACKAGE")"
		contenu="$(POL_Website_urlparse "$contenu")"
		pastebinurl="$(echo "$contenu" | pastebin)"
		POL_Browser "$pastebinurl"
		POL_SetupWindow_message "Pastebin url : $pastebinurl" "$TITLE"
	fi
	if [ "$ACTION" = "3" ]
	then
		POL_SetupWindow_Close
		exit
	fi
	debug_menu
}
pastebin()
{
	INPUT="$(</dev/stdin)"

	querystring="paste_private=0&paste_code=${INPUT}&paste_name=${PACKAGE}&paste_email=&paste_format="

	#post data to pastebin.com API
	curl -d "${querystring}" http://pastebin.com/api_public.php
}
debug_log()
{
	POL_SetupWindow_file "Log file" "$TITLE" "$LOGFILE.e"
}
debug_fulllog()
{
	POL_SetupWindow_file "Log file" "$TITLE" "$LOGFILE"
}
debug_sendtopom()
{
	[ "$POL_OS" == "Linux" ] && TRACKER="1"
	[ "$POL_OS" == "Mac" ] && TRACKER="4"
	
	POL_SetupWindow_textbox_multiline "$(eval_gettext "Please enter everything that might help us in english.\nIf you are not accurate, your bug will be closed")" "$TITLE"
	[ "$APP_ANSWER" = "" ] && POL_SetupWindow_message "$(eval_gettext "Please enter a description in english.\nIf you are not accurate, your bug will be closed")" "$TITLE" && POL_SetupWindow_Close
	echo "$APP_ANSWER" >> "$LOGFILE"
	POL_Website_login
	POL_Website_Init
	contenu="$(cat "$LOGFILE")"
	PACKAGE="$(POL_Website_urlparse "$PACKAGE")"
	contenu="$(POL_Website_urlparse "$contenu")"
	POL_Website_Request "http://bugs.playonlinux.com/en/new-$TRACKER.html" "priority=2&tracker=$TRACKER&title=$PACKAGE crashed while running&message=$contenu&submit=Send"
	POL_Website_Close
	POL_Website_logout
	POL_SetupWindow_message "$(eval_gettext "Thank you !\n\nYour report has been sent.\n\nWe will do our best to send you an answer very soon by mail\n\n")" "$TITLE"
	
	POL_SetupWindow_Close
	exit
	#POL_Website_Login
}
TITLE="$APPLICATION_TITLE"


if [ "$1" ]
then
PACKAGE="$1"
else
POL_SetupWindow_Init
POL_SetupWindow_games "$(eval_gettext "Please choose a software to run")" "$TITLE"
PACKAGE="$APP_ANSWER"
POL_SetupWindow_Close
fi

cd "$REPERTOIRE/shortcuts"
LOGFILE="$REPERTOIRE/tmp/errors_$PACKAGE.log"
echo "[b]File :[/b]$PACKAGE" > "$LOGFILE"
echo "[b]Date :[/b] $(Get_CurrentDate)" >> "$LOGFILE"
echo "[b]$APPLICATION_TITLE Version :[/b] $VERSION" >> "$LOGFILE"
echo "[b]File content :[/b]" >> "$LOGFILE"
echo "[code language%3Dplayonlinux]" >> "$LOGFILE"
cat "$PACKAGE" >> "$LOGFILE"
echo "[/code]" >> "$LOGFILE"
debugFile="$(cat "$PACKAGE" | grep "#POL_Log" | cut -d= -f2)"
if [ ! "$debugFile" = "" ]
then
	echo "[b]Install output $debugFile:[/b]" >> "$LOGFILE"
	
	cat "$POL_USER_ROOT/logs/$debugFile/$debugFile.log" >> "$LOGFILE"
fi
echo "[b]Output :[/b]" >> "$LOGFILE"
rm "$LOGFILE.e" 2> /dev/null
touch "$LOGFILE.e"

if [ "$(cat "$PACKAGE" | grep 'export WINEDEBUG="-all"')" = "" ]
then
	POL_Debug_Message "Debugger is starting"
	xterm -T "$(eval_gettext "$APPLICATION_TITLE debugger")" -e "echo \"Writting log to $LOGFILE.e\" && echo \"\" && sleep 1 && tail -f \"$LOGFILE.e\"" &
fi


$(POL_Config_Read PRE_RUNNING)
bash "$PACKAGE" 2>> "$LOGFILE.e" >> "$LOGFILE.e"

if [ ! "$?" = "0" ]
then 


	POL_SetupWindow_Init
	POL_SetupWindow_wait_next_signal "$(eval_gettext '$APPLICATION_TITLE debugger is starting...')" "$TITLE"
	if [ ! "$(cat "$PACKAGE" | grep 'export WINEDEBUG="-all"')" = "" ]
	then
		POL_SetupWindow_message "$(eval_gettext "The program you run crashed.\n\nYou need to enable debugging to get more details.\nPlease go to configure menu")" "$TITLE"
	else
		echo "[code language%3Dconsole]" >> "$LOGFILE"
		cat "$LOGFILE.e" >> "$LOGFILE"
		echo " [/code]" >> "$LOGFILE"

		echo "[b]Other :[/b]" >> "$LOGFILE"
	
		sleep 2
		debug_menu	
	fi
	POL_SetupWindow_Close
fi
$(POL_Config_Read POST_RUNNING)