#!/bin/bash
ferror-datas ()
{
if [ -z "$Fichiertrash" ]
  then
    yad --text-align="center" \
        --text="<span font_desc=\"DroidLogo bold italic 14\">You have not\nselected a file !!!</span>" \
        --timeout=3 \
        --image=/usr/share/icons/yad-error2.svg \
        --no-buttons --undecorated \
        --width "300" --height "50"
  fpoubelle  
fi
echo -n $Fichiertrash > /tmp/fichierssel
# Suppression du dernier caractère
sed -i 's/.$//' /tmp/fichierssel
# Remplaçement de "| " par un saut de ligne
sed -i 's/| /\n/g' /tmp/fichierssel
# Suppression des 20 premiers caractères de chaque ligne
sed -i 's/^....................//g' /tmp/fichierssel
fichierssel="/tmp/fichierssel"
}

fpoubelle ()
{
trashsize=$(du -sB K $HOME/.local/share/Trash/files | cut -d"K" -f1)
nbfic=$(ls -a $HOME/.local/share/Trash/files | sed -e "/\.$/d" | wc -l)

x=$(df | egrep -e "/dev/|tmpfs" | cut -c57- | sed '/^$/d' | sed '/run/d' | tr '\n' '|' | sed 's/.$//')
echo -n $x  > /tmp/partitions
# sed -i '$ s/.$//' /tmp/partitions
sed -i 's/|/\n/g' /tmp/partitions
mountpoints="/tmp/partitions"
for mountpoint in $(<$mountpoints)
do
  if [ -d /$mountpoint/.Trash-1000/files ];then
    trashsize=$(($trashsize + $(du -sB K /$mountpoint/.Trash-1000/files | cut -d"K" -f1)))
    nbfic=$(($nbfic + $(ls -a /$mountpoint/.Trash-1000/files | sed -e "/\.$/d" | wc -l)))
  fi
done
# echo $nbfic
if [ $nbfic -eq 0 ]
  then
#  for i in `ps aux | grep [x]dotool |awk '{ print $2}'`; do kill -9 $i;done;
  image="/usr/share/icons/yad-user-trash.png"
  cp ~/.icons/user-trash-empty.png ~/.icons/user-trash.png
  else
  image="/usr/share/icons/yad-user-trash-full.png"
  cp ~/.icons/user-trash-full.png ~/.icons/user-trash.png
fi

x=$(trash-list | sed '/^$/d' | tr '\n' '|' | sed 's/.$//')
echo -n $x > /tmp/trashlist
sed -i 's/|/\n/g' /tmp/trashlist
Fichiertrash=$(yad --list --always-print-result \
	--title=Trash Management \
  --window-icon=user-trash.png \
  --undecorated --no-headers --borders="5" \
  --image=$image --image-on-top \
  --margins="5" \
  --multiple \
  --text="<span color=\"#FF0000\" font_desc=\"DroidLogo bold italic 16\">    Select one or more Files to Recover or Remove</span>\n""<span font_desc=\"DroidLogo bold italic 16\">                      or entirely Empty the Trash.</span>\n""<span font_desc=\"DroidLogo bold italic 16\">                                   Trash Size : $trashsize K</span>\n" \
  --width "610" --height "300" \
  --column="Files to the Trash" \
  --buttons-layout=spread --button="!/usr/share/icons/yad-quit.svg:0" --button="!/usr/share/icons/yad-restore.svg:1" --button="!/usr/share/icons/yad-remove.svg:2" --button="!/usr/share/icons/yad-empty.svg:3" \
  --separator="|" \
	--rest=/tmp/trashlist)
case $? in
  0)
    exit;;
  1)
    ferror-datas
    for fichiersel in $(<$fichierssel)
      do
        Fichier=$(basename "$fichiersel")
        Rep=$(dirname "$fichiersel")
        Rep="$Rep/"
        if [ -e ~/.local/share/Trash/files/"$Fichier" ];then
          mv ~/.local/share/Trash/files/"$Fichier" "$Rep"
          rm ~/.local/share/Trash/info/"$Fichier.trashinfo"
        fi
        for mountpoint in $(<$mountpoints)
          do
            if [ -e /$mountpoint/.Trash-1000/files/"$Fichier" ];then
              mv /$mountpoint/.Trash-1000/files/"$Fichier" "$Rep"
              rm /$mountpoint/.Trash-1000/info/"$Fichier.trashinfo"
            fi
        done
    done
    fpoubelle;;
  2)
    ferror-datas
    for fichiersel in $(<$fichierssel)
      do
        Fichier=$(basename "$fichiersel")
        Rep=$(dirname "$fichiersel")
        Rep="$Rep/"
        if [ -d ~/.local/share/Trash/files/"$Fichier" ];then
          rm -Rf ~/.local/share/Trash/files/"$Fichier"
          rm ~/.local/share/Trash/info/"$Fichier.trashinfo"
        fi
        if [ -e ~/.local/share/Trash/files/"$Fichier" ];then
          rm ~/.local/share/Trash/files/"$Fichier"
          rm ~/.local/share/Trash/info/"$Fichier.trashinfo"
        fi
        for mountpoint in $(<$mountpoints)
          do
            if [ -d /$mountpoint/.Trash-1000/files/"$Fichier" ];then
              rm -Rf /$mountpoint/.Trash-1000/files/"$Fichier"
              rm /$mountpoint/.Trash-1000/info/"$Fichier.trashinfo"
            fi
            if [ -e /$mountpoint/.Trash-1000/files/"$Fichier" ];then
              rm /$mountpoint/.Trash-1000/files/"$Fichier"
              rm /$mountpoint/.Trash-1000/info/"$Fichier.trashinfo"
            fi
        done
    done
    fpoubelle;;
  3)
    trash-empty
    fpoubelle;;
esac

}

fpoubelle
exit 0
