#!/bin/bash
# Version 0.2.0, 8 April 2008 - see version information in main AppRun file.
#130214 internationalized. 
#130215 BK: tidy up. replace puppy-trash-restore.wav with puppy-trash.wav
#130310 BK 130310 change "burp" sound to "goodbye" and "hello" --no, avoid english, use 2barks.
#130712 SFR: Proper handling when restoring an item to a non-existent directory

export TEXTDOMAIN=trash
export OUTPUT_CHARSET=UTF-8

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

# Set icon path
emptyicon="/usr/local/lib/X11/pixmaps/trashcan_empty48.png"

# Play the trash sound - puppy-trash-restore.wav
function PlaySoundRestore #130215 tidy up...
{
 if [ -c /dev/mixer ];then #test for audio ready.
  # See if /dev/dsp is in use. If it is then do not play the sound file.
  fuser /dev/dsp
  if [ $? -ne 0 ];then #=0 if found.
   aplay "/usr/share/audio/2barks.wav" #130310
  fi
 fi
}
function PlaySoundTrashIt #130215 tidy up...
{
 if [ -c /dev/mixer ];then #test for audio ready.
  # See if /dev/dsp is in use. If it is then do not play the sound file.
  fuser /dev/dsp
  if [ $? -ne 0 ];then #=0 if found.
   aplay "/usr/share/audio/2barks.wav" #130310
  fi
 fi
}

# 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 RemoveItem
{
 # See what is in the trash.
 stuff=`ls /$HOME/.Trash`
 getFileName "$APPDIR"
 if [ "$FileName" = "$stuff" ]
 then
  ln -sf "$emptyicon" "TRASH/.DirIcon"
  rox -x TRASH
 fi
 # Delete this file and directory.
 rm -fr "$APPDIR"
 #PlaySoundTrashIt
}

function RestoreItem
{

 # Check if destination path is in /mnt and warn a user
 restore_path="`dirname "PATH"`"
 restore_name="`basename "PATH"`"
 if [ "`echo "PATH" | grep '^/mnt/'`" ]; then
   pupdialog --title "$(gettext 'Caution')" --yes-label "$(gettext 'Restore')" --no-label "$(gettext 'Cancel')" --yesno "$restore_name $(gettext 'will be restored to: ')$restore_path
$(gettext 'Make sure that the appropriate device is mounted!')"
   [ $? -ne 0 ] && exit   
 fi
 
 # If dest dir doesn't exist, ask to create one
 if [ ! -d "$restore_path" ]; then
   pupdialog --title "$(gettext 'Confirm Action')" --yes-label "$(gettext 'Create')" --no-label "$(gettext 'Cancel')" --yesno "$(gettext 'Destination directory: ')$restore_path
$(gettext 'does not exist! Create it?')"
   if [ $? -eq 0 ]; then
     mkdir -p "$restore_path"
   else
     exit
   fi
 fi
 # Remove existing item.
 rm -fr "PATH"
 # Restore the item.
 mv -f "$APPDIR/Files/$FileName" "PATH"

 # Get info from the 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/\"//g`
  # 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>
   <X>$x_ar</X>
   <Y>$y_ar</Y>
  </PinboardAdd>
 </env:Body>
</env:Envelope>
EOF
 fi

 RemoveItem
 PlaySoundRestore

}

function DeleteIt
{
 # Write confirmation dialogue
 pupdialog --title "$(gettext 'Confirm Action')" --yes-label "$(gettext 'Delete')" --no-label "$(gettext 'Cancel')" --yesno "$m $(gettext 'Are you sure you want to permanently delete this item?')"
 [ $? -eq 0 ] || exit

 # Remove the item
 RemoveItem
 PlaySoundTrashIt
}

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

  pupdialog --title "$(gettext 'Caution')" --extra-button --ok-label "$(gettext 'Overwrite it')"  --extra-label "$(gettext 'Trash it')" --cancel-label "$(gettext 'Cancel')" --yesno "PATH $(gettext 'Already exists. What would you like to do?')"
  dowhat=$?
  if [ $dowhat == 1 ] ; then #cancel..
   exit
  elif [ $dowhat == 255 ] ; then #window close..
   exit
  elif [ $dowhat == 3 ] ; then #trash existing item..
   TRASH/AppRun "PATH"
  fi

 fi
 # Restore the item.
 RestoreItem
}

# Check to see if they clicked on the application or dropped a file or directory on the icon.
#test -sd "$@"
#
# Changed 14.02.2013 - caused error /tmp/xerrs.log
#
test -z "$*"

# If they just clicked on the icon, or chose the "View File information" option.
if [ "$?" = "0" ]
then

 # Write the message.
 MSG=`which gxmessage` || MSG=xmessage
 instruction=`$MSG -print -default $(gettext 'Cancel') -buttons $(gettext 'Cancel'),$(gettext 'Show'),$(gettext 'Open'),$(gettext 'Restore'),$(gettext 'Delete') -center -title "$(gettext 'Trashed File Information')" -file Info`

 if [ "$instruction" = "$(gettext 'Show')" ]
 then rox -d "$APPDIR/Files"
 elif [ "$instruction" = "$(gettext 'Open')" ]
 then
  rox "$APPDIR/Files/$FileName"
 elif [ "$instruction" = "$(gettext 'Restore')" ]
 then RestoreIt
 elif [ "$instruction" = "$(gettext 'Delete')" ]
 then DeleteIt
 fi

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

# If they chose to open the item.
elif [ "$1" = "-open-file" ]
then
 rox "$APPDIR/Files/$FileName"

# If they chose to see the item.
elif [ "$1" = "-see-file" ]
then
 rox -d "$APPDIR/Files"

# If they chose to delete the file
elif [ "$1" = "-delete" ]
then
 DeleteIt

else

 # If they dropped something to the icon.

 pupdialog --title "$(gettext 'Trash Error')" --msgbox "$(gettext 'You cannot drop items onto things that are in Trash.')"
fi
