#!/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 : 210906-02

: YAD=${YAD:=/usr/bin/yad}

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


export CLASS=mxfb-trashcheck

# translation  re-use ;)
GETTEXT=gettext 
  TITLE=$($GETTEXT -d gtk30 "Trash"  )
  CLOSE=$($GETTEXT -d gtk30 "_Close" ); #CLOSE=${CLOSE//_}
 CANCEL=$($GETTEXT -d gtk30 "_Cancel"); #CANCEL=${CANCEL//_};
    YES=$($GETTEXT -d gtk30 "_Yes" )  ; #YES=${YES//_}
     NO=$($GETTEXT -d gtk30 "_No" )   ; #NO=${NO//_}
     OK=$($GETTEXT -d gtk30 "_OK" )   ; #OK=${OK//_}
   SHOW=$($GETTEXT -d gtk30-properties "Show 'Trash'")
   SHOW=$(echo "$SHOW" | sed 's/[a-zA-Z]/_&/')


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


main() {

local xpid

# run me only once
if xpid=$(xdotool search --class $CLASS getwindowpid); then
    kill -SIGUSR2 $xpid && exit
fi

trap hangup_trash_monitor EXIT TERM HUP

fix_mime

run_trash_monitor &


MSG_NONE="

          <b><big>$MSG_NONE</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=$CLASS
    --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=$CLASS
    --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)

    # TRANSLATORS:
    # Do not translate the count placeholder '${COUNT}'.
    # Singular form: 'There is 1 file in the trash.'
    # Plural form: 'There are 2 files in the trash.'
    MSG_SOME=$(eval_ngettext \
        'There is 1 file in the trash.' \
        'There are ${COUNT} files in the trash.' \
        ${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 $CLASS 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_monitor() {

    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
