#!/bin/bash

# Copyright (C) 2007-2010 PlayOnLinux Team
# Copyright (C) 2007-2011 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. 

# This is the first use script
[ "$PLAYONLINUX" = "" ] && exit 0
source "$PLAYONLINUX/lib/sources" 

export XQUARTZ_VERSION_LEO="2.7.1"
export XQUARTZ_VERSION="2.7.5"

install_fonts () # Install Microsoft Fonts
{
	if [ ! -e "$REPERTOIRE/configurations/msfonts_installed" ]; then
		WebVersion=$($POL_WGET $SITE/check.txt -O-)
		if [ "$WebVersion" == "" ]; then
			POL_SetupWindow_message "$(eval_gettext 'The PlayOnLinux website is unavailable.')$(eval_gettext "\nPlease make sure you are connected to the Internet.")" "$(eval_gettext 'Microsoft fonts')"
			POL_SetupWindow_Close
			return 1
		else
			POL_Call POL_Internal_InstallFonts || return 1
		fi	
	fi
	return 0
}


POL_Internal_XQuartz_Warning()
{
		cat << EOF_X > "$REPERTOIRE/tmp/xquartz"
$(eval_gettext "Please read this carefully")

$(eval_gettext 'XQuartz $XQUARTZ_VERSION has been installed on your computer.')
$(eval_gettext "However, it won't be activated until you reboot your computer.")

$(eval_gettext 'Please reboot your computer now, and run PlayOnMac')

EOF_X
		POL_SetupWindow_licence "$(eval_gettext "Pleaase read this carefully.")" "$STEP_TITLE" "$REPERTOIRE/tmp/xquartz"
		POL_SetupWindow_Close
		killall playonmac.real
		touch /tmp/playonmac_needs_reboot
}

install_x11()
{
	if [ "$OSX_VERSION" = "9" ]
	then
		XQuartzUrl="http://xquartz.macosforge.org/downloads/Leopard/X11-$XQUARTZ_VERSION_LEO.dmg"
		FilePrefix="X11"
	else
		XQuartzUrl="http://xquartz.macosforge.org/downloads/SL/XQuartz-$XQUARTZ_VERSION.dmg"
		FilePrefix="XQuartz"
	fi
	STEP_TITLE="XQuartz"
	if [ "$OSX_VERSION" = "10" ]
	then
		XQuartz_Message="$(eval_gettext "To ensure that your games will be compatible, PlayOnMac must install XQuartz")"
	else
		XQuartz_Message="$(eval_gettext "PlayOnMac needs to install XQuartz to work")"
	fi
	POL_SetupWindow_menu_num "$XQuartz_Message" "$STEP_TITLE" "$(eval_gettext "Please download XQuartz for me")~$(eval_gettext "I've downloaded the file by myself") ($FilePrefix-$XQUARTZ_VERSION.dmg)~$(eval_gettext "Don't install XQuartz for the moment")" "~"
	if [ "$APP_ANSWER" = "0" ]
	then
		POL_SetupWindow_download "$(eval_gettext "Downloading XQuartz")" "$STEP_TITLE" "$XQuartzUrl"
		got_xquartz="true"
	fi
	if [ "$APP_ANSWER" = "1" ]
	then
		POL_SetupWindow_browse "$(eval_gettext "Where is") $FilePrefix-$XQUARTZ_VERSION.dmg ?" "$STEP_TITLE"
		cd "$(dirname "$APP_ANSWER")"
		got_xquartz="true"
	fi
	
	if [ "$got_xquartz" = "true" ]
	then
		POL_SetupWindow_wait_next_signal "$(eval_gettext "Installing XQuartz...")" "$STEP_TITLE"
		open $FilePrefix-$XQUARTZ_VERSION.dmg
		while [ ! -d "/Volumes/$FilePrefix-$XQUARTZ_VERSION" ]
		do
			sleep 1
		done
		/System/Library/CoreServices/Installer.app/Contents/MacOS/Installer /Volumes/$FilePrefix-$XQUARTZ_VERSION/$FilePrefix.pkg
		umount /Volumes/$FilePrefix-$XQUARTZ_VERSION
		POL_Internal_RunXQuartz
		
		if [ ! "FORCE_X11_Reinstall" = "True" ]; then
			POL_Internal_XQuartz_Warning
		fi
		POL_SetupWindow_message "$(eval_gettext "XQuartz has been installed.")" "$STEP_TITLE"
	fi
	

	if [ ! -e "/Applications/Utilities/XQuartz.app" ] && [ ! "$OSX_VERSION" = "11" ]
	then
		POL_SetupWindow_message "$(eval_gettext "Unable to install XQuartz! You must install it to improve games compatibility.")" "$STEP_TITLE"
	fi
	#POL_Internal_XQuartz_Warning

}

install_prefix()
{
	if [ ! -e "$POL_USER_ROOT/wineprefix/default" ]
	then
		STEP_TITLE="$(eval_gettext "Default virtual drive configuration")"
		POL_SetupWindow_wait_next_signal "$(eval_gettext '$APPLICATION_TITLE is making a virtual windows hard drive')" "$STEP_TITLE" 
		POL_Wine_SelectPrefix "default"
		POL_Wine_PrefixCreate
	fi
}
POL_Shortcut_GetOLDPrefix()
{
	[ "$1" = "" ] && POL_Debug_Error "No shortcut specified"
	fichier="$REPERTOIRE/configurations/installed/$1"
	#fichier=${fichier//"\""/""}
	#fichier=${fichier//"//"/"/"}
	if [ -e "$fichier" ]
	then
		fichier="$(cat "$fichier" | grep WINEPREFIX | tail -n 1 | sed s/'\/\//\/'/)"
		fichier="${fichier/"$POL_USER_ROOT"/""}"
		fichier="$(printf "$fichier" | cut -d "/" -f2)"
	fi
	printf "$fichier" | tr -d \"
}
POL_Shortcut_GetOLDVersion()
{
	[ "$1" = "" ] && POL_Debug_Error "No shortcut specified"
	fichier="$REPERTOIRE/configurations/installed/$1"
	#fichier=${fichier//"\""/""}
	#fichier=${fichier//"//"/"/"}
	if [ -e "$fichier" ]
	then
		fichier="$(cat "$fichier" | grep PATH | tail -n 1 | sed s/'\/\//\/'/)"
		fichier="${fichier/"$POL_USER_ROOT"/""}"
		fichier="$(printf "$fichier" | cut -d "/" -f2)"
	fi
	printf "$fichier" | tr -d \"
}
migrate_oldpol()
{
	rmdir "$POL_USER_ROOT/configurations/installed" 2> /dev/null
	if [ -e "$POL_USER_ROOT/configurations/installed" ]
	then
		# On commence par les raccourcis
		POL_Debug_Message "Upgrading shortcuts"
		
		cd "$POL_USER_ROOT/configurations/installed"
		for shortcut in *
		do
			POL_Debug_Message "Updating shortcut : $shortcut"
			sVERSION="$(POL_Shortcut_GetOLDVersion "$shortcut")"
			sPREFIX="$(POL_Shortcut_GetOLDPrefix "$shortcut")"
			sTITLE="$shortcut"
			sWINE="$(cat "$shortcut" | grep '^wine')"
			sWINE="${sWINE/wine/POL_Wine}"
			sWINEPREFIX="$(cat "$shortcut" | grep '^export WINEPREFIX')"
			sCD="$(cat "$shortcut" | grep '^cd')"
		
			cat <<EOF > "$POL_USER_ROOT/shortcuts/$shortcut"
#!/bin/bash
[ "\$PLAYONLINUX" = "" ] && exit 0
source "\$PLAYONLINUX/lib/sources"
$sWINEPREFIX
export WINEDEBUG="-all"
$sCD
$sWINE
EOF
			chmod +x  "$POL_USER_ROOT/shortcuts/$shortcut"
			if [ ! "$sVERSION" = "" ]
			then
				POL_Debug_Message "Setting shortcut $sTITLE, prefix $sPREFIX to version $sVERSION"
				export WINEPREFIX="$POL_USER_ROOT/wineprefix/$sPREFIX"
				POL_Wine_SetVersionPrefix "$sVERSION"
			fi
		done
		
		
		POL_Debug_Message "Upgrading virtual drives"
		cd "$POL_USER_ROOT/wineprefix"
		for prefix in *
		do
			POL_Debug_Message "Updating $prefix"
			export WINEPREFIX="$POL_USER_ROOT/wineprefix/$prefix"
			[ "$(POL_Config_PrefixRead ARCH)" = "" ] && POL_Config_PrefixWrite "ARCH" "x86"
		done
	
		if [ -e "$POL_USER_ROOT/WineVersions" ]
		then
		
		POL_Debug_Message "Upgrading wine versions"
		cd "$POL_USER_ROOT/WineVersions"
		[ "$POL_OS" = "Mac" ] && vprefix="darwin-"
		[ "$POL_OS" = "Linux" ] && vprefix="linux-"
		new_vroot="$POL_USER_ROOT/wine/${vprefix}x86"
		mkdir -p "$new_vroot"
		for version in *
		do
			POL_Debug_Message "Updating $version"
			mv "$POL_USER_ROOT/WineVersions/$version" "$new_vroot/"
		done
		rmdir "$POL_USER_ROOT/WineVersions"
		
		if [ "$POL_OS" = "Linux" ]
		then
			cd "$new_vroot"
			for version in *
			do
				POL_Debug_Message "Removing /usr in path $version"
				cd "$new_vroot/$version"
				mv usr/* ./
				rmdir usr
			done
		fi
	fi
	mv "$POL_USER_ROOT/configurations/installed" "$POL_USER_ROOT/configurations/installed.backup" 
	fi
	export POL_WINEVERSION=""
}
migrate_oldpol

POL_SetupWindow_Init --protect
if [ "$(POL_Config_Read FIRST_FIRST_USE)" = "TRUE" ]
then
	POL_SetupWindow_free_presentation "$(eval_gettext '$APPLICATION_TITLE first use')" "$(eval_gettext '$APPLICATION_TITLE is going to finish its preparation')\n$(eval_gettext "Please ensure you are connected to the internet.")"
else
	POL_Config_Write FIRST_FIRST_USE TRUE
	POL_SetupWindow_free_presentation "$(eval_gettext '$APPLICATION_TITLE first use')" "$(eval_gettext 'It appears it is the first time you are using 	$APPLICATION_TITLE.')\n$(eval_gettext 'This wizard will help you prepare $APPLICATION_TITLE environment')\n$(eval_gettext "Please ensure you are connected to the internet.")"
fi

[ "$POL_OS" == "Mac" ] && [ ! -e "/Applications/Utilities/XQuartz.app" ] && install_x11
[ "$POL_OS" == "Mac" ] && [ "$OSX_VERSION" = "9" ] &&  install_x11

if [ "$DEBIAN_PACKAGE" = "TRUE" ]; then
	fonts_install
else
	if ! install_fonts; then
		POL_SetupWindow_message "$(eval_gettext 'Error while installing fonts')" "$TITLE"
		POL_SetupWindow_Close
		exit 0
	fi
fi

if [ "$POL_OS" = "Mac" ]; then
	if [ "$OSX_VERSION" = "11" -a "$OSX_SUB_VERSION" -gt "4" ] || [ "$OSX_VERSION" = "12" -a "$OSX_SUB_VERSION" -gt "1" ]; then
		# XQuartz 2.7.4 is needed here... See http://bugs.winehq.org/show_bug.cgi?id=31751
		XVERSION="$(cat "/Applications/Utilities/XQuartz.app/Contents/Info.plist" | grep -A1 "<key>CFBundleShortVersionString</key>" | tail -n1 | cut -d '>' -f2 | cut -d '<' -f1)"
		if VersionLower "$XVERSION" "2.7.4"; then
			export FORCE_X11_Reinstall="True"
			install_x11
		fi
	fi
fi 


POL_SetupWindow_message "$(eval_gettext 'Enjoy using $APPLICATION_TITLE')" "$(eval_gettext '$APPLICATION_TITLE first use')"
POL_SetupWindow_Close
POL_Config_Write FIRST_USE TRUE
POL_Config_Write GECKO_INSTALLED TRUE
exit 0