#!/bin/bash
# Copyright (C) 2007 Pâris Quentin
#  		     Cassarin-Grand Arthur

# 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. 
lancer ()
{
	if [ -e "$REPERTOIRE/configurations/installed/$1" ]
	then
		# $1 changed by $@ to run script with some arguments
		# need game install by POL >= 1.8.0
		cd $REPERTOIRE/configurations/installed
		bash $@ 
	else
		erreur "$LNG_GAME_NOT_INSTALLED"
	fi
	wait_wine_exit
}
verifier_jeux ()
{
	# Permet de vérifier qu'il y a au moins un jeu disponible #
	cd $REPERTOIRE
	if [ "$(cat liste_jeux)" = "" ]
	then
	 erreur "$LNG_GAME_GAMES_MISSING"	
	 exit
	fi
}
verifier_work ()
{
	# Permet de vérifier qu'il y a au moins un jeu disponible #
	cd $REPERTOIRE
	if [ "$(cat liste_work)" = "" ]
	then
	 erreur "$LNG_GAME_WORKS_MISSING"	
	 fixme "LNG_WORK"
	 exit
	fi
}
verifier_patchs ()
{
	# Permet de vérifier qu'il y a au moins un patch disponible #
	cd $REPERTOIRE
	if [ "$(cat liste_patchs)" = "" ]
	then
	 erreur "$LNG_GAME_PATCHS_MISSING"
	 exit	
	fi
}
verifier_expansions ()
{
	# Permet de vérifier qu'il y a au moins une expansion disponible #
	cd $REPERTOIRE
	if [ "$(cat liste_expansions)" = "" ]
	then
	 erreur "$LNG_GAME_ADDON_MISSING"
	 exit	
	fi
}
verifier_existence_jeux ()
{
	# Vérifier que au moins un jeu est installé "
	cd $REPERTOIRE/wineprefix
	if [ "$(ls -1)" = "" ]
	then
	    erreur "$LNG_GAME_TEST_GAME"
	    exit
	fi
}

verifier_installation ()
{
	if [ -e "$REPERTOIRE/wineprefix/$1" ]
	then
	    erreur "$LNG_GAME_ALREADY_INSTALLED"
	    exit
	fi
}
verifier_installation_e ()
{
	cd $REPERTOIRE/wineprefix/
	if [ ! -e "./$1" ]
	then
	   erreur "$(LNG_GAME_REQUIRED $1)"
	   exit
	fi
}
generer_icone ()
{
	ICONE=$1
	NOMICONE=$2
	NOMCOURT=$5
	PLACE=$3
	EXECUTER=$4
	
	mkdir -p $PLACE
	cd $PLACE
	echo "[Desktop Entry]" > "$NOMCOURT.desktop"
	echo "Encoding=UTF-8" >> "$NOMCOURT.desktop"
	echo "Name=$NOMICONE" >> "$NOMCOURT.desktop"
	echo "GenericName=Game" >> "$NOMCOURT.desktop"
	echo "Comment=PlayOnLinux" >> "$NOMCOURT.desktop"
	echo "Type=Application" >> "$NOMCOURT.desktop"
	echo "Exec=$EXECUTER" >> "$NOMCOURT.desktop"
	echo "Icon=$ICONE" >> "$NOMCOURT.desktop"
	echo "Categories=PlayOnLinux;" >> "$NOMCOURT.desktop"
	echo "Name[fr_FR]=$NOMICONE" >> "$NOMCOURT.desktop"
}
creer_lanceur () 
{
	# 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

	if [ "$4" = "" ]
	then
		ICONE_="playonlinux.png"
	else
		ICONE_="$4"
	fi

	if [ "$5" = "" ]
	then
		NOMICONE_="$1"
	else
		NOMICONE_="$5"
	fi


	Binaire="$3"
	SpecialArg="$7"
	
	cd $REPERTOIRE/icones
	wget $SITE/icones/$ICONE_
	if [ "$ICONE_" != "playonlinux.png" ]
	then
		convert -resize 32 "$ICONE_" "32/$NOMICONE_" # On fabrique l'icone
	fi

	echo "#!/bin/bash" > "$REPERTOIRE/configurations/installed/$NOMICONE_"
	echo "export WINEPREFIX=\"$REPERTOIRE/wineprefix/$1\"" >> "$REPERTOIRE/configurations/installed/$NOMICONE_"
	echo "export WINEDEBUG=\"-all\"" >> "$REPERTOIRE/configurations/installed/$NOMICONE_"
	echo "cd \"$WINEPREFIX/drive_c/$2\"" >> "$REPERTOIRE/configurations/installed/$NOMICONE_"
	echo "wine \"$Binaire\" $SpecialArg \$@" >> "$REPERTOIRE/configurations/installed/$NOMICONE_"
	chmod +x "$REPERTOIRE/configurations/installed/$NOMICONE_"

	bash $PLAYONLINUX/bash/reload --set

	reponse=$(question "$NOMICONE_ : $LNG_GAME_DESKTOP_ICONE" "Création des raccourcis" 0 0 0 "desktop.png")
	if [ "$reponse" = "0" ]
	then
		generer_icone "$REPERTOIRE/icones/$ICONE_" "$NOMICONE_" "$HOME/Desktop/" "$PLAYONLINUX/playonlinux --run \"$NOMICONE_\"" "$NOMICONE_"
	fi

	reponse=$(question "$NOMICONE_ : $LNG_GAME_MENU_ICONE" "Création des raccourcis" 0 0 0 "desktop.png")
	if [ "$reponse" = "0" ]
	then
		generer_icone "$REPERTOIRE/icones/$ICONE_" "$NOMICONE_" "$HOME/.local/share/applications/" "$PLAYONLINUX/playonlinux --run \"$NOMICONE_\"" "$NOMICONE_"
	fi
	
}
creer_lanceur_expansion ()
{
	creer_lanceur $@
}
detect_wineprefix()
{
	file="$REPERTOIRE/configurations/installed/$1"
	if [ -e "$file" ]
	then	
		fichier=$(cat "$file" | grep WINEPREFIX)
		fichier=${fichier:18}
		fichier=${fichier//"\""/""}
	fi
	echo $fichier
}
clean_wineprefix()
{
   # Préfixe (chemin complet)
   cd $REPERTOIRE/configurations/installed/
   TEST=${1//".PlayOnLinux/wineprefix/"/""}
   if [ "$TEST" = "$1" ]
   then
	erreur "Le répértoire n'est pas dans PlayOnLinux"
   else
	used=0
	   for file in *
	   do
		fichier="${file}"
		prefix=$(detect_wineprefix "$fichier")
		if [ "$prefix" = "$1" ]
		then
			used=1
		fi
	   done
	   if [ "$used" = 1 ] 
	   then
		echo "$1 est encore utilisé."
	   else
		reponse=$(question "Voulez-vous supprimer le préfixe $1 qui n'est pas utile ?")
		if [ "$reponse" = "0" ]
		then
			rm $1 -r
		fi
	   fi
   fi
}
