#!/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)" "$LNG_EXPERT_WVER" 1 3 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 -1)
$LNG_OTHER" "$LNG_EXPERT_WVER" 2 3 1 "" "--cut-line")
	fi

	if [ "$VERSIONTAPER" = "$LNG_OTHER" ]
	then	
		#VERSIONTAPER=$(champ "$LNG_WINEVERSIONS_VERSIONTOUSE ($LNG_EXEMPLE: 0.9.32)")
		VERSIONTAPER=$(champ "$LNG_WINEVERSIONS_CHOOSEDOWNLOAD ($LNG_EXEMPLE : 0.9.32)" "" 2 3 1 "$LNG_EXPERT_WVER")
		NONMENU="true"
	fi	
	if [ "$VERSIONTAPER" = "" ]
	then
		erreur "$LNG_WINEVERSIONS_MUSTSPECIFY"
		menu_principal_wine
	fi
	if [ ! -e "$REPERTOIRE/WineVersions/$VERSIONTAPER" ]
	then
		if [ "$OFFLINE" == "1" ]
		then
			message "$LNG_OFFLINE_MODE"
		else
			message "$(LNG_WINEVERSIONS_NOTINSTALLED $VERSIONTAPER)"
			installer_wine_version "$VERSIONTAPER"
		fi
	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" "$LNG_EXPERT_WVER" 3 3 0
}

deregler_wine_version ()
{
	cd $REPERTOIRE/configurations/installed/
	JEUX=$(menu "$LNG_WINEVERSIONS_GAMETOEDIT" "$(ls)" "$LNG_EXPERT_WVER" 1 2 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)"  "$LNG_EXPERT_WVER" 2 2 0
	exit
}
desinstaller_wine ()
{
	cd $REPERTOIRE/WineVersions
	if [ "$(ls)" = "" ]
	then
		erreur "$LNG_WINEVERSIONS_NOWINE"
		menu_principal_wine
	fi
	DESINSTALLER=$(menu "$LNG_WINEVERSIONS_CHOOSEUNINSTALL" "$(ls)" "$LNG_EXPERT_WVER" 1 3 1 "" "--cut-line")
	if [ ! "$DESINSTALLER" = "" ]
	then
		reponse=$(question "$(LNG_WINEVERSIONS_WANTUNINSTALL $DESINSTALLER)" "$LNG_EXPERT_WVER" 2 3 0)
		if [ "$reponse" = "0" ]
		then
			rm -r $REPERTOIRE/WineVersions/$DESINSTALLER -f
			message "$(LNG_WINEVERSIONS_UNINSTALLSUCCES $DESINSTALLER)" "$LNG_EXPERT_WVER" 3 3 0
		fi
	fi
	menu_principal_wine
}
installer_wine_git ()
{
	bash "$PLAYONLINUX/bash/expert/WineGIT"
	exit 0
}
menu_principal_wine ()
{
	if [ "$OFFLINE" == "1" ]
	then
		menu_principal_wine=$(menu "$LNG_WINEVERSION_ACTIONS" "$LNG_WINEVERSIONS_MENU_ASSIGN~$LNG_WINEVERSIONS_MENU_UNASSIGN~$LNG_WINEVERSIONS_MENU_UNINSTALL" "$LNG_EXPERT_WVER" 0 0 1 "" "~")
	else 
		menu_principal_wine=$(menu "$LNG_WINEVERSION_ACTIONS" "$LNG_WINEVERSIONS_MENU_INSTALL~$LNG_WINEVERSIONS_MENU_ASSIGN~$LNG_WINEVERSIONS_MENU_UNASSIGN~$LNG_WINEVERSIONS_MENU_UNINSTALL~$LNG_WINEVERSIONS_MENU_GIT" "$LNG_EXPERT_WVER" 0 0 1 "" "~")
	fi
	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_GIT" ]
	then
		installer_wine_git
	fi
	exit
}
#menu_principal_wine 
$PYTHON "$PLAYONLINUX/python/wine_versions.py"
