#!/bin/bash

##This script counts the files in the trash and asks for confirmation 
##to remove them permanently. It uses YAD dialog-box.
##Created by Kobalan for MX-Fluxbox with additions by MX Devs and users
##Released August 2021 under GPL3

# adjusted by fehlix,  September 2021
# version : 210901-01
 
: YAD=${YAD:=/usr/bin/yad}

export TEXTDOMAIN=mxfb-trashcheck
export TEXTDOMAINDIR="/usr/share/locale"
. gettext.sh

# translation  re-use ;)
 TITLE=$(gettext gtk30 "Trash"  )
 CLOSE=$(gettext gtk30 "_Close" ); CLOSE=${CLOSE//_}
CANCEL=$(gettext gtk30 "_Cancel"); CANCEL=${CANCEL//_};
   YES=$(gettext gtk30 "_Yes" )  ; YES=${YES//_}
    NO=$(gettext gtk30 "_No" )   ; NO=${NO//_}
    OK=$(gettext gtk30 "_OK" )   ; OK=${OK//_}
  SHOW=$(gettext gtk30-properties "Show 'Trash'")


MSG_NONE=$(gettext "There are no files in the trash.")
#MSG_ASK=$(gettext "Do you want to remove it permanently?")
MSG_ASK=$(gettext "Do you want to empty the trash?")


main() {

trap hangup_trash_monitor EXIT TERM HUP

fix_mime

run_trash_montor &
	
#---------------------------------------------------------
readarray -t  A < <( xdotool search --class mxfb-trash) 
ID="${A[@]: -1}"; 
if [[ -n $ID ]]; then
    xdotool windowraise $ID
    exit 0
fi
#---------------------------------------------------------


MSG_NONE="

          <b><big>$MSG_NONE</big></b>
"

MSG_SOME="

          <b><big>$MSG_SOME</big></b>
"

ICON_TRASH_NONE=trashcan_empty
ICON_TRASH_SOME=trashcan_full
  BUTTON_NO="$NO"'!dialog-no:0'
  BUTTON_OK="$OK"'!dialog-ok:0'
 BUTTON_YES="$YES"'!dialog-yes:2'
BUTTON_SHOW="$SHOW"'!'"$ICON_TRASH_SOME"':3'


YAD_NONE=(
    $YAD
    --title="$TITLE" 
    --borders=12 
	--height=20 
    --text-align=right 
    --fixed
    --class=mxfb-trash
    --window-icon=$ICON_TRASH_NONE
	--image=user-trash
	--timeout=300 
	--button="$BUTTON_OK"
)

YAD_SOME=(
    $YAD
    --title="$TITLE" 
    --borders=12 
    --text-align=right 
    --fixed
	--height 20 
    --class=mxfb-trash
    --image=$ICON_TRASH_SOME
    --window-icon=$ICON_TRASH_SOME
	--button="$BUTTON_YES"
	--button="$BUTTON_NO"
	--button="$BUTTON_SHOW"
)

while true; do

	COUNT=$(gio list trash:// | wc -l)

    MSG_SOME=$(eval_ngettext \
        'There is 1 file in the trash.' \
        'There are ${COUNT} files in the trash.' \
        $file_count
    )

	MSG_SOME="
    $MSG_SOME   
    $MSG_ASK            "
	MSG_SOME="

          <b><big>$MSG_SOME</big></b>
	"
	case ${COUNT:=0} in
	    0) "${YAD_NONE[@]}" --text="$MSG_NONE" ;;
	    *) "${YAD_SOME[@]}" --text="$MSG_SOME" ;;
	esac
		
	ret=$?
	case $ret in
		0|252) exit 0
		;; 
		2) gio trash --empty
		;;
		3) ( xdg-open trash:// 2>/dev/null 1>&2 ) 2>/dev/null 1>&2 & 
		;;
	esac
done
}

trash_monitor() {
    local monitor=
    while true ; do
        local TRASH
        read TRASH 
        break
    done < <( /usr/bin/gio monitor trash:// )
    hangup_trash_monitor
    return 0
}

reload_mxfb_trash() {
    local pid=$(xdotool search --class mxfb-trash getwindowpid)
    if [[ -n $pid ]]; then
        kill -SIGUSR2 $pid
        return 0
    else
        return 1
    fi 
}

hangup_trash_monitor() {
    pkill -SIGHUP -f '/usr/bin/gio monitor trash://'
}

run_trash_montor() {

    while trash_monitor 2>/dev/null; do
        reload_mxfb_trash || break
        sleep 0.5
    done
    hangup_trash_monitor
}

fix_mime() {
	local mime
	for mime in inode/directory x-scheme-handler/trash; do
	
		if grep -sq -iE '^Default.*(thunar|pcmanfm)' < <(LC_ALL=C.UTF-8 gio mime $mime); then 
		   continue
		fi
		if which thunar >/dev/null; then
		   gio mime $mime thunar.desktop
		elif which pcmanfm-qt >/dev/null; then
		   gio mime $mime pcmanfm-qt.desktop
		elif which pcmanfm >/dev/null; then
		   gio mime $mime pcmanfm.desktop
		fi
	done
}

main

exit 0
