#!/bin/bash
dosprefixcreate()
{
	if [ "$DOSPREFIX" = "" ]
	then
		erreur "Pas de préfixe DOS"
		#exit
	else
		mkdir -p $DOSPREFIX/drive_c/
	fi	
}

start_dos ()
{
	if [ "$WINEPREFIX" = "" ]
	then
		erreur "Pas de préfixe DOS"
	else
		DOSPREFIX="$WINEPREFIX"
		PROGRAMME="$1"
		REPERTOIRE_="$(pwd)"
		SLASH="/"
		DOUBLESLASH="//"

		REPERTOIRE_PREFIX="$DOSPREFIX/drive_c"
		REPERTOIRE_PREFIX=${REPERTOIRE_PREFIX//$DOUBLESLASH/$SLASH}
		echo $REPERTOIRE_PREFIX
		REPERTOIRE_TO_DOS=${REPERTOIRE_/"$REPERTOIRE_PREFIX"/"C:"}
		CDROM_DETECT=${1/"D:"/"CDROM"}


		REPERTOIRE_TO_DOS_=${REPERTOIRE_TO_DOS//"$SLASH"/"\\"}
		echo $REPERTOIRE_PREFIX
		if [ "$REPERTOIRE_TO_DOS" = "$REPERTOIRE_" ]
		then
			echo $REPERTOIRE_TO_DOS
			erreur "I can't run the program because it is not in the prefix."
		else
			cd "$REPERTOIRE/plugins/Dosbox Support/config/system_drive"
			echo "mount c $DOSPREFIX/drive_c" > autoexec.bat

			if [ ! "$CDROM" = "" ]
			then
				echo  "mount d $CDROM -t cdrom -usecd 0 -ioctl" >> autoexec.bat
			fi
			if [ "$CDROM_DETECT" = "$1" ]
			then
				echo "c:" >> autoexec.bat
			else
				echo "d:" >> autoexec.bat
			fi
			echo "cd $REPERTOIRE_TO_DOS_" >> autoexec.bat
			echo "$PROGRAMME" >> autoexec.bat
			if [ ! "$2" = "--noexit" ]
			then 
				echo "exit" >> autoexec.bat
			fi
			cd ..
			dosbox -conf default.conf
		fi
	fi
	cd $REPERTOIRE_
}

creer_lanceur_dos()
{
	creer_lanceur_dos_base "$@"
	creer_lanceur_v2 "$@"
}
POL_SetupWindow_make_dos_shortcut()
{
	# 1 = Wineprefix
	# 2 = Repertoire
	# 3 = Binaire
	# 4 = Icone (png ou xpm)
	# 5 = Nom de l'icône
	# 6 = Ignoré (question de compatibilité avec les versions < 2.0)
	# 7 = Eventuel argument

	creer_lanceur_dos_base "$@"

	echo "MsgIn" > "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
	echo "make_shortcut" >> "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
	echo "$NOMICONE_" >> "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
		
	MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
	while [ ! "$MESSAGE" = "MsgOut" ]
	do
		MESSAGE=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MsgOut$') 
		sleep 0.1
	done
	MESSAGE_BIS=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_RECEIVED=')
	if [ "$MESSAGE_BIS" = "MSG_RECEIVED=Cancel" ]
	then
		rm "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID"
		exit 
	else
		MESSAGE_MENU=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_MENU=')
		MESSAGE_DESKTOP=$(cat "$REPERTOIRE/configurations/guis/$POL_SetupWindow_ID" | grep '^MSG_DESKTOP=')		
		if [ "$MESSAGE_MENU" == "MSG_MENU=True" ]
		then
			generer_icone "$REPERTOIRE/icones/$ICONE_" "$NOMICONE_" "$HOME/.local/share/applications/" "$PLAYONLINUX/playonlinux --run \"$NOMICONE_\"" "$NOMICONE_"
		fi
		if [ "$MESSAGE_DESKTOP" == "MSG_DESKTOP=True" ]
		then
			generer_icone "$REPERTOIRE/icones/$ICONE_" "$NOMICONE_" "$DESKTOP" "$PLAYONLINUX/playonlinux --run \"$NOMICONE_\"" "$NOMICONE_"
		fi
	fi
}

creer_lanceur_dos_base () 
{
	# 1 = Wineprefix
	# 2 = Repertoire
	# 3 = Binaire
	# 4 = Icone (png ou xpm)
	# 5 = Nom de l'icône
	# 6 = Ignoré (question de compatibilité avec les versions < 2.0)
	# 7 = Eventuel argument

	if [ "$4" = "" ]
	then
		ICONE_="playonlinux.png"
	else
		ICONE_="$4"
	fi

	if [ "$5" = "" ]
	then
		NOMICONE_="$1"
	else
		NOMICONE_="$5"
	fi


	Binaire="$3"
	SpecialArg="$7"
	
	cd $REPERTOIRE/icones
	wget $SITE/icones/$ICONE_
	if [ "$ICONE_" != "playonlinux.png" ]
	then
		convert -resize 32 "$ICONE_" "32/$NOMICONE_" # On fabrique l'icone
	fi

	echo "#!/bin/bash" > "$REPERTOIRE/configurations/installed/$NOMICONE_"
	echo "export WINEPREFIX=\"$REPERTOIRE/wineprefix/$1\"" >> "$REPERTOIRE/configurations/installed/$NOMICONE_"
	echo "source $PLAYONLINUX/lib/sources" >> "$REPERTOIRE/configurations/installed/$NOMICONE_"
	echo "cd \"$WINEPREFIX/drive_c/$2\"" >> "$REPERTOIRE/configurations/installed/$NOMICONE_"
	echo "export CDROM=$CDROM && start_dos \"$Binaire\" $SpecialArg \$@" >> "$REPERTOIRE/configurations/installed/$NOMICONE_"
	chmod +x "$REPERTOIRE/configurations/installed/$NOMICONE_"

	bash $PLAYONLINUX/bash/reload --set


}
export DOSBOX_SUPPORT=1
#if [ ! -e "$REPERTOIRE/plugins/DosBox-Support" ]
#then
#	attendre "Loading dosbox-support ..." "cp $PLAYONLINUX/etc/dosbox $REPERTOIRE/ -r" "Dosbox support for PlayOnLinux" 0 0 0 "" 1
#fi
