#!/bin/bash
# July 10, 2005 - Dan Van Wormer - Puppy Linux

# Create the >Trash directory if it does not already exist.
[ -d /root/.Trash ] || mkdir /root/.Trash

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

# Play the trash sound
function PlaySound
{

# See if /dev/dsp is in use. If it is then do not play the sound file.
fuser /dev/dsp
if [ $? -eq 0 ];then #=0 if found.
 exit
else
 play.tcl $APPDIR/systemmsg.wav
fi
exit

}

function EmptyIcon
{

cp -f "$APPDIR/trashcan_empty.png" "$APPDIR/.DirIcon"
cd ${HOME}
rox -x $APPDIR

}

function FullIcon
{

cp -f "$APPDIR/trashcan_full.png" "$APPDIR/.DirIcon"
cd ${HOME}
rox -x $APPDIR

}

# 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
}

function MoveToTrash
{

getFileName $m

# Create a directory for the trashed item.
mkdir "/root/.Trash/$FileName-$$"

# move the item to the Trash directory.
mv "$m" "/root/.Trash/$FileName-$$"

# Copy the icon for the trashed item.
cp -f "$APPDIR/template/trash.png" "/root/.Trash/$FileName-$$/.DirIcon"

# Copy the AppInfo file over.
cp -f "$APPDIR/template/AppInfo.xml" "/root/.Trash/$FileName-$$/AppInfo.xml"

# Write the new AppRun file with.
gawk -v var="PATH" -v name="$m" -v trash="$APPDIR" '{sub(var, name)}
{sub("TRASH", trash)}
{sub("TRASH", trash); print}' $APPDIR/template/temp-AppRun > /root/.Trash/$FileName-$$/AppRun

# Make it executable.
chmod 777 /root/.Trash/$FileName-$$/AppRun

}

function RemoveOldRoxIcon
{

while grep ">$m<" /root/Choices/ROX-Filer/PuppyPin
 do


  number=`grep -m 1 ">$m<" /root/Choices/ROX-Filer/PuppyPin`

  # Write the new PuppyPin file with the trashed item removed.
  gawk -v var="$number" '{sub(var, ""); print}' /root/Choices/ROX-Filer/PuppyPin > /root/Choices/ROX-Filer/PuppyPin1

  # Rename the new PuppyPin file
  mv /root/Choices/ROX-Filer/PuppyPin1 /root/Choices/ROX-Filer/PuppyPin

  # Restart the pinboard.
  rox -p /root/Choices/ROX-Filer/PuppyPin

done

}

# If they chose the "Look in the Trash" option.
if [ "$1" = "-look" ]
then

 exec rox /root/.Trash

# If they chose the "Empty the Trash" option.
elif [ "$1" = "-empty" ]
then

 # See what is in the trash.
 stuff=`ls /root/.Trash`

 # Wright the confirmation message.
 MSG=`which gxmessage` || MSG=xmessage
 $MSG -buttons "Delete:21,Cancel" -center -geometry 300x300 -title 	"Confirm Delete" "$stuff"

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

  # If they chose to delete.
  rm -r /root/.Trash/*

 EmptyIcon

 PlaySound

# If they chose to "Quickly Empty the Trash".
elif [ "$1" = "-empty-quick" ]
then

 rm -r /root/.Trash/*

 EmptyIcon

 PlaySound

else

 # Check to see if they clicked on the application or sent a file or directory to be deleted.
 test -sd "$@"
 if [ "$?" = "0" ]
 then

  # If they just clicked on the icon.
  exec rox /root/.Trash

else

 # If they sent something to the trash can.
 for m in $@
 do

  # Test to see if the item is a symbolic link.
  if [ -h $m ]
  then

   # Wright the confirmation message.
   MSG=`which gxmessage` || MSG=xmessage
   $MSG -buttons "Yes:21,No" -center -geometry 550x90 -title 	"Confirm Action" "The item $m is a Symbolic Link.
This will move the Symbolic Link to the Trash and leave the original item  alone.
Do you want to continue?"

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

 fi

 match=`grep ">$m<" /root/Choices/ROX-Filer/PuppyPin`

 # Test $match to see if the item had a link to it on the pinboard.
 test "$match"
 if [ "$?" = "0" ]
 then

  # Wright the confirmation message.
  MSG=`which gxmessage` || MSG=xmessage
  $MSG -buttons "Yes:21,No" -center -geometry 560x90 -title 	"Confirm Action" "There is one or more Desktop shortcuts pointing to $m
This will move the original item to the Trash and remove the shortcut(s) from the Desktop.
Do you want to continue?"

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

 while check=`grep ">$m<" /root/Choices/ROX-Filer/PuppyPin`
 do

  OldNumber=`grep -c ">$m<" /root/Choices/ROX-Filer/PuppyPin`

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">
  <PinboardRemove>
   <Path>$m</Path>
  </PinboardRemove>
 </env:Body>
</env:Envelope>
EOF

# Check to see if the entry was removed.
NewNumber=`grep -c ">$m<" /root/Choices/ROX-Filer/PuppyPin`
if [ $OldNumber = $NewNumber ]
then

# If SOAP did not work then remove the icon manualy.
RemoveOldRoxIcon $match

fi
done

fi

MoveToTrash $m

done

FullIcon

PlaySound

fi

fi

fi

