#!/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"
cfg_check
#French
if [ "$POL_LANG" == "fr_FR.UTF-8" ]; then
	LNG_WINEBOOSTER_ITEM_CHOOSE="Choisissez une option a modifiée"
	LNG_WB_ALL="Tous"
else
#English
	LNG_WINEBOOSTER_ITEM_CHOOSE="Choose a \"to be modified\" option"
	LNG_WB_ALL="All"
fi

set_wb_ddr ()
{
	A_DDR=`cat $prefixe/user.reg | grep "DirectDrawRenderer\"=" | sed -e {s/DirectDrawRenderer//g} -e {s/\"//g} -e {s/=//g}`
	if [ "$A_DDR" == "" ]; then
		DDR=$(champ "$LNG_WINEBOOSTER_DDR" "gdi" 2 7 1 "WineBooster" ) ;
	else
		DDR=$(champ "$LNG_WINEBOOSTER_DDR" "$A_DDR" 2 7 1 "WineBooster" ) ;
	fi
	if [ "$DDR" == "opengl" ]; then
		WINE_DDR="opengl"
	else
		WINE_DDR="gdi"
	fi
	if [ "$WB_ITEM" != "$LNG_WB_ALL" ]; then
	TEMP="$HOME/.PlayOnLinux/tmp/WineBooster"
	chmod 777 $TEMP -R
	rm $TEMP -R
	mkdir -p $TEMP

	echo "[HKEY_CURRENT_USER\Software\Wine\Direct3D]" > $TEMP/wb_ddr.reg
	echo "\"DirectDrawRenderer\"=\"$WINE_DDR\"" >> $TEMP/wb_ddr.reg
	regedit $TEMP/wb_ddr.reg

	simuler_reboot
	chmod 777 $TEMP -R
	rm $TEMP -R
	menu_winebooster
	fi
}

set_wb_uglsl ()
{
	A_UGLSL=`cat $prefixe/user.reg | grep "UseGLSL\"=" | sed -e {s/UseGLSL//g} -e {s/\"//g} -e {s/=//g}`
	if [ "$A_UGLSL" == "" ]; then
		UGLSL=$(champ "$LNG_WINEBOOSTER_UGLSL" "Y" 3 7 1 "WineBooster" ) ;
	else
		if [ "$A_UGLSL" == "enabled" ]; then
			A_UGLSL="Y"
		else
			A_UGLSL="N"
		fi
		UGLSL=$(champ "$LNG_WINEBOOSTER_UGLSL" "$A_UGLSL" 3 7 1 "WineBooster" ) ;
	fi
	if [ "$UGLSL" == "Y" ]; then
		WINE_GLSL="enabled"
	else
		WINE_GLSL="disabled"
	fi
	if [ "$WB_ITEM" != "$LNG_WB_ALL" ]; then	
	TEMP="$HOME/.PlayOnLinux/tmp/WineBooster"
	chmod 777 $TEMP -R
	rm $TEMP -R
	mkdir -p $TEMP

	echo "[HKEY_CURRENT_USER\Software\Wine\Direct3D]" > $TEMP/wb_uglsl.reg
	echo "\"UseGLSL\"=\"$WINE_GLSL\"" >> $TEMP/wb_uglsl.reg
	regedit $TEMP/wb_uglsl.reg

	simuler_reboot
	chmod 777 $TEMP -R
	rm $TEMP -R
	menu_winebooster
	fi
}

set_wb_vms ()
{
	A_VMS=`cat $prefixe/user.reg | grep "VideoMemorySize\"=" | sed -e {s/VideoMemorySize//g} -e {s/\"//g} -e {s/=//g}`
	if [ "$A_VMS" == "" ]; then
		VMS=$(champ "$LNG_WINEBOOSTER_VMS" "64" 4 7 1 "WineBooster" ) ;
	else
		VMS=$(champ "$LNG_WINEBOOSTER_VMS" "$A_VMS" 4 7 1 "WineBooster" ) ;
	fi
		if [ "$VMS" == "" ]; then
	WINE_VMS="64"
	else
		WINE_VMS=$VMS
	fi
	if [ "$WB_ITEM" != "$LNG_WB_ALL" ]; then
	TEMP="$HOME/.PlayOnLinux/tmp/WineBooster"
	chmod 777 $TEMP -R
	rm $TEMP -R
	mkdir -p $TEMP

	echo "[HKEY_CURRENT_USER\Software\Wine\Direct3D]" > $TEMP/wb_vms.reg
	echo "\"VideoMemorySize\"=\"$WINE_VMS\"" >> $TEMP/wb_vms.reg
	regedit $TEMP/wb_vms.reg

	simuler_reboot
	chmod 777 $TEMP -R
	rm $TEMP -R
	menu_winebooster
	fi
}

set_wb_orm ()
{
	A_ORM=`cat $prefixe/user.reg | grep "OffscreenRenderingMode\"=" | sed -e {s/OffscreenRenderingMode//g} -e {s/\"//g} -e {s/=//g}`
	if [ "$A_ORM" == "" ]; then
		ORM=$(champ "$LNG_WINEBOOSTER_ORM" "backbuffer" 5 7 1 "WineBooster" ) ;
	else
	ORM=$(champ "$LNG_WINEBOOSTER_ORM" "$A_ORM" 5 7 1 "WineBooster" ) ;
		fi
	if [ "$ORM" == "pbuffer" ]; then
		WINE_ORM="pbuffer"
	else if [ "$ORM" == "fbo" ]; then
		WINE_ORM="fbo"
	else
		WINE_ORM="backbuffer"
	fi
	fi
	if [ "$WB_ITEM" != "$LNG_WB_ALL" ]; then
	TEMP="$HOME/.PlayOnLinux/tmp/WineBooster"
	chmod 777 $TEMP -R
	rm $TEMP -R
	mkdir -p $TEMP

	echo "[HKEY_CURRENT_USER\Software\Wine\Direct3D]" > $TEMP/wb_orm.reg
	echo "\"OffscreenRenderingMode\"=\"$WINE_ORM\"" >> $TEMP/wb_orm.reg
	regedit $TEMP/wb_orm.reg

	simuler_reboot
	chmod 777 $TEMP -R
	rm $TEMP -R
	menu_winebooster
	fi
}

set_wb_rtlm ()
{
	A_RTLM=`cat $prefixe/user.reg | grep "RenderTargetLockMode\"=" | sed -e {s/RenderTargetLockMode//g} -e {s/\"//g} -e {s/=//g}`
	if [ "$A_RTLM" == "" ]; then
		RTLM=$(champ "$LNG_WINEBOOSTER_RTLM" "auto" 6 7 1 "WineBooster" ) ;
	else
		RTLM=$(champ "$LNG_WINEBOOSTER_RTLM" "$A_RTLM" 6 7 1 "WineBooster" ) ;
	fi
	if [ "$RTLM" == "disabled" ]; then
		WINE_RTLM="disabled"
	else if [ "$RTLM" == "readdraw" ]; then
		WINE_RTLM="readdraw"
	else if [ "$RTLM" == "readtex" ]; then
		WINE_RTLM="readtex"
	else if [ "$RTLM" == "texdraw" ]; then
		WINE_RTLM="texdraw"
	else if [ "$RTLM" == "textex" ]; then
		WINE_RTLM="textex"
	else
		WINE_RTLM="auto"
	fi
	fi
	fi
	fi
	fi
	if [ "$WB_ITEM" != "$LNG_WB_ALL" ]; then
	TEMP="$HOME/.PlayOnLinux/tmp/WineBooster"
	chmod 777 $TEMP -R
	rm $TEMP -R
	mkdir -p $TEMP

	echo "[HKEY_CURRENT_USER\Software\Wine\Direct3D]" > $TEMP/wb_rtlm.reg
	echo "\"RenderTargetLockMode\"=\"$WINE_RTLM\"" >> $TEMP/wb_rtlm.reg
	regedit $TEMP/wb_rtlm.reg

	simuler_reboot
	chmod 777 $TEMP -R
	rm $TEMP -R
	menu_winebooster
	fi
}

set_wb_all ()
{
	set_wb_ddr
	set_wb_uglsl
	set_wb_vms
	set_wb_orm
	set_wb_rtlm
	TEMP="$HOME/.PlayOnLinux/tmp/WineBooster"
	chmod 777 $TEMP -R
	rm $TEMP -R
	mkdir -p $TEMP

	echo "[HKEY_CURRENT_USER\Software\Wine\Direct3D]" > $TEMP/winebooster.reg
	echo "\"DirectDrawRenderer\"=\"$WINE_DDR\"" >> $TEMP/winebooster.reg
	echo "\"RenderTargetLockMode\"=\"$WINE_RTLM\"" >> $TEMP/winebooster.reg
	echo "\"OffscreenRenderingMode\"=\"$WINE_ORM\"" >> $TEMP/winebooster.reg
	echo "\"UseGLSL\"=\"$WINE_GLSL\"" >> $TEMP/winebooster.reg
	echo "\"VideoMemorySize\"=\"$WINE_VMS\"" >> $TEMP/winebooster.reg
	regedit $TEMP/winebooster.reg

	simuler_reboot
	chmod 777 $TEMP -R
	rm $TEMP -R
	menu_winebooster
}

menu_winebooster ()
{
	WB_ITEM=$(menu "$LNG_WINEBOOSTER_ITEM_CHOOSE" "$LNG_WB_ALL DirectDrawRenderer UseGLSL VideoMemorySize OffscreenRenderingMode RenderTargetLockMode" "WineBooster" 1 7 1 "" " ")
	if [ "$WB_ITEM" == "$LNG_WB_ALL" ]; then
		set_wb_all
	fi
	if [ "$WB_ITEM" == "DirectDrawRenderer" ]; then
		set_wb_ddr
	fi
	if [ "$WB_ITEM" == "UseGLSL" ]; then
		set_wb_uglsl
	fi
	if [ "$WB_ITEM" == "VideoMemorySize" ]; then
		set_wb_vms
	fi
	if [ "$WB_ITEM" == "OffscreenRenderingMode" ]; then
		set_wb_orm
	fi
	if [ "$WB_ITEM" == "RenderTargetLockMode" ]; then
		set_wb_rtlm
	fi
	exit
}

GAME=unset

prefixe=$(detect_wineprefix "$1")
if [ -e "$prefixe" ]
then
        select_prefixe "$prefixe"
	GAME=$1
fi
if [ "$GAME" != "" ]; then
	menu_winebooster
	message "Optimisation terminée" "WineBooster" 7 7 1
fi
exit
