#!/bin/bash
# ROX-Trasher: Trashed item template script
# March, 2007 - Gabriel Rojas - Fernando Garcia Bermudez

#### Initialization
# Determine the path of the script
APPDIR=`dirname "$0"`
cd "$APPDIR"
APPDIR="`pwd`"

#####	TRANSLATIONS
# English (Default)
msg21="An item named PATH already exists.\nDo you want to overwite it?"
msg22="Do you REALLY want to delete this item?"
msg23="Do you want to restore the item PATH?"
msg24="You can't drop items onto things that are in the Trash."
msg25="Confirm action"

case $LANG in
    # Español
    es* )
        msg21="Un elemento llamado PATH ya existe.\n¿Desea sobreescribirlo?"
        msg22="¿REALMENTE desea eliminar este elemento?"
        msg23="¿Quiere restaurar el elemento PATH?"
        msg24="No puede arrastrar elementos sobre los que ya se encuentran en la papelera."
        msg25="Confirme la acción"
    ;;

    # Galego
    gl* )
        msg21="Un elemento chamado PATH xa existe.\nDesexa sobrescribilo?"
        msg22="REALMENTE desexa eliminar este elemento?"
        msg23="Quere restaurar o elemento PATH?"
        msg24="Non pode arrastrar elementos sobre os que xa se atopan no cesto do lixo."
        msg25="Confirme a acción"
    ;;

esac

# Find the name of the item
function notAForwardSlash {
    fSlash=/
    
    if [[ $1 != $fSlash ]]; then
        return 0
    else
        return 1
    fi
}

function processFileName {
   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.
processFileName "PATH"

function RestoreItem {

    if test ! -e "FIX";then
    	mkdir -p "FIX"
    fi
        # 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`"

    processFileName "$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 "$msg21"

        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 "$msg22"

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

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

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

    stuff="`ls STORE`"

    processFileName "$APPDIR"

    if [ "$FileName" = "$stuff" ]; then
        "TRASH/trash-icon" empty
        rox -x "TRASH"
    fi

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

# Extra???
#    stuff=`ls STORE`
#
#    processFileName "$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.

        # Ask for confirmation.
        zenity --window-icon="TRASH/.DirIcon" --title "$msg25" --question --text "$msg23"

        # 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 "$msg21"

            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="$msg24"
    fi
fi
