#!/bin/bash

# Copyright (C) 2007-2010 PlayOnLinux Team
# Copyright (C) 2007 Pâris Quentin
# Copyright (C) 2009 Łukasz Wojniłowicz

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

GenId() # On génère un ID unique pour notre fenêtre
{
	N_LETTRES=50

	if [ "$1" != "" ]
	then
	N_LETTRES=$1
	fi
	ALPHA='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'

	MOT=""
	i=0
	while ((i < $N_LETTRES));
	do
	CHIFFRE=$(( $RANDOM % ${#ALPHA}));
	printf ${ALPHA:$CHIFFRE:1};
	(( i=i+1 ))
	done
	exit 0
}
export POL_SetupWindow_ID=$(GenId)

POL_SetupWindow_presentation ()
{
	POL_SetupWindow_free_presentation "$(eval_gettext "Welcome to PlayOnLinux's Installation Wizard.")" "$(eval_gettext 'This wizard will help you install ')$1$(eval_gettext ' on your computer.')\n\n$(eval_gettext 'This program was created by: ')$2\n($3)\n\n$(eval_gettext 'This installation program is provided by: ')$4\n\n$1$(eval_gettext ' will be installed in: ')$REPERTOIRE/wineprefix/$5$(eval_gettext "\n\nPlayOnLinux is not responsible for the anything that might happen as a result of using these scripts.\n\n")$(eval_gettext 'Click Next to start')"

}
POL_SetupWindow_free_presentation ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_free_presentation "$@"
	else
		cat << EOF > "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
MsgIn
free_presentation
$1
$2
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		fi
	fi
}
POL_SetupWindow_message ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_message "$@"
	else
		cat << EOF > "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
MsgIn
message
$1
$2
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		fi
	fi
}
POL_SetupWindow_missing ()
{
	if [ "$MANQUE" = "true" ]
	then
		LNG_MISSING_ERROR=$(eval_gettext "One or more program(s) are missing. Please install them and run the script again.")

		POL_SetupWindow_message "$LNG_MISSING_ERROR\n\n$MISSING" "$(eval_gettext Error)"
		POL_SetupWindow_Close
		exit $EXIT_MISSING
	fi
}
POL_SetupWindow_licence ()
{
	
	if [ -f "$3" ]
	then
		if [ "$POL_CMD" == "true" ] ; then
			POL_CMD_licence "$@"
		else
			cat << EOF > "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
MsgIn
licence
$1
$2
$3
EOF
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			while [ ! "$MESSAGE" = "MsgOut" ]
			do
				MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
				sleep 0.1
			done
			MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
			if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
			then
				rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
				exit 
			fi
		fi
	fi
}
POL_SetupWindow_message_image ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_message_image "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
message_image
$1
$2
$3
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		fi
	fi
}
POL_SetupWindow_wait_next_signal ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_wait_next_signal "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
attendre_signal
$1
$2
EOF
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			exit 
		fi
	fi
}
POL_SetupWindow_pulsebar ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_pulsebar "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
pulsebar
$1
$2
EOF
		sleep 0.2 #sleeping 0.2 GUI have to read dialog file. see bug #30
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			exit 
		fi
	fi
}
POL_SetupWindow_pulse ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_pulse "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
pulse
$1
EOF
		sleep 0.2 #sleeping 0.2 GUI have to read dialog file. see bug #30
	fi
}
POL_SetupWindow_set_text ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_set_text "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
set_text
$1
EOF
		sleep 0.2 #sleeping 0.2 GUI have to read dialog file. see bug #30
	fi
}
POL_SetupWindow_wait ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_wait "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
attendre
$1
$2
$3
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		fi
	fi
}
POL_SetupWindow_download ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_download "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
download
$1
$2
$3
$(pwd)/
EOF
		sleep 0.2 #may be fix launchpad bug #392513.

		DOWNLOADEDFILE=`cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | head -n5 | tail -n1`
		DOWNLOADSERVER=`echo $DOWNLOADEDFILE | cut -d/ -f3`
		DOWNLOADEDFILE=$(basename "$DOWNLOADEDFILE")

		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			exit 
		fi

		MESSAGE_TER=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_VALUE=')
		MESSAGE_TER=${MESSAGE_TER:10}
		if [ ! "$MESSAGE_TER" = 0 ]
		then
			if [ $(echo "$DOWNLOADEDFILE" | grep -o 'PlayOnLinux-wine') = "PlayOnLinux-wine" ]
			then
				ERROR_MESSAGE="$(eval_gettext "Wine version server temporary unavailable.\nPlease try again later.")"
			elif [ "$MESSAGE_TER" = 1 ]
			then
				ERROR_MESSAGE="$(eval_gettext "Following server is no longer available:\n\n")$DOWNLOADSERVER"
			elif [ "$MESSAGE_TER" = 2 ]
			then
				ERROR_MESSAGE="$(eval_gettext "Following file is no longer available on server:\n\n")$DOWNLOADEDFILE"
			fi

			POL_SetupWindow_message_image "$ERROR_MESSAGE" "$(eval_gettext "Download failure")" "$PLAYONLINUX/themes/tango/warning.png"
			POL_SetupWindow_Close
			exit $EXIT_DOWNLOADERROR
		fi
	fi
}
POL_SetupWindow_detect_exit ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_detect_exit
	else
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		fi
	fi
}
POL_SetupWindow_textbox ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_textbox "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
champ
$1
$2
$3
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		else
			MESSAGE_TER=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_VALUE=')
			MESSAGE_TER=${MESSAGE_TER:10}
			export APP_ANSWER="$MESSAGE_TER"
		fi
	fi
}
POL_SetupWindow_browse ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_browse "$@"
	else
	cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
browse
$1
$2
$3
$PWD
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		else
			MESSAGE_TER=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_VALUE=')
			MESSAGE_TER=${MESSAGE_TER:10}
			export APP_ANSWER="$MESSAGE_TER"
		fi
	fi
}
POL_SetupWindow_question ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_question "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
question
$1
$2
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		else
			MESSAGE_TER=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_QUESTION=')
			MESSAGE_TER=${MESSAGE_TER:13}
			export APP_ANSWER="$MESSAGE_TER"
		fi
	fi
}
POL_SetupWindow_menu ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_menu "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
menu
$1
$2
$3
$4
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		else
			MESSAGE_TER=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_VALUE=')
			MESSAGE_TER=${MESSAGE_TER:10}
			export APP_ANSWER="$MESSAGE_TER"
		fi
	fi
}
POL_SetupWindow_checkbox_list ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_checkbox_list "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
checkbox_list
$1
$2
$3
$4
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		else
			MESSAGE_TER=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_VALUE=')
			MESSAGE_TER=${MESSAGE_TER:10}
			export APP_ANSWER="$MESSAGE_TER"
		fi
	fi
}
POL_SetupWindow_menu_num ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_menu_num "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
menu_num
$1
$2
$3
$4
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		else
			MESSAGE_TER=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_VALUE=')
			MESSAGE_TER=${MESSAGE_TER:10}
			export APP_ANSWER="$MESSAGE_TER"
		fi
	fi
}
POL_SetupWindow_menu_list ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_menu_list "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
menu_list
$1
$2
$3
$4
$5
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		else
			MESSAGE_TER=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_VALUE=')
			MESSAGE_TER=${MESSAGE_TER:10}
			export APP_ANSWER="$MESSAGE_TER"
		fi
	fi
}
POL_SetupWindow_games ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_games "$@"
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
get_games
$1
$2
$3
$4
EOF
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		while [ ! "$MESSAGE" = "MsgOut" ]
		do
			MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
			sleep 0.1
		done
		MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
		if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
		then
			rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			exit 
		else
			MESSAGE_TER=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_VALUE=')
			MESSAGE_TER=${MESSAGE_TER:10}
			export APP_ANSWER="$MESSAGE_TER"
		fi
	fi
}
POL_SetupWindow_specialprefixcreate ()
{
	POL_SetupWindow_wait_next_signal "$(eval_gettext 'Please wait while the Wine prefix is being created...')" "$(eval_gettext 'Wine prefix configuration')"
	wine wineprefixcreate
	sleep 2
	set_user_dir
	set_programfile_dir
	POL_SetupWindow_detect_exit
}
POL_SetupWindow_oldprefixcreate ()
{
	POL_SetupWindow_wait_next_signal "$(eval_gettext 'Please wait while the Wine prefix is being created...')" "$(eval_gettext 'Wine prefix configuration')"
	wine wineprefixcreate
	POL_SetupWindow_detect_exit
}
POL_SetupWindow_prefixcreate ()
{
	POL_SetupWindow_normalprefixcreate
	POL_LoadVar_PROGRAMFILES
}
POL_SetupWindow_normalprefixcreate()
{
	POL_SetupWindow_wait_next_signal "$(eval_gettext 'Please wait while the Wine prefix is being created...')" "$(eval_gettext 'Wine prefix configuration')"
	wine wineboot
	POL_SetupWindow_detect_exit
}
POL_SetupWindow_make_shortcut()
{
	# 1 = Wineprefix
	# 2 = Repertoire
	# 3 = Binaire
	# 4 = Icone (png ou xpm)
	# 5 = Nom de l'icône
	# 6 = Ignoré (question de compatibilité avec les versions < 2.0)
	# 7 = Eventuel argument

	creer_lanceur_base "$@"

	cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
make_shortcut
$NOMICONE_
EOF
		
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_make_shortcut "$NONICONE_"
	fi

	MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
	while [ ! "$MESSAGE" = "MsgOut" ]
	do
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		sleep 0.1
	done
	MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
	if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
	then
		rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
		exit 
	else
		if [ -f "$REPERTOIRE/icones/full_size/$NOMICONE_" ]; then # L'icone existe...
			iconPath="$REPERTOIRE/icones/full_size/$NOMICONE_"
		else
			iconPath="$REPERTOIRE/icones/full_size/playonlinux.png"
		fi
	
		MESSAGE_MENU=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_MENU=')
		MESSAGE_DESKTOP=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_DESKTOP=')		
		if [ "$MESSAGE_MENU" == "MSG_MENU=True" ]
		then
			generer_icone "$iconPath" "$NOMICONE_" "$HOME/.local/share/applications/" "$PLAYONLINUX/playonlinux --run \"$NOMICONE_\"" "$NOMICONE_"
		fi
		if [ "$MESSAGE_DESKTOP" == "MSG_DESKTOP=True" ]
		then
			generer_icone "$iconPath" "$NOMICONE_" "$DESKTOP" "$PLAYONLINUX/playonlinux --run \"$NOMICONE_\"" "$NOMICONE_"
		fi
	fi
}
POL_SetupError()
{
	POL_SetupWindow_message "$1\n\nAborting." "Error"
	POL_SetupWindow_Close
	exit
}
POL_SetupWindow_auto_shortcut()
{
	# 1 = Wineprefix
	# 2 = Binaire
	# 3 = Nom du raccourics
	# 4 = Icone
	# 5 = Eventuel argument

	Binaire="$2"
	ShortName="$3"
	SpecialArg="$5"

	
	if [ "$3" = "" ]
	then
		NOMICONE_="$1"
	else
		NOMICONE_="$3"
	fi
	
	mkdir -p "$REPERTOIRE/icones/32"
	mkdir -p "$REPERTOIRE/icones/full_size"
	
	ICON_WEB_NAME="$4"
	ICON_OK=0

	## On chope le dossier du binaire
	cd "$REPERTOIRE/wineprefix/$1" || POL_SetupError "Prefixe $1 does not exists"
	cd drive_c || POL_SetupError "drive_c folder does not exists"
	binary_path=$(find ./ -iname "$Binaire" | tail -n 1)
	binary_dir=$(dirname "$binary_path")
	Binaire=$(basename "$binary_path")
	[ "$binary_dir" = "" ] && POL_SetupError "Can't find $Binaire"

	
	# Création de l'icone.
	
	# Cas 1 : le nom de l'icone est donné.
	if [ -n "$ICON_WEB_NAME" ]; then
		if [ ! "$OFFLINE" = "1" ]; then # On peut le télécharger...
			wget -q "$SITE/icones/$ICON_WEB_NAME" -O "$REPERTOIRE/icones/full_size/$NOMICONE_" # On prend l'icone full size
		fi
		
		if [ -f "$REPERTOIRE/icones/full_size/$NOMICONE_" ]; then
			convert -resize 32 "$REPERTOIRE/icones/full_size/$NOMICONE_" "$REPERTOIRE/icones/32/$NOMICONE_" # On fabrique l'icone 32*32
			ICON_OK=1
		fi
	elif [ "$ICON_OK" -ne 1 ]; then # Pas d'incone sur le web => Création à partir de l'exe...
		echo "No internet icon..."
		
		mkdir -p "$REPERTOIRE/tmp/win32Icon"
		cd "$REPERTOIRE/tmp/win32Icon"
		wrestool -x -t14 "$REPERTOIRE/wineprefix/$1/drive_c/$binary_dir/$Binaire" &> icons-brut

		icotool -x "icons-brut" -o "$REPERTOIRE/tmp/win32Icon/"

		find . -name "*32x32x32.png" > icons-list
		find . -name "*32x32x16.png" >> icons-list
		find . -name "*32x32x8.png" >> icons-list
		find . -name "*32x32x4.png" >> icons-list
		ICON_FILE=$(cat "icons-list" | head -n 1)
		[ "$ICON_FILE" != "" ] && [ -s "$ICON_FILE" ] && cp "$ICON_FILE" "$REPERTOIRE/icones/32/$NOMICONE_"

		rm -r "$REPERTOIRE/tmp/win32Icon"
		
		# On ne crée pas d'icone "full_size", parce que les résolutions des icones windows ne sont pas suffisantes tout le temps.
		# Pour verifier la taille des icones, il faudrait utiliser "identify", mais ce programme n'est pas toujours fournis.
	fi


	## On génère le binaire 
	echo "#!/bin/bash" > "$REPERTOIRE/configurations/installed/$ShortName"
	echo "export WINEPREFIX=\"$REPERTOIRE/wineprefix/$1\"" >> "$REPERTOIRE/configurations/installed/$ShortName"
	echo "export WINEDEBUG=\"-all\"" >> "$REPERTOIRE/configurations/installed/$ShortName"
	echo "cd \"$REPERTOIRE/wineprefix/$1/drive_c/$binary_dir\"" >> "$REPERTOIRE/configurations/installed/$ShortName"
	echo "wine \"$Binaire\" $SpecialArg \$@" >> "$REPERTOIRE/configurations/installed/$ShortName"
	chmod +x "$REPERTOIRE/configurations/installed/$ShortName"
	
	
	## On prévient l'utilisateur que l'icône est prête, on lui propose de creer divers raccorucis
	cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
make_shortcut
$NOMICONE_
EOF

	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_auto_shortcut "$NONICONE_"
	fi
	## Attente de la réponse 
	MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
	while [ ! "$MESSAGE" = "MsgOut" ]
	do
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		sleep 0.1
	done
	
	## Lecture de la réponse
	MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')

	
	if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ] ## Si il annule, on quitte 
	then
		rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
		exit 
	else ## Sinon
		if [ -f "$REPERTOIRE/icones/full_size/$NOMICONE_" ]; then # L'icone existe...
			iconPath="$REPERTOIRE/icones/full_size/$NOMICONE_"
		else
			iconPath="$REPERTOIRE/icones/full_size/playonlinux.png"
		fi
	
		MESSAGE_MENU=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_MENU=')
		MESSAGE_DESKTOP=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_DESKTOP=')		
		if [ "$MESSAGE_MENU" == "MSG_MENU=True" ]
		then
			generer_icone "$iconPath" "$NOMICONE_" "$HOME/.local/share/applications/" "$PLAYONLINUX/playonlinux --run \"$ShortName\"" "$ShortName"
		fi
		if [ "$MESSAGE_DESKTOP" == "MSG_DESKTOP=True" ]
		then
			generer_icone "$iconPath" "$NOMICONE_" "$DESKTOP" "$PLAYONLINUX/playonlinux --run \"$ShortName\"" "$ShortName"
		fi
	fi
}
POL_SetupWindow_Close ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_close
	else
		cat << EOF > $REPERTOIRE/configurations/guis/$POL_SetupWindow_ID
MsgIn
exit
EOF
	fi
}
POL_SetupWindow_Quit ()
{
	POL_SetupWindow_Close "$@"
}
POL_SetupWindow_Init ()
{
	if [ "$POL_CMD" == "true" ] ; then
		POL_CMD_init $@
	else
		if [ "$1" = "" ] || [ "$1" = "--force" ]
		then
			arg1="None"
		else 
			arg1="$1"
		fi
		if [ "$2" = "" ]
		then
			arg2="None"
		else
			arg2="$2"
		fi

		mkdir -p "$REPERTOIRE/configurations/guis/"
		if [ ! -e "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" ] || [ "$1" = "--force" ]
		then
			echo "Wait" > "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
			if [ "$POL_DEBUG" == "TRUE" ]
			then
				$PYTHON "$PLAYONLINUX/python/guiv3.py" "$arg1" "$arg2" &
			else
				$PYTHON "$PLAYONLINUX/python/guiv3.py" "$arg1" "$arg2" > /dev/null 2> /dev/null &
			fi
		elif [ ! "$1" = "--force" ]
		then
			echo "E. Window $POL_SetupWindow_ID already opened !"
		fi
	fi
}
POL_SetupWindow_Open ()
{
	POL_SetupWindow_Init "$@"
}
POL_SetupWindow_reboot ()
{
	POL_SetupWindow_wait_next_signal "$(eval_gettext 'Please wait while PlayOnLinux is simulating a Windows reboot')" "Wine"
	wineboot
	POL_SetupWindow_detect_exit
}
POL_SetupWindow_cdrom () 
{
	# 1 = Numéro du CD
	LIST_DEVICES_=""
	cd /media
	for device in *
	do
		if [ ! "$device" = "$LNG_OTHER" ]
		then
			fstab1=`cat '/etc/fstab' | grep "$device"`
			fstab2=`cat '/etc/fstab' | grep "$device" | grep iso9660`
			if [ "$fstab1" = "" -o "$fstab2" != "" ]
			then
				LIST_DEVICES_+="$device~"
			fi
		fi
	done
		
	LNG_WINE_ASKFORCDROM=$(eval_gettext "Where is your CD-ROM mounted?")
	LNG_OTHER=$(eval_gettext "Other")
	POL_SetupWindow_menu "$LNG_WINE_ASKFORCDROM" "CD-ROM" "$LIST_DEVICES_$LNG_OTHER" "~"		
	CDROM="$APP_ANSWER"
	if [ "$CDROM" = "$LNG_OTHER" ]
	then
		POL_SetupWindow_textbox "$LNG_WINE_ASKFORCDROM" "CD-ROM" "/media/cdrom"
		CDROM="$APP_ANSWER"
	else
		CDROM="/media/$CDROM"
	fi


	if [ "$CDROM" = "" ]
	then
		CDROM="/media/cdrom"
	fi
}
POL_SetupWindow_check_cdrom ()
{
	#if [ ! "$CDROM" = "" ]
	# Si quelqu'un peut tester ça ce serait cool
	if [ ! "$(find "$CDROM" -iname "$CDROM/$1")" = "" ]
	then
		while [ ! -e "$CDROM/$1" ]
		do
			POL_SetupWindow_message "$(eval_gettext "Error: Unable to find the CD-ROM!")" "CD-ROM"
			POL_SetupWindow_cdrom
		done
	fi
}
POL_SetupWindow_install_wine ()
{
	rm "$REPERTOIRE/WineVersions/installed" 2> /dev/null
	ARCHITECTURE="i386"
	VERSIONENTRER="$1"

	if [ "$VERSIONENTRER" = "" ]
	then
		exit
	fi
	if [ ! -e "$REPERTOIRE/WineVersions/$VERSIONENTRER" ]
	then
		VERSION="$VERSIONENTRER"
		ADRESSEDUDEB=$(wget "http://mulx.playonlinux.com/wine/linux-i386/LIST" -O- | grep ";$VERSION;" | tail -n 1 | cut -d ";" -f1)
		if [ "$ADRESSEDUDEB" = "" ]
		then
		 POL_SetupWindow_message "$(eval_gettext "Unable to find version: ")$VERSIONENTRER" "Wine $1"
		 POL_SetupWindow_Close
		 exit
		fi

		cd "$REPERTOIRE/tmp"
		POL_SetupWindow_download "$(eval_gettext "Downloading Wine: ")$VERSIONENTRER" "Wine" "http://mulx.playonlinux.com/wine/linux-i386/$ADRESSEDUDEB"
		POL_SetupWindow_wait_next_signal "$(eval_gettext "Downloading Wine: ")$VERSIONENTRER" "Wine"
		sleep 1
		sha1=$(wget "http://mulx.playonlinux.com/wine/linux-i386/$ADRESSEDUDEB.sha1" -O-)
		sha1_file=$(sha1sum "./$ADRESSEDUDEB")
		echo "Server sha1 : $sha1"
		echo "Client sha1 : $sha1_file"

		POL_SetupWindow_detect_exit
		if [ ! "$sha1" = "$sha1_file" ]
		then
			POL_SetupWindow_message "$(eval_gettext 'The download seems to have failed.')" "Wine $1"
		else
			POL_SetupWindow_wait_next_signal "$(eval_gettext 'Extracting Wine...')" "Wine $1"
			"$PLAYONLINUX/playonlinux-pkg" -i "$ADRESSEDUDEB"
			POL_SetupWindow_detect_exit
		fi
		rm "$ADRESSEDUDEB"
	fi
	touch "$REPERTOIRE/WineVersions/installed"
}

POL_GetSetupImages () # ( optional : --erase ) 
{
	# This function helps you to manage your setup images
	# It will download images only once, except if you use --erase
	#
	# 1 - Top image url
	# 2 - Left image url
	# 3 - Name (only alpha-numerical please)
	# 
	# Path of images are returned in $POL_SetupWindow_LeftImage and $POL_SetupWindow_TopImage

	if [ "$1" == "--force" ]
	then
		left="$3"
		top="$2"
		name="$4"
	else
		left="$2"
		top="$1"
		name="$3"
	fi	
	mkdir -p "$POL_USER_ROOT/configurations/setups/$name"
	cd "$POL_USER_ROOT/configurations/setups/$name"

	if [ "$1" == "--force" ] || [ ! -e "top" ]
	then
		wget -q --timeout=10 "$top" -O top 2> /dev/null && POL_SetupWindow_TopImage="$PWD/top" || export POL_SetupWindow_TopImage="$PLAYONLINUX/etc/setups/default/top.png"
		if [ "$POL_SetupWindow_TopImage" = "$PLAYONLINUX/etc/setups/default/top.png" ]
		then
			rm -f top
		fi
	else
		POL_SetupWindow_TopImage="$PWD/top"
	fi

	if [ "$1" == "--force" ] || [ ! -e "left" ]
	then
		wget -q --timeout=10 "$left" -O left 2> /dev/null && POL_SetupWindow_LeftImage="$PWD/left" || export POL_SetupWindow_LeftImage="$PLAYONLINUX/etc/setups/default/left.jpg"
		if [ "$POL_SetupWindow_LeftImage" = "$PLAYONLINUX/etc/setups/default/left.jpg" ]
		then
			rm -f left
		fi
	else
		POL_SetupWindow_LeftImage="$PWD/left"
	fi
	cd "$OLDPWD"

}

POL_SetupWindow_InitWithImages ()
{
	POL_SetupWindow_Init "$POL_SetupWindow_TopImage" "$POL_SetupWindow_LeftImage"
}
