#!/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="2.7.1"


install_fonts () # Install Microsoft Fonts
{
	if [ ! -e "$REPERTOIRE/configurations/msfonts_installed" ]
	then
		
	#	POL_SetupWindow_Init "$PLAYONLINUX/etc/setups/fonts/top.png" "$PLAYONLINUX/etc/setups/fonts/left.jpg"
		POL_SetupWindow_message "$(eval_gettext "Microsoft fonts aren't installed; I'll install them for you.")$(eval_gettext "\nPlease make sure you are connected to the Internet.")" "$(eval_gettext 'Microsoft fonts')" 
		echo -e "---$(eval_gettext " Licence translated into your language ")---
$(eval_gettext "These fonts were provided by Microsoft\n\"in the interest of cross-platform compatibility\".")
$(eval_gettext "This is no longer the case, but they are still available from third parties.")

$(eval_gettext "You are free to download these fonts and use them for your own use,\nbut you may not redistribute them in modified form,\nincluding changes to the file name or packaging format.")\n" > "$REPERTOIRE/tmp/licence"
		echo "---$(eval_gettext " Original licence ")---
These fonts were provided by Microsoft \"in the interest of
cross-platform compatibility\".
This is no longer the case, but they are still available from
third parties.

You are free to download these fonts and use them for your own use,
but you may not redistribute them in modified form, including changes
to the file name or packaging format." >> "$REPERTOIRE/tmp/licence"
		cat "$REPERTOIRE/tmp/licence"

		POL_SetupWindow_licence "$(eval_gettext 'Please read and accept the following:')" "$(eval_gettext 'Microsoft fonts')" "$REPERTOIRE/tmp/licence"
		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
		else
			rm -r "$REPERTOIRE/fonts/" 2> /dev/null
			rm "$REPERTOIRE/fonts" 2> /dev/null
			rm "$REPERTOIRE/configurations/fonts_installed" 2> /dev/null
			FONTDIR="$REPERTOIRE/fonts/"
			mkdir -p "$FONTDIR"
			FONTS_INSTALLER="andale32.exe arialb32.exe arial32.exe comic32.exe courie32.exe georgi32.exe impact32.exe times32.exe trebuc32.exe verdan32.exe webdin32.exe"
			cd "$FONTDIR"
			PULSE=1
			POL_SetupWindow_pulsebar "$(eval_gettext 'Downloading fonts')" "$(eval_gettext 'Microsoft fonts')"

			for font in $FONTS_INSTALLER
			do
			 sleep 0.1
			 POL_SetupWindow_set_text "$(eval_gettext 'Downloading: ')$font..."
			 echo "$(eval_gettext 'Downloading: ')$font..."
			 $POL_WGET "http://heanet.dl.sourceforge.net/sourceforge/corefonts/$font"
			 PULSE=$(( PULSE + 8 ))
			 POL_SetupWindow_pulse $PULSE
			done

			echo "$(eval_gettext 'Downloading: ')tahoma32.exe"
			POL_SetupWindow_set_text "$(eval_gettext 'Downloading: ')tahoma32.exe"
			$POL_WGET http://download.microsoft.com/download/office97pro/fonts/1/w95/en-us/tahoma32.exe
			POL_SetupWindow_pulse 100
			sleep 1
			PULSE=1
			POL_SetupWindow_pulsebar "$(eval_gettext 'Installing fonts')" "$(eval_gettext 'Microsoft fonts')"

			for font in $FONTS_INSTALLER
			do
			 sleep 0.1
			 POL_SetupWindow_set_text "$(eval_gettext 'Installing: ')$font..."
			 echo "$(eval_gettext 'Installing: ')$font..."
			 cabextract "$font" > /dev/null
			 PULSE=$(( PULSE + 8 ))
			 POL_SetupWindow_pulse $PULSE
			 sleep 0.1
			done

			POL_SetupWindow_set_text "$(eval_gettext 'Installing: ')tahoma32.exe"
			echo "$(eval_gettext 'Installing: ')tahoma32.exe"
			cabextract tahoma32.exe > /dev/null
			POL_SetupWindow_pulse 100
			sleep 1
			rm *.exe
			rm *.done
			rm *.dll
			rm *.inf
			rm *.txt
			touch "$REPERTOIRE/configurations/msfonts_installed"
			POL_SetupWindow_message "$(eval_gettext 'Microsoft fonts have been installed successfully.')" "$(eval_gettext 'Microsoft fonts')"
		#	POL_SetupWindow_Close

		fi
	fi
}


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.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
		POL_Internal_XQuartz_Warning
		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 environement')\n$(eval_gettext "Please ensure you are connected to the internet.")"
fi

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

[ "$POL_OS" == "Mac" ] && [ "$OSX_VERSION" = "9" ] &&  install_x11

install_fonts
install_prefix
POL_SetupWindow_message "$(eval_gettext 'Thank you for using $APPLICATION_TITLE')" "$(eval_gettext '$APPLICATION_TITLE first use')"
POL_SetupWindow_Close
POL_Config_Write FIRST_USE TRUE
POL_Config_Write GECKO_INSTALLED TRUE