#!/bin/bash
if [ "$PLAYONLINUX" = "" ]
then
	exit 0
fi
source "$PLAYONLINUX/lib/sources"

POL_SetupWindow_Init "$REPERTOIRE/plugins/Transgaming Cedega/setups/top.png" "$REPERTOIRE/plugins/Transgaming Cedega/setups/left.jpg"
POL_SetupWindow_free_presentation "Transgaming Cedega" "This plugin will permit you to import Transgaming Cedega games into PlayOnLinux"
if [ ! -e "$HOME/.cedega" ] 
then
	POL_SetupWindow_message "Unable to find cedega profile\nMaybe cedega is not properly installed" "Transgaming Cedega"
fi
cd "$HOME/.cedega"
FICHIERS=""
for file in *
do
	if [ ! "$file" =  "configuration_profiles" ]
	then
		if [ ! "$FICHIERS" = "" ]
		then
			FICHIERS+="~"		
		fi
	FICHIERS+="$file"
	fi
done
#FICHIERS="$(ls -1 | grep 'configuration_profiles' -v)"

POL_SetupWindow_menu "Choose a cedega prefix to import" "Transgaming Cedega" "$FICHIERS" "~"
prefix="$APP_ANSWER"
#prefix=$(menu "Choose a cedega prefix to import" "$FICHIERS" "Cedega Import Assistant" 0 0 1 "" "--cut-line")

cd "$HOME/.cedega/.winex_ver"
FICHIERS=""
for file in *
do
	if [ ! "$file" =  "configuration_profiles" ]
	then
		if [ ! "$FICHIERS" = "" ]
		then
			FICHIERS+="~"		
		fi
	FICHIERS+="$file"
	fi
done
type=0
if [ "$type" == "0" ]
then
	cd $REPERTOIRE/wineprefix
	ln -s $HOME/.cedega/$prefix
	POL_SetupWindow_menu  "Choose your winex version" "Transgaming Cedega" "$FICHIERS" "~"
	#winex=$(menu "Choose your winex version" "$FICHIERS" "Cedega Import Assistant" 0 0 1 "" "--cut-line")	
	winex="$APP_ANSWER"
	mkdir "$REPERTOIRE/tmp/cedega" -p
	cd "$REPERTOIRE/tmp/cedega"
	rm * 2> /dev/null
	$PYTHON "$HOME/.PlayOnLinux/plugins/Transgaming Cedega/scripts/import_shortcut.py" "$prefix" "$winex" 
fi

while [ ! "$shortcut" = "Finished" ]
do
pwet="0"
FICHIERS=""
for file in *
do
	if [ ! -e "$HOME/.PlayOnLinux/configurations/installed/$file" ]
	then
			if [ ! "$pwet" = "1" ]
			then
				pwet="1"
			else
				FICHIERS+="~"
			fi
		FICHIERS+="$file"
	fi

done
#shortcut=$(menu "The following shortcuts have been found.\nSelect the one you want to import" "$FICHIERS~Finished" "Cedega Import Assistant" 0 0 1 "" "~")	
POL_SetupWindow_menu "The following shortcuts have been found. Select the one you want to import." "Transgaming Cedega" "$FICHIERS~Finished" "~"
shortcut="$APP_ANSWER"
	if [ ! "$shortcut" = "Finished" ]
	then
		cp "$REPERTOIRE/tmp/cedega/$shortcut" "$REPERTOIRE/configurations/installed"
		bash $PLAYONLINUX/bash/reload --set
		POL_SetupWindow_message "$shortcut imported" "Transgaming Cedega"
	fi
done
POL_SetupWindow_Close
