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

message "$LNG_WINEBOOSTER_WELCOME1"
message "$LNG_WINEBOOSTER_WELCOME2"
message "$LNG_WINEBOOSTER_WELCOME3"

GAME=$(menu "$LNG_WINEBOOSTER_GAME_CHOOSE" "$(ls $REPERTOIRE/wineprefix)" 400 300);
if [ "$GAME" = "" ]
then
	erreur "$LNG_WINEBOOSTER_GAME_ERROR"
exit 0
fi
verifier_installation_e "$GAME"

A_WVERSION=`cat $REPERTOIRE/jeux/$GAME | grep "Version" | awk {'print $2'} | tr -d '\' | sed -e {s/Version//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_WVERSION" == "" ]; then
WVERSION=$(champ "$LNG_WINEBOOSTER_WVERSION" "winxp" ) ;
else
WVERSION=$(champ "$LNG_WINEBOOSTER_WVERSION" "$A_WVERSION" ) ;
fi
if [ "$WVERSION" == "vista" ]; then
WINE_WVERSION="vista"
else if [ "$WVERSION" == "win2003" ]; then
WINE_WVERSION="win2003"
else if [ "$WVERSION" == "winxp" ]; then
WINE_WVERSION="winxp"
else if [ "$WVERSION" == "win2k" ]; then
WINE_WVERSION="win2k"
else if [ "$WVERSION" == "winnt" ]; then
WINE_WVERSION="winnt"
else if [ "$WVERSION" == "winme" ]; then
WINE_WVERSION="winme"
else if [ "$WVERSION" == "win98" ]; then
WINE_WVERSION="win98"
else if [ "$WVERSION" == "win95" ]; then
WINE_WVERSION="win95"
else if [ "$WVERSION" == "win31" ]; then
WINE_WVERSION="win31"
else
WINE_WVERSION="winxp"
fi
fi
fi
fi
fi
fi
fi
fi
fi

A_DDR=`cat $REPERTOIRE/jeux/$GAME | grep "DirectDrawRenderer" | awk {'print $2'} | tr -d '\' | sed -e {s/DirectDrawRenderer//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_DDR" == "" ]; then
DDR=$(champ "$LNG_WINEBOOSTER_DDR" "gdi" ) ;
else
DDR=$(champ "$LNG_WINEBOOSTER_DDR" "$A_DDR" ) ;
fi
if [ "$DDR" == "opengl" ]; then
WINE_DDR="opengl"
else
WINE_DDR="gdi"
fi

A_VERTEXSHADER=`cat $REPERTOIRE/jeux/$GAME | grep "VertexShaderMode" | awk {'print $2'} | tr -d '\' | sed -e {s/VertexShaderMode//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_VERTEXSHADER" == "" ]; then
VERTEXSHADER=$(champ "$LNG_WINEBOOSTER_VERTEXSHADER" "N" ) ;
else
VERTEXSHADER=$(champ "$LNG_WINEBOOSTER_VERTEXSHADER" "$A_VERTEXSHADER" ) ;
fi
if [ "$VERTEXSHADER" == "Y" ]; then
WINE_VS="enabled"
else
WINE_VS="disabled"
fi

A_PIXELSHADER=`cat $REPERTOIRE/jeux/$GAME | grep "PixelShaderMode" | awk {'print $2'} | tr -d '\' | sed -e {s/PixelShaderMode//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_PIXELSHADER" == "" ]; then
PIXELSHADER=$(champ "$LNG_WINEBOOSTER_PIXELSHADER" "N" ) ;
else
PIXELSHADER=$(champ "$LNG_WINEBOOSTER_PIXELSHADER" "$A_PIXELSHADER" ) ;
fi
if [ "$VERTEXSHADER" == "Y" ]; then
WINE_PS="enabled"
else
WINE_PS="disabled"
fi

A_UGLSL=`cat $REPERTOIRE/jeux/$GAME | grep "UseGLSL" | awk {'print $2'} | tr -d '\' | sed -e {s/UseGLSL//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_UGLSL" == "" ]; then
UGLSL=$(champ "$LNG_WINEBOOSTER_UGLSL" "N" ) ;
else
UGLSL=$(champ "$LNG_WINEBOOSTER_UGLSL" "$A_UGLSL" ) ;
fi
if [ "$UGLSL" == "Y" ]; then
WINE_GLSL="enabled"
else
WINE_GLSL="disabled"
fi

A_VMS=`cat $REPERTOIRE/jeux/$GAME | grep "VideoMemorySize" | awk {'print $2'} | tr -d '\' | sed -e {s/VideoMemorySize//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_VMS" == "" ]; then
VMS=$(champ "$LNG_WINEBOOSTER_VMS" "64" ) ;
else
VMS=$(champ "$LNG_WINEBOOSTER_VMS" "$A_VMS" ) ;
fi
if [ "$VMS" == "" ]; then
WINE_VMS="64"
else
WINE_VMS=$VMS
fi

A_ORM=`cat $REPERTOIRE/jeux/$GAME | grep "OffscreenRenderingMode" | awk {'print $2'} | tr -d '\' | sed -e {s/OffscreenRenderingMode//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_ORM" == "" ]; then
ORM=$(champ "$LNG_WINEBOOSTER_ORM" "backbuffer" ) ;
else
ORM=$(champ "$LNG_WINEBOOSTER_ORM" "$A_ORM" ) ;
fi
if [ "$ORM" == "pbuffer" ]; then
WINE_ORM="pbuffer"
else if [ "$ORM" == "fbo" ]; then
WINE_ORM="fbo"
else
WINE_ORM="backbuffer"
fi
fi

A_RTLM=`cat $REPERTOIRE/jeux/$GAME | grep "RenderTargetLockMode" | awk {'print $2'} | tr -d '\' | sed -e {s/RenderTargetLockMode//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_RTLM" == "" ]; then
RTLM=$(champ "$LNG_WINEMASTER_RTLM" "auto" ) ;
else
RTLM=$(champ "$LNG_WINEMASTER_RTLM" "$A_RTLM" ) ;
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

A_WDESKTOP=`cat $REPERTOIRE/jeux/$GAME | grep "Desktop" | awk {'print $2'} | tr -d '\' | sed -e {s/Desktop//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_WDESKTOP" == "" ]; then
WDESKTOP=$(champ "$LNG_WINEBOOSTER_WDESKTOP" "0" ) ;
else
WDESKTOP=$(champ "$LNG_WINEBOOSTER_WDESKTOP" "$A_WDESKTOP" ) ;
fi
if [ "$WDESKTOP" == "0" ]; then
WINE_DESKTOP="0"
else
WINE_DESKTOP=$WDESKTOP
fi

A_WMANAGED=`cat $REPERTOIRE/jeux/$GAME | grep "Managed" | awk {'print $2'} | tr -d '\' | sed -e {s/Managed//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_WMANAGED" == "" ]; then
WMANAGED=$(champ "$LNG_WINEBOOSTER_WMANAGED" "Y" ) ;
else
WMANAGED=$(champ "$LNG_WINEBOOSTER_WMANAGED" "$A_WMANAGED" ) ;
fi
if [ "$WMANAGED" == "Y" ]; then
WINE_MANAGED="Y"
else
WINE_MANAGED="N"
fi

A_WDXGRAB=`cat $REPERTOIRE/jeux/$GAME | grep "DXGrab" | awk {'print $2'} | tr -d '\' | sed -e {s/DXGrab//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_WDXGRAB" == "" ]; then
WDXGRAB=$(champ "$LNG_WINEBOOSTER_WDXGRAB" "N" ) ;
else
WDXGRAB=$(champ "$LNG_WINEBOOSTER_WDXGRAB" "$A_WDXGRAB" ) ;
fi
if [ "$WDXGRAB" == "Y" ]; then
WINE_DXGRAB="Y"
else
WINE_DXGRAB="N"
fi

A_WAUDIO=`cat $REPERTOIRE/jeux/$GAME | grep "Audio" | awk {'print $2'} | tr -d '\' | sed -e {s/Audio//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_WAUDIO" == "" ]; then
WAUDIO=$(champ "$LNG_WINEBOOSTER_WAUDIO" "alsa" ) ;
else
WAUDIO=$(champ "$LNG_WINEBOOSTER_WAUDIO" "$A_WAUDIO" ) ;
fi
if [ "$WAUDIO" == "oss" ]; then
WINE_AUDIO="oss"
else
WINE_AUDIO="alsa"
fi

A_AEMUL=`cat $REPERTOIRE/jeux/$GAME | grep "EmulDriver" | awk {'print $2'} | tr -d '\' | sed -e {s/EmulDriver//g} -e {s/\"//g} -e {s/=//g}`
if [ "$A_AEMUL" == "" ]; then
AEMUL=$(champ "$LNG_WINEBOOSTER_AEMUL" "N" ) ;
else
AEMUL=$(champ "$LNG_WINEBOOSTER_AEMUL" "$A_AEMUL" ) ;
fi
if [ "$AEMUL" == "Y" ]; then
WINE_AEMUL="Y"
else
WINE_AEMUL="N"
fi

TEMP="$HOME/.PlayOnLinux/tmp/WineBooster"
chmod 777 $TEMP -R
rm $TEMP -R
mkdir -p $TEMP
cd $REPERTOIRE/wineprefix/$GAME
select_prefixe "$(pwd)"

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
echo "\"PixelShaderMode\"=\"$WINE_PS\"" >> $TEMP/winebooster.reg
echo "\"VertexShaderMode\"=\"$WINE_VS\"" >> $TEMP/winebooster.reg
echo "[HKEY_CURRENT_USER\Software\Wine\Driver]" >> $TEMP/winebooster.reg
echo "\"Audio\"=\"$WINE_AUDIO\"" >> $TEMP/winebooster.reg
echo "[HKEY_CURRENT_USER\Software\Wine\DirectSound]" >> $TEMP/winebooster.reg
echo "\"EmulDriver\"=\"$WINE_AEMUL\"" >> $TEMP/winebooster.reg
echo "[HKEY_CURRENT_USER\Software\Wine\X11 Driver]" >> $TEMP/winebooster.reg
echo "\"Desktop\"=\"$WINE_DESKTOP\"" >> $TEMP/winebooster.reg
echo "\"Managed\"=\"$WINE_MANAGED\"" >> $TEMP/winebooster.reg
echo "\"DXGrab\"=\"$WINE_DXGRAB\"" >> $TEMP/winebooster.reg
echo "[HKEY_CURRENT_USER\Software\Wine]" >> $TEMP/winebooster.reg
echo "\"Version\"=\"$WINE_WVERSION\"" >> $TEMP/winebooster.reg
regedit $TEMP/winebooster.reg

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

message "Optimisation terminée"
exit 
