#!/bin/bash

# Copyright (C) 2007-2010 PlayOnLinux Team
# 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. 

# PlayOnLinux / PlayOnMac Online

[ "$PLAYONLINUX" = "" ] && exit 
source "$PLAYONLINUX/lib/sources"

TITLE="$APPLICATION_TITLE Online"

personal_scripts()
{
	POL_Website_GET "$SITE/V4_data/repository/get_personal_list.php"
	PSCRIPTS="$(POL_Website_Result)"
	if [ "$PSCRIPTS" ]; then
		POL_SetupWindow_menu "$(eval_gettext "This is the list of the scripts you sent to us. You can run one of them")" "$TITLE" "$PSCRIPTS~Back" "~"
		
		if [ ! "$APP_ANSWER" = "" -a ! "$APP_ANSWER" = "Back" ];  then
			POL_Website_GET "$SITE/V4_data/repository/get_file.php?id=$APP_ANSWER"
			POL_Website_Result > "$POL_USER_ROOT/install"
			cd "$POL_USER_ROOT"
			POL_SetupWindow_wait "$(eval_gettext "Running...")" "$TITLE"
			POL_Bash "install" 
		fi
	else
		POL_SetupWindow_message "$(eval_gettext 'You do not seems to have any personal script')" "$TITLE"
	fi
}
send_script()
{
	POL_SetupWindow_message "$(eval_gettext "This tools will allow you to help us :")\n$(eval_gettext "If you have successfully installed a program with 'Install a non-listed\nsoftware' button, it will allow you to add this program in our list")\n\n$(eval_gettext "We're going to ask you some questions, please be as much accurate as\npossible")" "$TITLE"
	LIST=""
	cd "$POL_USER_ROOT/scripts"
	for file in *
	do
		LIST="$LIST~$file"
	done
	LIST="${LIST:1}"
	POL_SetupWindow_menu "$(eval_gettext "Choose the file corresponding to your program.")" "$TITLE" "$LIST" "~"
	SCRIPTFILE="$APP_ANSWER"
	script="$(cat "$SCRIPTFILE")"
	script="$(POL_Website_urlparse "$script")"
	POL_SetupWindow_question "$(eval_gettext "Does your program run?")" "$TITLE"
	if [ "$APP_ANSWER" = "TRUE" ]
	then
		POL_SetupWindow_textbox "$(eval_gettext "What is the title of your program?")" "$TITLE"
		SCRIPTTITLE="$APP_ANSWER"
		POL_SetupWindow_textbox "$(eval_gettext "What is the editor of your program?")" "$TITLE"
		EDITOR="$APP_ANSWER"
		POL_SetupWindow_textbox "$(eval_gettext "What is the editor website of your program?")" "$TITLE" "http://"
		URL="$APP_ANSWER"

		POL_SetupWindow_menu_num "$(eval_gettext "Select a category for your program")" "$TITLE" "$(eval_gettext "Other")~$(eval_gettext "Games")~$(eval_gettext "Accessories")~$(eval_gettext "Office")~$(eval_gettext "Internet")~$(eval_gettext "Multimedia")~$(eval_gettext "Graphics")~$(eval_gettext "Development")~$(eval_gettext "Education")~$(eval_gettext "Patches")~$(eval_gettext "Testing")" "~" 
		CATEGORY="$APP_ANSWER"
		URL="$(printf $APP_ANSWER | sed s/\\//\\\\\\//g)" # J'adore ce genre de ligne la
		POL_SetupWindow_textbox_multiline "$(eval_gettext "What feature of your program does not work?\nIf everything works, please mention it")" "$TITLE"
		FEATURE="$APP_ANSWER"
		POL_SetupWindow_textbox_multiline "$(eval_gettext "Write any change you made to wine configuration here:")" "$TITLE"
		DESCRIPTION="Features
$FEATURE
		
Wine configuration :
$APP_ANSWER"


script="${script/'{SCRIPT_NAME}'/$SCRIPTTITLE}"
script="${script/'{SCRIPT_EDITEUR}'/$EDITOR}"
script="${script/'{SCRIPT_URL}'/$URL}"
script="${script/'{SCRIPT_USER}'/$POL_LOGIN}"

[ "$POL_OS" == "Mac" ] && repos="playonmac"
[ "$POL_OS" == "Linux" ] && repos="playonlinux"
POL_Website_Request "http://www.$repos.com/repository/new.php" "name=Generated $SCRIPTTITLE&send=Add"

#POL_SetupWindow_message "Debug : premiere requete faite" "$TITLE"
SCRIPT_ID="$(POL_Website_Result | grep '<input type="hidden" name="id"'| cut -d"\"" -f16)"

POL_Debug_Message "SCRIPT ID generated : $SCRIPT_ID"

POL_Website_Request "http://www.$repos.com/repository/edit.php" "name=Generated $SCRIPTTITLE $SCRIPT_ID&cat=$CATEGORY&dosbox=0&message=$script&description=$DESCRIPTION&send=Edit&id=$SCRIPT_ID"

POL_Website_GET "http://www.$repos.com/repository/state.php?sur=1&id=$SCRIPT_ID&state=0"
#POL_Website_Result
#echo "name=Generated $SCRIPTTITLE $SCRIPT_ID&cat=$CATEGORY&dosbox=0&message=$script&description=$DESCRIPTION&send=Edit&id=$SCRIPT_ID"
	else
		POL_SetupWindow_message "$(eval_gettext "You must be able to run this program!")" "$TITLE"
	fi
}
main_menu()
{
	LNG_SCRIPTS="$(eval_gettext "Run one of your personal scripts")"
	LNG_SEND="$(eval_gettext "Send to us a working script generated by manual installation")"
	LNG_LOGOUT="$(eval_gettext "Logout")"
	POL_SetupWindow_menu_num "$(eval_gettext 'You are connected to $APPLICATION_TITLE online as $POL_LOGIN')" "$TITLE" "$LNG_SCRIPTS~$LNG_SEND~$LNG_LOGOUT" "~"
	ACTION="$APP_ANSWER"
	if [ "$ACTION" = "0" ]
	then
		personal_scripts
	fi
	if [ "$ACTION" = "1" ]
	then
		send_script
	fi
	if [ "$ACTION" = "2" ]
	then
		POL_Website_Close
		POL_Website_logout
		POL_SetupWindow_message "$(eval_gettext 'You have been successfully logged out of $APPLICATION_TITLE Online\nYou can safely close this window')" "$TITLE"
		POL_SetupWindow_Close
	fi
	main_menu
}
POL_SetupWindow_Init
POL_Website_login
POL_Website_Init
main_menu

exit 0