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

if [ "$PLAYONLINUX" = "" ]
then
	exit 0
fi

source "$PLAYONLINUX/lib/sources" 
regler_wine_version ()
{
	cd $REPERTOIRE/configurations/installed/
	JEUX=$(menu "$LNG_WINEVERSIONS_GAMETOEDIT" "$(ls)" "" 0 0 1 "" "--cut-line")
	if [ "$JEUX" = "" ]
	then
		menu_principal_wine
	fi
	cd $REPERTOIRE/WineVersions
	if [ "$(ls)" = "" ]
	then
		VERSIONTAPER="$LNG_OTHER"
	else	
		VERSIONTAPER=$(menu "$LNG_WINEVERSIONS_VERSIONTOUSE" "$(ls) 
$LNG_OTHER" "" 0 0 1 "" "--cut-line")
	fi

	if [ "$VERSIONTAPER" = "$LNG_OTHER" ]
	then	
		VERSIONTAPER=$(champ "$LNG_WINEVERSIONS_VERSIONTOUSE ($LNG_EXEMPLE: 0.9.32)")
		NONMENU="true"
	fi	
	if [ "$VERSIONTAPER" = "" ]
	then
		erreur "$LNG_WINEVERSIONS_MUSTSPECIFY"
		menu_principal_wine
	fi
	if [ ! -e "$REPERTOIRE/WineVersions/$VERSIONTAPER" ]
	then
		message "$(LNG_WINEVERSIONS_NOTINSTALLED $VERSIONTAPER)"
		installer_wine_version "$VERSIONTAPER"
	else
		if [ "$NONMENU" = "true" ]
		then
			message "$LNG_WINEVERSIONS_ALREADYINSTALLED"
		fi
	fi
	FICHIER="$REPERTOIRE/tmp/nouveaulanceur"
	echo "#!/bin/bash" > $FICHIER
	echo "PATH=\"$REPERTOIRE/WineVersions/$VERSIONTAPER/usr/bin/:\$PATH\"" >> $FICHIER
	cat "$REPERTOIRE/configurations/installed/$JEUX" | tail -n 4 >> $FICHIER
	mv $REPERTOIRE/tmp/nouveaulanceur "$REPERTOIRE/configurations/installed/$JEUX"
	chmod +x "$REPERTOIRE/configurations/installed/$JEUX"
	ASSIGNED=$(LNG_WINEVERSIONS_ASSIGN "$JEUX" $VERSIONTAPER)
	message "$ASSIGNED"
}

deregler_wine_version ()
{
	cd $REPERTOIRE/configurations/installed/
	JEUX=$(menu "$LNG_WINEVERSIONS_GAME" "$(ls)" "" 0 0 1 "" "--cut-line")
	if [ "$JEUX" = "" ]
	then
		exit
	fi
	
	FICHIER="$REPERTOIRE/tmp/nouveaulanceur"
	echo "#!/bin/bash" > $FICHIER
	cat "$REPERTOIRE/configurations/installed/$JEUX" | tail -n 4 >> $FICHIER

	mv $REPERTOIRE/tmp/nouveaulanceur "$REPERTOIRE/configurations/installed/$JEUX"
	chmod +x "$REPERTOIRE/configurations/installed/$JEUX"
	message "$(LNG_WINEVERSIONS_DEFAULT $JEUX)"
	exit
}
desinstaller_wine ()
{
	cd $REPERTOIRE/WineVersions
	if [ "$(ls)" = "" ]
	then
		erreur "$LNG_WINEVERSIONS_NOWINE"
		menu_principal_wine
	fi
	DESINSTALLER=$(menu "$LNG_WINEVERSIONS_CHOOSEUNINSTALL" "$(ls)" "" 0 0 1 "" "--cut-line")
	if [ ! "$DESINSTALLER" = "" ]
	then
		reponse=$(question "$(LNG_WINEVERSIONS_WANTUNINSTALL $DESINSTALLER)")
		if [ "$reponse" = "0" ]
		then
			rm -r $REPERTOIRE/WineVersions/$DESINSTALLER -f
			message "$(LNG_WINEVERSIONS_UNINSTALLSUCCES $DESINSTALLER)"
		fi
	fi
	menu_principal_wine
}
menu_principal_wine ()
{
	menu_principal_wine=$(menu "$LNG_WINEVERSIONS_WINEVERSIONS" "$LNG_WINEVERSIONS_MENU_INSTALL $LNG_WINEVERSIONS_MENU_ASSIGN $LNG_WINEVERSIONS_MENU_UNASSIGN $LNG_WINEVERSIONS_MENU_UNINSTALL")
	if [ "$menu_principal_wine" = "$LNG_WINEVERSIONS_MENU_UNINSTALL" ]
	then
		desinstaller_wine 
	fi
	if [ "$menu_principal_wine" = "$LNG_WINEVERSIONS_MENU_INSTALL" ]
	then
		installer_wine_version
	fi
	if [ "$menu_principal_wine" = "$LNG_WINEVERSIONS_MENU_ASSIGN" ]
	then
		regler_wine_version
	fi
	if [ "$menu_principal_wine" = "$LNG_WINEVERSIONS_MENU_UNASSIGN" ]
	then
		deregler_wine_version
	fi
	if [ "$menu_principal_wine" = "$LNG_WINEVERSIONS_MENU_SESSION" ]
	then
		change_session_actuelle
	fi
	exit
}
menu_principal_wine 
