#!/bin/bash

# Copyright (C) 2008 Pâris Quentin

# 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
pol_winecfg()
{
	POL_SetupWindow_wait_next_signal "$LNG_KPROCESSING" "$TITRE"
	wineserver -k
	POL_SetupWindow_wait_next_signal "$(eval_gettext 'Running wine configurator')" "$TITRE"
	winecfg
}
pol_kprocess()
{
	POL_SetupWindow_wait_next_signal "$LNG_KPROCESSING" "$TITRE"
	sleep 1
	wineserver -k
	sleep 1
}
pol_regedit()
{
	POL_SetupWindow_wait_next_signal "$LNG_KPROCESSING" "$TITRE"
	wineserver -k
	POL_SetupWindow_wait_next_signal "$(eval_gettext 'Running regedit')" "$TITRE"
	regedit
}
pol_wineboot()
{
	POL_SetupWindow_wait_next_signal "$LNG_KPROCESSING" "$TITRE"
	wineserver -k
	POL_SetupWindow_wait_next_signal "$(eval_gettext 'Simulating Windows reboot')" "$TITRE"
	wineboot
}
pol_awinecfg()
{
	POL_SetupWindow_wait_next_signal "$LNG_KPROCESSING" "$TITRE"
	wineserver -k
	POL_SetupWindow_wait_next_signal "$(eval_gettext 'Running advanced wine configurator')" "$TITRE"
	bash "$REPERTOIRE/plugins/Advanced Wine Configuration/scripts/menu"
}
pol_scfg()
{
	POL_SetupWindow_wait_next_signal "$LNG_KPROCESSING" "$TITRE"
	wineserver -k
	POL_SetupWindow_wait_next_signal"$(eval_gettext 'Running specific wine configurator')" "$TITRE"
	bash "$REPERTOIRE/configurations/configurators/$APP"
}
main_menu()
{
	LNG_CONFIGURE=$(eval_gettext "Configure wine")
	LNG_REGISTRY=$(eval_gettext "Registry Editor")
	LNG_KPROCESS=$(eval_gettext "Kill all the prefix process")
	LNG_UPDATEPREFIX=$(eval_gettext "Update the wineprefix")
	LNG_WINDOWS_REBOOT=$(eval_gettext "Simulate Windows reboot")
	LNG_APLUGIN=$(eval_gettext "Use advanced wine configuration plugin")
	LNG_POLCFG=$(eval_gettext "Use PlayOnLinux's configurator for")

	ITEMS="$LNG_CONFIGURE~$LNG_REGISTRY~$LNG_KPROCESS~$LNG_UPDATEPREFIX~$LNG_WINDOWS_REBOOT"
	if [ "-e" "$REPERTOIRE/plugins/Advanced Wine Configuration" ]
	then
		ITEMS+="~$LNG_APLUGIN"
	fi

	if [ "-e" "$REPERTOIRE/configurations/configurators/$APP" ]
	then
		ITEMS+="~$LNG_POLCFG $APP"
	fi
	POL_SetupWindow_menu "$ACTION" "$TITRE" "$ITEMS" "~"
	if [ "$APP_ANSWER" = "$LNG_CONFIGURE" ]
	then
		pol_winecfg
	fi
	if [ "$APP_ANSWER" = "$LNG_REGISTRY" ]
	then
		pol_regedit
	fi
	if [ "$APP_ANSWER" = "$LNG_KPROCESS" ]
	then
		pol_kprocess
	fi
	if [ "$APP_ANSWER" = "$LNG_UPDATEPREFIX" ]
	then
		POL_SetupWindow_prefixcreate
	fi
	if [ "$APP_ANSWER" = "$LNG_WINDOWS_REBOOT" ]
	then
		pol_wineboot
	fi
	if [ "$APP_ANSWER" = "$LNG_APLUGIN" ]
	then
		pol_awinecfg
	fi
	if [ "$APP_ANSWER" = "$LNG_POLCFG $APP" ]
	then
		pol_scfg
	fi
}

APP="$1"
prefixe=$(detect_wineprefix "$1")
export WINEPREFIX="$prefixe"
LNG_KPROCESSING=$(eval_gettext "Killing process in the prefix ..")
TITLE=$(eval_gettext "PlayOnLinux applications configurator")
INTRO=$(eval_gettext "Welcome into PlayOnLinux applications configurator.")
INTRO1=$(eval_gettext "This wizard will help you to configure :")
INTRO2=$(eval_gettext "The following wine prefix will be updated :")
INTRO3=$(eval_gettext "Be carreful ! PlayOnLinux will need to kill all the programs running in this prefix !")

ACTION=$(eval_gettext "Please choose an action")
POL_SetupWindow_Init
POL_SetupWindow_free_presentation "$TITLE" "$INTRO\n$INTRO1 $1\n\n$INTRO2 $prefixe\n\n$INTRO3"
while [ 1 ]
do
main_menu
sleep 1
done
POL_SetupWindow_Close
exit 
