#!/bin/bash

# Copyright (C) 2007-2010 PlayOnLinux Team
# Copyright (C) 2009 Łukasz Wojniłowicz
# Copyright (C) 2010 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


create_shortcut()
{
	POL_SetupWindow_question "$(eval_gettext "Would you like to create a shortcut to the application in PlayOnLinux?\nIf you patched a game, you should probably select no.")" "$(eval_gettext 'Manual installation')"
	reponse="$APP_ANSWER"
	if [ "$reponse" = "TRUE" ]
	then
		cd $REPERTOIRE/wineprefix/$GAMENAME/drive_c/

		POL_SetupWindow_browse "$(eval_gettext "Select the program's executable file.")" "$(eval_gettext 'Manual installation')" ""

		EXEC="$APP_ANSWER"
		EXECDIR=$(dirname "$EXEC")
		EXECDIR=${EXECDIR//"$REPERTOIRE"/""}
		DRIVE="/wineprefix/$GAMENAME/drive_c/"
		EXECDIR=${EXECDIR//"$DRIVE"/""}
	
		POL_SetupWindow_textbox "$(eval_gettext "Please choose a name for the shortcut.")" "$(eval_gettext 'Manual installation')" ""
		LAUNCHER=$APP_ANSWER
		if [ ! "$EXECDIR" = "" ]
			then
			if [ ! "$LAUNCHER" = "" ]
			then
				POL_SetupWindow_make_shortcut "$GAMENAME" "$EXECDIR" "$EXEC" "playonlinux.png" "$LAUNCHER"
				if [ -n "$WINEVERSIONLIVE" ] 
				then
					Set_WineVersion_Assign "$WINEVERSIONLIVE" "$LAUNCHER"
				fi
				#echo "$EXECDIR"
			fi
		fi
		create_shortcut
	fi
}
delete_prefixe()
{
	cd $REPERTOIRE/wineprefix/

	LIST_FILE=""
	for file in *
	do	
		if [ "$LIST_FILE" = "" ]
		then
			LIST_FILE="$file" 
		else
			LIST_FILE+="~$file"
		fi
	done
	
	POL_SetupWindow_menu "$(eval_gettext "What is your program's prefix?")" "$(eval_gettext 'Manual installation')" "$LIST_FILE" "~"
	GAMENAME="$APP_ANSWER"
	POL_SetupWindow_question "$(eval_gettext 'Are you sure you want to delete this prefix?') ($GAMENAME)" "$(eval_gettext 'Manual installation')"
	if [ "$APP_ANSWER" = "TRUE" ]
	then
		rm "$REPERTOIRE/wineprefix/$GAMENAME" -r
		POL_SetupWindow_message "$(eval_gettext "The prefix has been deleted.")" "$(eval_gettext 'Manual installation')"
	fi
	POL_SetupWindow_Close
	exit 0
}

POL_SetupWindow_Init
POL_SetupWindow_free_presentation "$(eval_gettext 'Manual installation')" "$(eval_gettext "Welcome to PlayOnLinux's manual installation wizard.\n\nThis script will allow you to install any program on PlayOnLinux and use it with all the tools (WineConfig, AdvancedWineConfiguration, Capture, etc.) as if it had been installed with an official script.\n\nWarning: We are unable to guarantee that your application will work perfectly.")"

LNG_LI_NEW=$(eval_gettext "Install a program in a new prefix")
LNG_LI_PATCH=$(eval_gettext "Edit an existing application")
LNG_LI_DELETE=$(eval_gettext "Delete a prefix")
POL_SetupWindow_menu "$(eval_gettext "What would you like to do?")" "$(eval_gettext 'Manual installation')" "$LNG_LI_NEW~$LNG_LI_PATCH~$LNG_LI_DELETE" "~"
menu="$APP_ANSWER"
if [ "$menu" = "" ]
then
	POL_SetupWindow_Close
	exit 0
fi
if [ "$menu" = "$LNG_LI_NEW" ]
then
	POL_SetupWindow_textbox "$(eval_gettext "Please type a name for your application's Wine prefix.\nThis name shouldn't contain spaces.")" "$(eval_gettext 'Manual installation')" ""
	GAMENAME="$APP_ANSWER"
fi
if [ "$menu" = "$LNG_LI_PATCH" ]
then
	cd $REPERTOIRE/wineprefix/
	LIST_FILE=$(read_prefixes)
	
	POL_SetupWindow_menu "$(eval_gettext "What is your program's prefix?")" "$(eval_gettext 'Manual installation')" "$LIST_FILE" "~"
	GAMENAME="$APP_ANSWER"
fi
if [ "$menu" = "$LNG_LI_DELETE" ]
then
	delete_prefixe
fi

if [ "$GAMENAME" = "" ]
then
	POL_SetupWindow_Close
	exit 1
fi

select_prefixe "$REPERTOIRE/wineprefix/$GAMENAME"

LNG_WC_ASSWV=$(eval_gettext "Assign a Wine version to a program")
LNG_WC_USEWT=$(eval_gettext "Use WineTricks")
LNG_WC_CONFWINE=$(eval_gettext "Configure Wine")
ITEMS="$LNG_WC_ASSWV~$LNG_WC_CONFWINE"
if [ "-e" "$REPERTOIRE/plugins/WineTricks/scripts/menu" ]
	then
		ITEMS+="~$LNG_WC_USEWT"
	fi

POL_SetupWindow_checkbox_list "$(eval_gettext "What would you like to do before installation?")" "$(eval_gettext 'Manual installation')" "$ITEMS" "~"
WHATTORUNBEFORE="$APP_ANSWER"

#Running AssignWine
if [ "$(echo $WHATTORUNBEFORE | grep -o "$LNG_WC_ASSWV")" == "$LNG_WC_ASSWV" ]
then
	WINEVERSIONLIST="System"
	WINEVERSIONDIRLIST=$(find $REPERTOIRE/WineVersions/* -maxdepth 0 -type d)
	for single in $WINEVERSIONDIRLIST ; do
	  singleparsed=${single##*/}
	  WINEVERSIONLIST=`echo $WINEVERSIONLIST~$singleparsed`
	done

	POL_SetupWindow_menu_list "$(eval_gettext "Which version of Wine would you like to use?")" "$(eval_gettext 'Manual installation')" "$WINEVERSIONLIST" "~"
	WINEVERSIONLIVE="$APP_ANSWER"
	if [ "$WINEVERSIONLIVE" = "System" ]
	then
		WINEVERSIONLIVE="--reset"
	fi
	Use_WineVersion "$WINEVERSIONLIVE"
fi

POL_SetupWindow_prefixcreate

#Running WineConfiguration
if [ "$(echo $WHATTORUNBEFORE | grep -o "$LNG_WC_CONFWINE")" == "$LNG_WC_CONFWINE" ]
then
	POL_SetupWindow_wait_next_signal "$(eval_gettext "Configure Wine")" "$(eval_gettext 'Manual installation')"
	winecfg
	POL_SetupWindow_detect_exit
fi

#Running WineTricks
if [ "$(echo $WHATTORUNBEFORE | grep -o "$LNG_WC_USEWT")" == "$LNG_WC_USEWT" ]
then
	bash "$REPERTOIRE/plugins/WineTricks/scripts/menu" "LiveInstall"
fi

cd $HOME
POL_SetupWindow_browse "$(eval_gettext "Please select the install file to run.")" "$(eval_gettext 'Manual installation')" 
INSTALLDIR="$APP_ANSWER"
if [ "$INSTALLDIR" = "" ]
then
	POL_SetupWindow_message "$(eval_gettext "You must choose a file!")" "$(eval_gettext 'Manual installation')"
	exit 1
fi
WORKINGINSTALLDIR=$(dirname "$INSTALLDIR")
cd $WORKINGINSTALLDIR
POL_SetupWindow_wait_next_signal "$(eval_gettext 'PlayOnLinux is installing your application...')" "$(eval_gettext 'Manual installation')"
extension=$(echo "$INSTALLDIR" | sed 's/.*\.//g')
if [ "$extension" = "msi" ]
then
wine msiexec /i "$INSTALLDIR"
else
wine "$INSTALLDIR"
fi
POL_SetupWindow_detect_exit 

POL_SetupWindow_message "$(eval_gettext 'Please do not press Next until the installation has been completed.')" "$(eval_gettext 'Manual installation')"
create_shortcut
POL_SetupWindow_Close
