#!/bin/bash

# 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 "$LNG_LI_SHORTCUT" "$LNG_AUTORUN_TITLE"
	reponse="$APP_ANSWER"
	if [ "$reponse" = "TRUE" ]
	then
		cd "$REPERTOIRE/wineprefix/$Prefix/drive_c/"

		POL_SetupWindow_browse "$LNG_LI_EXEC" "$LNG_AUTORUN_TITLE" ""

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

		POL_SetupWindow_textbox "$LNG_LAUNCHER" "$LNG_AUTORUN_TITLE" ""
		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 "$LNG_AUTORUN_CANT_FIND" "$LNG_AUTORUN_TITLE"
		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 "$LNG_AUTORUN_CDROM_FOUND\n\n$LNG_AUTORUN_CDROM_NAME: $NameIs\n$LNG_AUTORUN_CDROM_SETUP: $cdromDirIs/$SetupIs" "$LNG_AUTORUN_TITLE" "$IconIs" 
	else
		POL_SetupWindow_message "$LNG_AUTORUN_CDROM_FOUND\n\n$LNG_AUTORUN_CDROM_NAME: $NameIs\n$LNG_AUTORUN_CDROM_SETUP: $cdromDirIs/$SetupIs" "$LNG_AUTORUN_TITLE" "$IconIs" 
	fi

	export IGNORE_ICON_DIR="false"	
	if [ ! "$NameIs" = "Unknown" ]
	then
		PreFm="~$LNG_AUTORUN_NEXT: $Prefix ($LNG_AUTORUN_RECOM)"
	fi
	POL_SetupWindow_menu_num "$LNG_AUTORUN_CANT_FIND" "$LNG_AUTORUN_TITLE" "$LNG_AUTORUN_EXISTING~$LNG_AUTORUN_NEW${PreFm}" "~"
	action="$APP_ANSWER"
#	action=$(menu "$LNG_AUTORUN_WHERE" "$LNG_AUTORUN_EXISTING~$LNG_AUTORUN_NEW${PreFm}" "$LNG_AUTORUN_TITLE" 2 $NUMSTEP 1 "" "~" "" 1)
	
	if [ "$action" = "0" ]
	then
		cd $REPERTOIRE/wineprefix
		POL_SetupWindow_menu "$LNG_AUTORUN_PREFIX_C" "$LNG_AUTORUN_TITLE" "$(read_prefixes)" "~"
		Prefix="$APP_ANSWER"

	fi
	
	if [ "$action" = "1" ]
	then
		POL_SetupWindow_textbox "$LNG_AUTORUN_PREFIX_C" "$LNG_AUTORUN_TITLE" ""
		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')" "$LNG_AUTORUN_TITLE"
	wine "$cdromDirIs"/$SetupIs
	POL_SetupWindow_detect_exit
	POL_SetupWindow_message "$(eval_gettext 'Please press Next only when installation is fully complete.')" "$LNG_AUTORUN_TITLE"
	cd "$REPERTOIRE/wineprefix/$Prefix/drive_c/"
	create_shortcut
	

}

LNG_AUTORUN_CANT_FIND="$(eval_gettext 'No setup location found in autorun.inf')"
LNG_AUTORUN_CDROM_FOUND="$(eval_gettext 'A CD-ROM has been found on your drive')"
LNG_AUTORUN_CDROM_NAME="$(eval_gettext 'Name ')"
LNG_AUTORUN_CDROM_SETUP="$(eval_gettext 'Setup ')"
LNG_AUTORUN_TITLE="$(eval_gettext 'Autorun')"
LNG_AUTORUN_NEXT="$(eval_gettext 'In the following prefix')"
LNG_AUTORUN_RECOM="$(eval_gettext 'recomended')"
LNG_AUTORUN_WHERE="$(eval_gettext 'Where do you want to install this CD-ROM?')"
LNG_AUTORUN_EXISTING="$(eval_gettext 'In an existing prefix (add-on)')"
LNG_AUTORUN_NEW="$(eval_gettext 'In a new prefix')"
LNG_NOAUTORUN="$(eval_gettext 'No autorun support found')"
LNG_AUTORUN_N="$(eval_gettext 'autorun has been found')"
LNG_AUTORUN_PREFIX_C="$(eval_gettext 'Choose the prefix')"
LNG_LI_CHOOSEFILE="$(eval_gettext 'You have to choose a file !')" 
LNG_LI_SHORTCUT="$(eval_gettext 'Do you want to create a shortcut to the application in PlayOnLinux ?')\n$(eval_gettext 'If you patched a game, you should probably select no.')" 
LNG_LI_EXEC="$(eval_gettext 'Choose the file to exec the game.')" 
LNG_LAUNCHER="$(eval_gettext 'Please choose a name for the 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 $LNG_AUTORUN_N" "$LNG_AUTORUN_TITLE" "$A_LIST" "#"
	autorun=$APP_ANSWER
	if [ ! "$autorun" = "" ]
	then	
		run_autorun "$autorun"
	fi
fi
POL_SetupWindow_Close
fi
fi
