#!/usr/bin/env bash

# Copyright (C) 2007-2010 PlayOnLinux Team
# Copyright (C) 2008 Pâris Quentin
# Copyright (C) 2009 Łukasz Wojniłowicz

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

[ "$PLAYONLINUX" = "" ] && exit 0
source "$PLAYONLINUX/lib/sources"
TITLE="$(eval_gettext '$APPLICATION_TITLE Application Configurator')"

polconfigurator_winecfg()
{
        POL_Wine_AutoSetVersionEnv
	wineserver -k
	POL_Wine winecfg
}
polconfigurator_kprocess()
{
	if [ "$1" ]; then
		POL_SetupWindow_Init
		POL_SetupWindow_wait_next_signal "$(eval_gettext "Killing programs in the virtual drive...")" "$TITLE"
	fi
		
	sleep 1
        POL_Wine_AutoSetVersionEnv
	wineserver -k
	sleep 1

	if [ "$1" ]; then
		POL_SetupWindow_Close
	fi
	
}
polconfigurator_regedit()
{
        POL_Wine_AutoSetVersionEnv
	wineserver -k
	POL_Wine regedit
}
polconfigurator_wineboot()
{
        POL_Wine_AutoSetVersionEnv
	wineserver -k
	POL_Wine wineboot
}
polconfigurator_awinecfg()
{
        POL_Wine_AutoSetVersionEnv
	wineserver -k
	bash "$POL_USER_ROOT/plugins/Advanced Wine Configuration/scripts/menu"
}
polconfigurator_winetricks()
{
        POL_Wine_AutoSetVersionEnv
	wineserver -k
	bash "$POL_USER_ROOT/plugins/WineTricks/scripts/menu" "$1"
}
polconfigurator_scfg()
{
        POL_Wine_AutoSetVersionEnv
	wineserver -k
	bash "$POL_USER_ROOT/configurations/configurators/$APP"
}
polconfigurator_changeicon()
{
	
	if [ "$1" ]; then	
	    (convert "$1" -geometry 32X32 "$POL_USER_ROOT/icones/32/$APP" && touch "$POL_USER_ROOT/icones/32/tmp" && sleep 1 && rm "$POL_USER_ROOT/icones/32/tmp")&
	    exit
	else
	    cd "$HOME/.local/share/icons/"
	    POL_SetupWindow_browse "$(eval_gettext "Please select an icon file")" "$(eval_gettext "Icon selection")" 
	    iconame=$(basename "$APP_ANSWER")
	    extensionwithoutcase=${iconame##*.}
	    extension=`echo "$extensionwithoutcase" | tr '[:upper:]' '[:lower:]'`
	    if [ "$iconame" != "" ] && [ "$extension" = ico ] || [ "$extension" = xpm ] || [ "$extension" = png ] || [ "$extension" = jpg ] || [ "$extension" = jpeg ] || [ "$extension" = bmp ]; then
	        convert "$APP_ANSWER" -geometry 32X32 "$POL_USER_ROOT/icones/32/$APP"
	    else
	        POL_SetupWindow_message_image "$extension $(eval_gettext "is not an icon extension")" "$(eval_gettext "Bad file extension")" "$PLAYONLINUX/themes/tango/warning.png"
	        return
	    fi
	fi
	touch "$POL_USER_ROOT/icones/32/tmp"
	sleep 1
	rm "$POL_USER_ROOT/icones/32/tmp"
}


main_menu()
{
    local LNG_CONFIGURE=$(eval_gettext "Configure Wine")
    local LNG_REGISTRY=$(eval_gettext "Registry Editor")
    local LNG_KPROCESS=$(eval_gettext "Kill all prefix processes")
    local LNG_UPDATEPREFIX=$(eval_gettext "Update wineprefix")
    local LNG_WINDOWS_REBOOT=$(eval_gettext "Simulate Windows reboot")
    local LNG_APLUGIN=$(eval_gettext "Use Advanced Wine Configuration plugin")
    local LNG_POLCFG=$(eval_gettext "Use PlayOnLinux's configurator for")
	
    local LNG_CHANGEICON=$(eval_gettext 'Change the icon')
    local LNG_WINETRICKS=$(eval_gettext "Use WineTricks")
    
    # ~$LNG_UPDATEPREFIX
	local ITEMS="$LNG_CONFIGURE~$LNG_REGISTRY~$LNG_KPROCESS~$LNG_WINDOWS_REBOOT~$LNG_CHANGEICON"
	if [ -e "$POL_USER_ROOT/plugins/Advanced Wine Configuration" ]
	then
		ITEMS+="~$LNG_APLUGIN"
	fi

	if [ -e "$POL_USER_ROOT/plugins/WineTricks" ]
	then
		ITEMS+="~$LNG_WINETRICKS"
	fi

	if [ -e "$POL_USER_ROOT/configurations/configurators/$APP" ]
	then
		ITEMS+="~$LNG_POLCFG $APP"
	fi
        local funct=""
        while [ -z "$funct" ]; do
	    POL_SetupWindow_menu "$(eval_gettext "Please choose an action to perform")" "$TITLE" "$ITEMS" "~"
            [ "$APP_ANSWER" = "$LNG_CONFIGURE" ] && funct=winecfg
            [ "$APP_ANSWER" = "$LNG_REGISTRY" ] && funct=regedit
            [ "$APP_ANSWER" = "$LNG_KPROCESS" ] && funct=kprocess
            # [ "$APP_ANSWER" = "$LNG_UPDATEPREFIX" ] && funct="??"
            [ "$APP_ANSWER" = "$LNG_WINDOWS_REBOOT" ] && funct=wineboot
            [ "$APP_ANSWER" = "$LNG_CHANGEICON" ] && funct=changeicon
            [ "$APP_ANSWER" = "$LNG_APLUGIN" ] && funct=awinecfg
            [ "$APP_ANSWER" = "$LNG_WINETRICKS" ] && funct=winetricks
            [ "$APP_ANSWER" = "$LNG_POLCFG" ] && funct=scfg
        done
        
	redirect_to_pol_configure_function "$APP" "$funct"
}
redirect_to_pol_configure_function()
{
    local APP="$1"
    local FUNCTIONTORUN="$2"
    shift 2
    polconfigurator_"$FUNCTIONTORUN" "$@"
}

prefixe=$(detect_wineprefix "$1")
export WINEPREFIX="$prefixe"
redirect_to_pol_configure_function "$@"

exit
