#!/bin/bash

# Copyright (C) 2007-2010 PlayOnLinux Team
# Copyright (C) 2007 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" 
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 'Autorun')"
	reponse="$APP_ANSWER"
	if [ "$reponse" = "TRUE" ]
	then
		cd "$REPERTOIRE/wineprefix/$Prefix/drive_c/"

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

		EXEC="$APP_ANSWER"
		EXECDIR=$(dirname "$EXEC")
		EXECDIR=${EXECDIR//"$REPERTOIRE"/""}
		DRIVE="/wineprefix/$Prefix/drive_c/"
		EXECDIR=${EXECDIR//"$DRIVE"/""}

		POL_SetupWindow_textbox "$(eval_gettext 'Please choose a name for the shortcut.')" "$(eval_gettext 'Autorun')" ""
		LAUNCHER=$APP_ANSWER
		if [ ! "$EXECDIR" = "" ]
			then
			if [ ! "$LAUNCHER" = "" ]
			then
				POL_SetupWindow_make_shortcut "$Prefix" "$EXECDIR" "$EXEC" "playonlinux.png" "$LAUNCHER"
				#echo "$EXECDIR"
			fi
		fi
		create_shortcut
	fi
}
run_autorun()
{
	echo ""		
	NUMSTEP="3"
	open=$(grep "open" -i "$1")
	open=${open//" = "/"="}
	
	icon=$(grep "icon" -i "$1")
	icon=${icon//" = "/"="}

	name=$(grep "Name" -i "$1")
	name=${name//" = "/"="}

	SetupIs=$(echo "$open" | cut -d\= -f 2)
	NameIs=$(echo $name | cut -d\= -f 2)
	IconIs=$(echo "$icon" | cut -d\= -f 2)

	if [ ! "$SetupIs" = "" ]
	then
		SetupIsNum=${#SetupIs}
		SetupIs=${SetupIs:0:$(( SetupIsNum -1 ))}
	else
		POL_SetupWindow_message "$(eval_gettext 'No setup location found in autorun.inf')" "$(eval_gettext 'Autorun')"
		exit 0
	fi
	
	if [ ! "$NameIs" = "" ]
	then
		NameIsNum=${#NameIs}
		NameIs=${NameIs:0:$(( NameIsNum -1 ))}
	fi

	if [ ! "$IconIs" = "" ]
	then
		IconIsNum=${#IconIs}
		IconIs=${IconIs:0:$(( IconIsNum -1 ))}
	fi
	Prefix=$(echo $NameIs | tr -cs '[a-zA-Z0-9]' '[_*]')

	cdromDirIs="/media/$(echo $1 | cut -d / -f 3)"
	if [ "$IconIs" = "" ]
	then
		$IconIs = "null"
	fi
	echo $IconIs
	icon_test=`ls "$cdromDirIs" | grep $IconIs -i`
	echo $icon_test
	if [ "$icon_test" = "" ]
	then
		IconIs="$PLAYONLINUX/etc/playonlinux.png"
	else
		IconIs="$cdromDirIs/$icon_test"		
	fi
	if [  ! "$(echo $IconIs | grep '.exe$')" = "" ]
	then
		IconIs="$PLAYONLINUX/etc/playonlinux.png"
	fi
	echo $cdromDirIs
	echo "Setup path	 : $cdromDirIs/$SetupIs"
	echo "CD name 	 : $cdromDirIs/$NameIs"
	echo "Icon		 : $IconIs"
	echo "WinePrefix  	 : $Prefix"

	if [ "$NameIs" = "" ]
	then
		NameIs="Unknown"
	fi
	export IGNORE_ICON_DIR="true"	
	if [ -e "$IconIs" ]
	then
		POL_SetupWindow_message_image "$(eval_gettext "A CD-ROM has been found in your drive.\n\nName: ")$NameIs\n$(eval_gettext "Installer: ")$cdromDirIs/$SetupIs" "$(eval_gettext 'Autorun')" "$IconIs" 
	else
		POL_SetupWindow_message "$(eval_gettext "A CD-ROM has been found in your drive.\n\nName: ")$NameIs\n$(eval_gettext "Installer: ")$cdromDirIs/$SetupIs" "$(eval_gettext 'Autorun')" "$IconIs" 
	fi

	export IGNORE_ICON_DIR="false"	
	if [ ! "$NameIs" = "Unknown" ]
	then
		LNG_AUTORUN_NEXT="$(eval_gettext 'In the following prefix')"
		PreFm="~$LNG_AUTORUN_NEXT: $Prefix ($(eval_gettext 'recommended'))"
	fi
	LNG_AUTORUN_EXISTING="$(eval_gettext 'In an existing prefix (add-on)')"
	LNG_AUTORUN_NEW="$(eval_gettext 'In a new prefix')"
	POL_SetupWindow_menu_num "$(eval_gettext 'No setup location found in autorun.inf')" "$(eval_gettext 'Autorun')" "$LNG_AUTORUN_EXISTING~$LNG_AUTORUN_NEW${PreFm}" "~"
	action="$APP_ANSWER"
	
	if [ "$action" = "0" ]
	then
		cd $REPERTOIRE/wineprefix
		POL_SetupWindow_menu "$(eval_gettext 'Choose the prefix')" "$(eval_gettext 'Autorun')" "$(read_prefixes)" "~"
		Prefix="$APP_ANSWER"

	fi
	
	if [ "$action" = "1" ]
	then
		POL_SetupWindow_textbox "$(eval_gettext 'Choose the prefix')" "$(eval_gettext 'Autorun')" ""
		Prefix="$APP_ANSWER"
	fi

	if [ "$Prefix" = "" ]
	then
		POL_SetupWindow_Close
		exit
	fi
	echo $Prefix
	select_prefix "$REPERTOIRE/wineprefix/$Prefix"
	POL_SetupWindow_prefixcreate
	POL_SetupWindow_wait_next_signal "$(eval_gettext 'PlayOnLinux is installing your application')" "$(eval_gettext 'Autorun')"
	wine "$cdromDirIs"/$SetupIs
	POL_SetupWindow_detect_exit
	POL_SetupWindow_message "$(eval_gettext 'Please do not press Next until the installation has been completed.')" "$(eval_gettext 'Autorun')"
	cd "$REPERTOIRE/wineprefix/$Prefix/drive_c/"
	create_shortcut
	

}

cd /media
A_LIST=""
i=0

for device in *
do
	fstab=`cat '/etc/fstab' | grep "$device"`
	fstab_bis=`cat '/etc/fstab' | grep "$device" | grep iso9660`
	if [ "$fstab" = "" -o "$fstab_bis" != "" ]
	then
		AutorunInf=$(ls "/media/$device" | grep -i "autorun.inf")
		#echo "Checking $device..."
		if [ ! "$AutorunInf" = "" ]
		then
			#echo "Found /media/$device/$AutorunInf"
			if [ $i = 0 ]
			then
				A_LIST="/media/$device/$AutorunInf"
			else
				A_LIST="$A_LIST#/media/$device/$AutorunInf"
			fi
			i=$(( i+1 ))
		fi
	fi
done
sleep 2
touch $REPERTOIRE/tmp/last_list_autorun
LAST_LIST=`cat "$REPERTOIRE/tmp/last_list_autorun"`
if [ ! "$A_LIST" = "$LAST_LIST" ]
then
echo "$A_LIST" > "$REPERTOIRE/tmp/last_list_autorun"

if [ ! "$i" = 0 ]
then
POL_SetupWindow_Init
if [ "$i" = 1 ]
then
	run_autorun "$A_LIST"
else
	POL_SetupWindow_menu "$i $(eval_gettext 'autoruns have been found')" "$(eval_gettext 'Autorun')" "$A_LIST" "#"
	autorun=$APP_ANSWER
	if [ ! "$autorun" = "" ]
	then	
		run_autorun "$autorun"
	fi
fi
POL_SetupWindow_Close
fi
fi
