#!/bin/bash

destination=$(zenity --file-selection --directory --title "Where to copy files?")

echo $NAUTILUS_SCRIPT_SELECTED_URIS > ~/.gnome2/temp_copy_list

for file in $(cat ~/.gnome2/temp_copy_list); do \

	file_name=$(echo $file | sed -e 's/file:\/\///g' -e 's/\%20/\ /g')
	short_file_name=$(echo $file | sed -e 's#.*/##g' -e 's/\%20/\ /g')

	if [[ -w $destination && -a $destination/$short_file_name ]]; then \
		confirm=$(zenity zenity --question --text "Overwrite Files?"; echo $?)
		if [[ $confirm == 0 ]]; then
			cp -r "$file_name" $destination
			if (( $? != 0 )); then
			zenity --info --text "Something went wrong" --title "Failure"
			fi
		fi
	elif [[ -w $destination ]]; then \
		cp -r "$file_name" $destination
		if (( $? != 0 )); then
		zenity --info --text "Something went wrong" --title "Failure"
		fi
	else	zenity --info --title "Failure" --text "$destination does either not\n exist or is not writable"
	fi; \
done

rm -f ~/.gnome2/temp_copy_list
