#!/bin/bash
#----------------------------------------------#
# Mate Clear Recent                            #   
# Removes recent viewed documents list         #
#----------------------------------------------#

TITLE="Clear Recent"
VERSION="0.2.4" 

ICON="/usr/share/icons/mate/48x48/status/changes-prevent.png"
QICON="/usr/share/icons/mate/48x48/actions/process-stop.png"
NICON="/usr/share/icons/mate/48x48/status/changes-allow.png"
ECON="/usr/share/icons/mate/48x48/status/dialog-error.png"
CONF="$HOME/.config/clear-recent"

# Test for mate installed...
if ! test -f /usr/bin/mate-about; then
yad --width=300 --height=100 --center --fixed \
--text="\nMate desktop is not installed! " \
--window-icon="$ECON" \
--image="$ECON" \
--title="$TITLE" \
--button=gtk-ok:0 
exit 0
else
exec $@
fi
if ! test -d $HOME/.config/autostart; then
 mkdir -p $HOME/.config/autostart
fi
if ! test -d $CONF; then
  mkdir -p $CONF
fi
if ! test -f $CONF/clear-recent ; then
`echo \
'#!/bin/sh
while true; do
cd $HOME; 
cat /dev/null > .local/share/recently-used.xbel; 
sleep 10; 
done' | awk '{print $0}' > $CONF/clear-recent`
chmod a+x $CONF/clear-recent
fi
if ! test -f $CONF/"Clear Recent".desktop ; then
`echo \
'[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon=clear-recent
Icon[en_US]=clear-recent
Exec=.config/clear-recent/clear-recent
Name[en_US]=Clear Recent
Comment[en_US]=Clear Recent Documents
Name=Clear Recent
Comment=Clear Recent Documents' | awk '{print $0}' > $CONF/"Clear Recent".desktop`
chmod a+x $CONF/"Clear Recent".desktop
fi
# Stop recent documents yes/no dialog
ENTRY=`yad --width=400 --center --fixed \
--form --title="$TITLE" \
--field="Enable Auto clearing:CHK" \
--text="\nStop showing history files in <b>Recent Documents</b>?" \
--window-icon=$QICON \
--image="$QICON" \
--button=gtk-no:1 \
--button=gtk-yes:0`
if [ $? = "1" ]; then
# Show no change recent documents dialog
rm -f $HOME/.config/autostart/'Clear Recent'.desktop > /dev/null 2>&1
`yad --width=400 --height=100 --center --fixed \
--text="\n<b>No changes</b> were made in recent document files" \
--window-icon=$NICON \
--image="$NICON" \
--title="$TITLE" \
--button=gtk-about:5 \
--button=gtk-ok:0`
if [ $? = "5" ]; then 
$(yad --width=400 --center --fixed --text='                     \
<b>                Clear Recent</b>\n\nStop showing history files in recent documents\n
© 2026 WTFPL lxgator@gmail.com\n\n            <a href="https://pclosdebian.com/">https://pclosdebian.com</a>' \
--window-icon=$QICON \
--image=$QICON \
--title='Clear Recent 0.2.4' \
--button=gtk-close:1)
exit 0
else
pkill -f clear-recent > /dev/null 2>&1
fi
 fi
    RET=$?
    OIFS=$IFS # save Internal Field Separator
    IFS="|" # separator is "|" in returned choices
    i=0
    retChoice=()
    for ret in $ENTRY;do 
        retChoice[$i]="$ret"
        i=$(($i+1))
    done
IFS=$OIFS # reset IFS back to default
Auto_clearing=${retChoice[0]} # Autostart
if [[ $Auto_clearing == TRUE ]]; then
CONF="$HOME/.config/clear-recent"
ln -s $CONF/"Clear Recent".desktop $HOME/.config/autostart > /dev/null 2>&1
exec $CONF/clear-recent &
notify-send -t 6000 -i "clear-recent" "Clear Recent" "Auto Clearing is Running" 
  fi 
# Final recent documents no show dialog
if [ $? = "0" ]; then
while true; do
cd $HOME; 
cat /dev/null > .local/share/recently-used.xbel; 
sleep 10; 
done | yad --width=375 --height=100 --center --fixed \
--text="\nFiles are not showing in <b>Recent Documents</b>" \
--window-icon=$ICON \
--image="$ICON" \
--title="$TITLE" \
--button=gtk-ok:3
 fi


