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

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

if  [ "$XDG_DATA_DIRS" = "" ]; then
    XDG_DATA_DIRS="/usr/share"
fi

######### FUNCTIONS ##########
##############################

function fallback {

# Set the fallback icon theme
theme=`grep gtk-fallback-icon-theme /etc/gtk-2.0/gtkrc | sed -e s/'gtk-fallback-icon-theme'// -e s/' '// -e s/'='// -e s/'"'// -e s/'"'//`
theme=`expr "$theme" : '[[:space:]]*\(.*\)[[:space:]]*$'`

}

function set_theme {

# Check the ROX theme
if [ -e "$HOME/.config/rox.sourceforge.net/ROX-Filer/Options" ]; then
	theme=`grep "icon_theme" "$HOME/.config/rox.sourceforge.net/ROX-Filer/Options" | sed -e s/'<Option name=\"icon_theme\">'// -e s/'<\/Option>'//`
	theme=`expr "$theme" : '[[:space:]]*\(.*\)[[:space:]]*$'`
	else
# If not exists, use the ROX fallback theme
fallback
fi
}

## END OF FUNCTIONS ##########
##############################

set_theme

trash_icon=`find "$HOME/.config/rox.sourceforge.net/MIME-icons" -name $3.png`

if [ "$trash_icon" = "" ];then 

type=${3/_/-}

# If theme is ROX, check in the default icon dir
if [ "$theme" = "ROX" ]; then

icon_dir="$HOME/.icons/ROX/MIME"

type=${3/_/:}

trash_icon="$icon_dir/mime-$type.png"

else

# If not, check in the rest of directories
if test -e "$HOME/.icons/$theme/scalable/filesystems"; then

gnome=`find "$HOME/.icons/$theme/scalable/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$HOME/.icons/$theme/scalable/filesystems/gnome-fs-directory.svg"

else

trash_icon="$HOME/.icons/$theme/scalable/filesystems/fs-directory.svg"

fi

else

if test -e "$HOME/.icons/$theme/48x48"; then

gnome=`find "$HOME/.icons/$theme/48x48/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$HOME/.icons/$theme/48x48/filesystems/gnome-fs-directory.png"

else

trash_icon="$HOME/.icons/$theme/48x48/filesystems/fs-directory.png"
fi
fi
fi

# THIS NEED A BIG FAT DEPURATION!!!

if [ "$trash_icon" = "" ];then 

if test -e "$XDG_DATA_DIRS/icons/$theme/scalable/mimetypes"; then

gnome=`find "$XDG_DATA_DIRS/icons/$theme/scalable/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$XDG_DATA_DIRS/icons/$theme/scalable/mimetypes/gnome-mime-$type.svg"

else

trash_icon="$XDG_DATA_DIRS/icons/$theme/scalable/mimetypes/mime-$type.svg"

fi

else
if test -e "$XDG_DATA_DIRS/icons/$theme/48x48/mimetypes"; then
gnome=`find "$XDG_DATA_DIRS/icons/$theme/48x48/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$XDG_DATA_DIRS/icons/$theme/48x48/mimetypes/gnome-mime-$type.png"

else

trash_icon="$XDG_DATA_DIRS/icons/$theme/48x48/mimetypes/mime-$type.png"

fi
fi
fi

fi

if [ "$trash_icon" = "" ];then 

if test -e "/usr/share/pixmaps/icons/$theme/scalable/mimetypes"; then

gnome=`find "/usr/share/pixmaps/icons/$theme/scalable/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="/usr/share/pixmaps/icons/$theme/scalable/mimetypes/gnome-mime-$type.svg"

else

trash_icon="/usr/share/pixmaps/icons/$theme/scalable/mimetypes/mime-$type.svg"

fi

else

if test -e "/usr/share/pixmaps/$theme/48x48"; then

gnome=`find "/usr/share/pixmaps/icons/$theme/48x48/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="/usr/share/pixmaps/icons/$theme/48x48/mimetypes/gnome-mime-$type.png"

else

trash_icon="/usr/share/pixmaps/icons/$theme/48x48/mimetypes/mime-$type.png"

fi
fi
fi

fi

if [ -d "$4/files/$2" ];then

if [ -e "$4/files/$2/.DirIcon" ];then

trash_icon="$4/files/$2/.DirIcon"

else

if [ -e "$HOME/.icons/$theme/scalable/filesystems" ]; then

gnome=`find "$HOME/.icons/$theme/scalable/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$HOME/.icons/$theme/scalable/filesystems/gnome-fs-directory.svg"

else

trash_icon="$HOME/.icons/$theme/scalable/filesystems/fs-directory.svg"

fi

else

if [ -e "$HOME/.icons/$theme/48x48" ]; then

gnome=`find "$HOME/.icons/$theme/48x48/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$HOME/.icons/$theme/48x48/filesystems/gnome-fs-directory.png"

else

trash_icon="$HOME/.icons/$theme/48x48/filesystems/fs-directory.png"

fi
fi
fi


if test -e "$XDG_DATA_DIRS/icons/$theme/scalable/filesystems"; then

gnome=`find "$XDG_DATA_DIRS/icons/$theme/scalable/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$XDG_DATA_DIRS/icons/$theme/scalable/filesystems/gnome-fs-directory.svg"

else

trash_icon="$XDG_DATA_DIRS/icons/$theme/scalable/filesystems/fs-directory.svg"

fi

else
if test -e "$XDG_DATA_DIRS/icons/$theme/48x48/filesystems"; then
gnome=`find "$XDG_DATA_DIRS/icons/$theme/48x48/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$XDG_DATA_DIRS/icons/$theme/48x48/filesystems/gnome-fs-directory.png"

else

trash_icon="$XDG_DATA_DIRS/icons/$theme/48x48/filesystems/fs-directory.png"

fi
fi
fi

if test -e "/usr/share/pixmaps/$theme/scalable/filesystems"; then

gnome=`find "/usr/share/pixmaps/$theme/scalable/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="/usr/share/pixmaps/$theme/scalable/filesystems/gnome-fs-directory.svg"

else

trash_icon="/usr/share/pixmaps/$theme/scalable/filesystems/fs-directory.svg"

fi

else

if test -e "/usr/share/pixmaps/$theme/48x48"; then

gnome=`find "/usr/share/pixmaps/$theme/48x48/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="/usr/share/pixmaps/$theme/48x48/filesystems/gnome-fs-directory.png"

else

trash_icon="/usr/share/pixmaps/$theme/48x48/filesystems/fs-directory.png"

fi
fi
fi

fi
fi

  if [ -x "$4/files/$2/AppRun" ]; then
if [ -e "$4/files/$2/.DirIcon" ]; then
trash_icon="$4/files/$2/.DirIcon"
else
trash_icon="/usr/lib/rox/ROX-Filer/images/application.png"
fi
fi

if test ! -e "$trash_icon"; then

fallback

if test -e "$HOME/.icons/$theme/scalable/mimetypes"; then

gnome=`find "$HOME/.icons/$theme/scalable/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$HOME/.icons/$theme/scalable/mimetypes/gnome-mime-$type.svg"

else

trash_icon="$HOME/.icons/$theme/scalable/mimetypes/mime-$type.svg"

fi

else
if test -e "$HOME/.icons/$theme/48x48/mimetypes"; then
gnome=`find "$HOME/.icons/$theme/48x48/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$HOME/.icons/$theme/48x48/mimetypes/gnome-mime-$type.png"

else

trash_icon="$HOME/.icons/$theme/48x48/mimetypes/mime-$type.png"
fi
fi
fi
fi

if test ! -e "$trash_icon"; then

fallback

if test -e "$XDG_DATA_DIRS/icons/$theme/scalable/mimetypes"; then

gnome=`find "$XDG_DATA_DIRS/icons/$theme/scalable/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$XDG_DATA_DIRS/icons/$theme/scalable/mimetypes/gnome-mime-$type.svg"

else

trash_icon="$XDG_DATA_DIRS/icons/$theme/scalable/mimetypes/mime-$type.svg"

fi

else
if test -e "$XDG_DATA_DIRS/icons/$theme/48x48/mimetypes"; then
gnome=`find "$XDG_DATA_DIRS/icons/$theme/48x48/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$XDG_DATA_DIRS/icons/$theme/48x48/mimetypes/gnome-mime-$type.png"

else

trash_icon="$XDG_DATA_DIRS/icons/$theme/48x48/mimetypes/mime-$type.png"
fi
fi
fi
fi

if test ! -e "$trash_icon"; then

fallback

if test -e "/usr/share/pixmaps/$theme/scalable/mimetypes"; then

gnome=`find "/usr/share/pixmaps/$theme/scalable/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="/usr/share/pixmaps/$theme/scalable/mimetypes/gnome-mime-$type.svg"

else

trash_icon="/usr/share/pixmaps/$theme/scalable/mimetypes/mime-$type.svg"

fi

else
if test -e "/usr/share/pixmaps/$theme/48x48/mimetypes"; then
gnome=`find "/usr/share/pixmaps/$theme/48x48/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="/usr/share/pixmaps/$theme/48x48/mimetypes/gnome-mime-$type.png"

else

trash_icon="/usr/share/pixmaps/$theme/48x48/mimetypes/mime-$type.png"
fi
fi
fi
fi

if test ! -e "$trash_icon"; then
set_theme


if test -e "$HOME/.icons/$theme/scalable/filesystems"; then

gnome=`find "$HOME/.icons/$theme/scalable/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$HOME/.icons/$theme/scalable/filesystems/gnome-fs-regular.svg"

else

trash_icon="$HOME/.icons/$theme/scalable/filesystems/fs-regular.svg"

fi

else
if test -e "$HOME/.icons/$theme/48x48/filesystems"; then
gnome=`find "$HOME/.icons/$theme/48x48/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$HOME/.icons/$theme/48x48/filesystems/gnome-fs-regular.png"

else

trash_icon="$HOME/.icons/$theme/48x48/filesystems/fs-regular.png"
fi
fi
fi
fi

if test ! -e "$trash_icon"; then
set_theme


if test -e "$XDG_DATA_DIRS/icons/$theme/scalable/filesystems"; then

gnome=`find "$XDG_DATA_DIRS/icons/$theme/scalable/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$XDG_DATA_DIRS/icons/$theme/scalable/filesystems/gnome-fs-regular.svg"

else


trash_icon="$XDG_DATA_DIRS/icons/$theme/scalable/filesystems/fs-regular.svg"

fi

else

if test -e "$XDG_DATA_DIRS/icons/$theme/48x48/filesystems"; then
gnome=`find "$XDG_DATA_DIRS/icons/$theme/48x48/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$XDG_DATA_DIRS/icons/$theme/48x48/filesystems/gnome-fs-regular.png"

else

trash_icon="$XDG_DATA_DIRS/icons/$theme/48x48/filesystems/fs-regular.png"
fi
fi
fi
fi
if test ! -e "$trash_icon"; then
set_theme


if test -e "/usr/share/pixmaps/icons/$theme/scalable/filesystems"; then

gnome=`find "/usr/share/pixmaps/icons/$theme/scalable/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="/usr/share/pixmaps/icons/$theme/scalable/filesystems/gnome-fs-regular.svg"

else

trash_icon="/usr/share/pixmaps/icons/$theme/scalable/filesystems/fs-regular.svg"

fi

else
if test -e "/usr/share/pixmaps/icons/$theme/48x48"; then
gnome=`find "/usr/share/pixmaps/icons/$theme/48x48/filesystems" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="/usr/share/pixmaps/icons/$theme/48x48/filesystems/gnome-fs-regular.png"

else

trash_icon="/usr/share/pixmaps/icons/$theme/48x48/filesystems/fs-regular.png"
fi
fi
fi
fi

fi

if test ! -e "$trash_icon"; then


if test -e "$HOME/.icons/hicolor/scalable/mimetypes"; then

gnome=`find "$HOME/.icons/hicolor/scalable/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$HOME/.icons/hicolor/scalable/mimetypes/gnome-mime-$type.svg"

else

trash_icon="$HOME/.icons/hicolor/scalable/mimetypes/mime-$type.svg"

fi

else
if test -e "$HOME/.icons/hicolor/48x48"; then
gnome=`find "$HOME/.icons/hicolor/48x48/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$HOME/.icons/hicolor/48x48/mimetypes/gnome-mime-$type.png"

else

trash_icon="$HOME/.icons/hicolor/48x48/mimetypes/mime-$type.png"

fi
fi
fi
fi

if test ! -e "$trash_icon"; then


if test -e "$XDG_DATA_DIRS/icons/hicolor/scalable/mimetypes"; then

gnome=`find "$XDG_DATA_DIRS/icons/hicolor/scalable/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$XDG_DATA_DIRS/icons/hicolor/scalable/mimetypes/gnome-mime-$type.svg"

else

trash_icon="$XDG_DATA_DIRS/icons/hicolor/scalable/mimetypes/mime-$type.svg"

fi

else
if test -e "$XDG_DATA_DIRS/icons/hicolor/scalable"; then
gnome=`find "$XDG_DATA_DIRS/icons/hicolor/48x48/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="$XDG_DATA_DIRS/icons/hicolor/48x48/mimetypes/gnome-mime-$type.png"

else

trash_icon="$XDG_DATA_DIRS/icons/hicolor/48x48/mimetypes/mime-$type.png"

fi
fi
fi
fi

if test ! -e "$trash_icon"; then


if test -e "/usr/share/pìxmaps/icons/hicolor/scalable/mimetypes"; then

gnome=`find "/usr/share/pìxmaps/icons/hicolor/scalable/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="/usr/share/pìxmaps/icons/hicolor/scalable/mimetypes/gnome-mime-$type.svg"

else

trash_icon="/usr/share/pìxmaps/icons/hicolor/scalable/mimetypes/mime-$type.svg"

fi

else
if test -e "/usr/share/pìxmaps/icons/hicolor/48x48"; then
gnome=`find "/usr/share/pìxmaps/icons/hicolor/48x48/mimetypes" -name gnome*`

if [ "$gnome" != "" ]; then

trash_icon="/usr/share/pìxmaps/icons/hicolor/48x48/mimetypes/gnome-mime-$type.png"

else

trash_icon="/usr/share/pìxmaps/icons/hicolor/48x48/mimetypes/mime-$type.png"
fi

fi
fi
fi
fi
# Link the icon for the trashed item.
ln -sf "$trash_icon" "$1/$2/.DirIcon"

exit
