#!/bin/bash
# March, 2007 - Gabriel Rojas - Fernando Garcia Bermudez

# Determine the path to this application.
APPDIR=`dirname "$0"`
cd "$APPDIR"
APPDIR="`pwd`"

#################################################
#	TRANSLATIONS
		###### Default = English #####
                txt21="An item named PATH already exists.
Do you want to overwite it?"
                txt22="Do you REALLY want to delete this item?"
                txt23="Do you want to restore the item PATH?"
                txt24="You can't drop items onto things that are in the Trash."
                txt25="Confirm action"

	case $LANG in
		######## Español ########
		es* )
                txt21="Un elemento llamado PATH ya existe.
¿Desea sobreescribirlo?"
                txt22="¿REALMENTE desea eliminar este item?"
                txt23="¿Quiere restaurar el elemento PATH?"
                txt24="No puede arrastrar elementos sobre los que ya se encuentran en la papelera."
                txt25="Confirme la accion"
		;;

		######## Galego ########
		gl* )
                txt21="Un elemento chamado PATH xa existe.
Desexa sobrescribilo?"
                txt22="REALMENTE desexa eliminar este elemento?"
                txt23="Quere restaurar o elemento PATH?"
                txt24="Non pode arrastrar elementos sobre os que xa se atopan no cesto do lixo."
                txt25="Confirme a acción"
		;;

	esac

### End of Translations ###
###########################



# Find the name of the file.
function notAForwardSlash
{
fSlash=/
   if [[ $1 != $fSlash ]]

        then
      return 0
        else
           return 1
        fi
}


function getFileName
{
   STRING="$1"

        LENGTH=${#STRING}

   for ((n=0;n <= $LENGTH; n++))
        do
      CHAR=${STRING:$n:1}

      if notAForwardSlash $CHAR
      then
         FileName=$FileName$CHAR

      else
         FileName=""
      fi
   done
}

# Get the name of the item in the Trash.
getFileName "PATH"

function RestoreItem
{


# Restore the item.
mv "XDG/files/FILE" "PATH"

# Get info from the pinboard shortcut file (if exists)
if [ -e "$APPDIR/shortcut" ]; then
x_ar=`awk '{print $2}' $APPDIR/shortcut | sed -e s/x=//g -e s/\"//g`
y_ar=`awk '{print $3}' $APPDIR/shortcut | sed -e s/y=//g -e s/\"//`
pth="PATH"
label_desk=`grep "PATH" $APPDIR/shortcut | sed -e s/'label'// -e s/'"'// -e s/'">'// -e s/'<\/icon>'// -e s/'<icon x='// -e s/$x_ar// -e s/$y_ar// -e s/'y='// -e s/'="'// -e s/'"'// -e s/'"'// -e s/'"'// -e s^$pth^^`
label_desk=`expr "$label_desk" : '[[:space:]]*\(.*\)[[:space:]]*$'`

# Restore the shortcut on the desktop
rox --RPC << EOF
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
 <env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
  <PinboardAdd>
   <Path>PATH</Path>
<Label>$label_desk</Label>
   <X>$x_ar</X>
   <Y>$y_ar</Y>
  </PinboardAdd>
 </env:Body>
</env:Envelope>
EOF
fi

# Get info from the panel shortcut file (if exists)
if [ -e "$APPDIR/shortcut_panel" ]; then

side=`sed -e 2d "$APPDIR/shortcut_panel"`
label=`sed -e 1d "$APPDIR/shortcut_panel"`
# Restore the shortcut on the panel
rox --RPC << EOF
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
 <env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
  <PanelAdd>
<Side>$side</Side>
<Label>$label</Label>
   <Path>PATH</Path>
  </PanelAdd>
 </env:Body>
</env:Envelope>
EOF
fi

# See what is in the trash.
stuff=`ls STORE`

getFileName "$APPDIR"

if [ "$FileName" = "$stuff" ]
then

"TRASH/trash-icon" empty

rox -x "TRASH"

fi

# BUG FIX: items with space on their names
file2=${FileName//' '/%}

# Remove the restore-all file info.
rm "TRASH/.restore-all/restore-$file2"

# Delete this file and directory.
rm -fr "$APPDIR"
rm -fr "XDG/files/FILE"
rm -fr "XDG/info/FILE.trashinfo"

}

# If they chose the "Restore" option.
if [ "$1" = "-restore" ]
then

# See if there is already a file or folder of the same name in the same location.
if [ -e "PATH" ]
then

zenity --window-icon="TRASH/.DirIcon" --warning --text "$txt21"

if test "$?" = "0";then
# Restore the item.
RestoreItem
fi
else
# Restore the item.
RestoreItem
fi

elif [ "$1" = "-delete" ]
then
zenity --window-icon="TRASH/.DirIcon" --warning --text "$txt22"

# If they chose to cancel.
[ $? -eq 0 ] || exit

# Remove the restore-all file info.
rm "TRASH/.restore-all/restore-FILE"

stuff=`ls STORE`

getFileName "$APPDIR"

if [ "$FileName" = "$stuff" ]
then

"TRASH/trash-icon" empty

rox -x "TRASH"

fi

# Delete this file and directory.
rm -fr "$APPDIR"
rm -fr "XDG/files/FILE"
rm -fr "XDG/info/FILE.trashinfo"

stuff=`ls STORE`

getFileName "$APPDIR"

if [ "$FileName" = "$stuff" ]
then

"TRASH/trash-icon" empty

rox -x "TRASH"

fi

else

# Check to see if they clicked on the application or dropped a file or directory on the icon.
if [ "$*" = "" ]
then

# If they just clicked on the icon.

# Write the confirmation message.
zenity --window-icon="TRASH/.DirIcon" --title "$txt25" --question --text "$txt23"

# If they chose to cancel.
[ $? -eq 0 ] || exit

# See if there is already a item of the same name in the same location.
if [ -e "PATH" ]
then

zenity --window-icon="TRASH/.DirIcon" --warning --text "$txt21"

if test "$?" = "0";then
# Restore the item.
RestoreItem
fi
else
# Restore the item.
RestoreItem
fi


else

# If they dropped something to the icon.

# Write the error message.

zenity --window-icon="TRASH/.DirIcon" --error --text="$txt24"

fi

fi
