#!/bin/bash
# simple script to clear trash/thumbnails/cache/history/logs repertories

# define dot size
[ -d ~/.local/share/Trash ] && SPACE1="$(du -kh --max-depth=0 ~/.local/share/Trash | awk '{print $1}')" || SPACE1="0"
[ -d ~/.cache ] && SPACE2="$(du -kh --max-depth=0 ~/.cache | awk '{print $1}')" || SPACE2="0"
[ -d ~/.thumbnails ] && SPACE3="$(du -kh --max-depth=0 ~/.thumbnails | awk '{print $1}')" || SPACE3="0"

# set messages & labels
userlocale=$(echo $LANG | cut -b-2)
case $userlocale in
    fr) title=" nettoyer le dossier utilisateur "
        trash=" corbeille ($SPACE1)"
        securetrash=" corbeille -  sécurisé ($SPACE1)"
        cache=" cache ($SPACE2)"
        thumb=" aperçus ($SPACE3)"
        histo=" historique (fichiers récents, aspell, ispell, lftp, ranger, w3m, wget & zathura)"
        logs=" logs (weechat, profanity, ghb & Xorg)"
        all=" la totale"
        secureall=" la totale -  sécurisé"
        cancel=" annuler"
        trashcl=" corbeille vidée"
        securetrashcl=" corbeille écrasée et vidée"
        cachecl=" cache effacé"
        thumbcl=" aperçus purgés"
        histocl=" historique nettoyé"
        logscl=" logs nettoyés"
        homecl=" dossier utilisateur nettoyé"
        securehomecl=" dossier utilisateur nettoyé et sécurisé"
        helpclean="usage : lancer 'dotcleaner' sans arguments ou avec [trash|securetrash|cache|thumbnails|history|logs|all|secureall]"
        securemsg=" effacement sécurisé -- veuillez patienter ..."
        ;;
    *)  title=" clean home directory"
        trash=" trash ($SPACE1)"
        securetrash=" trash -  secure ($SPACE1)"
        cache=" cache ($SPACE2)"
        thumb=" thumbnails ($SPACE3)"
        histo=" history (recent files, aspell, ispell, lftp, ranger, w3m, wget & zathura)"
        logs=" logs (weechat, profanity, ghb & Xorg)"
        all=" all"
        secureall=" all -  secure"
        cancel=" cancel"
        trashcl=" trash cleared"
        securetrashcl=" trash securely cleared"
        cachecl=" cache deleted"
        thumbcl=" thumbnails purged"
        histocl=" history cleared"
        logscl=" logs cleared"
        homecl=" home cleared"
        securehomecl=" home securely cleared"
        helpclean="usage: launch 'dotcleaner' alone or with arguments: [trash|securetrash|cache|thumbnails|history|logs|all|secureall]"
        securemsg=" secure delete -- please wait ..."
        ;;
esac

# commands
vterm="xterm -T SecureCleaner -geometry 42x4 -e"
secure="shred -n 1 -u -v"

# history files
histolist="$HOME/.local/share/lftp/cwd_history
    $HOME/.local/share/lftp/rl_history
    $HOME/.local/share/lftp/transfer_log
    $HOME/.local/share/ranger/history
    $HOME/.local/share/recently-used.xbel
    $HOME/.local/share/zathura/history
    $HOME/.local/share/zathura/input-history
    $HOME/.w3m/history
    $HOME/.aspell.*
    $HOME/.ispell_default
    $HOME/.wget-hsts"

# logs files
logslist="$HOME/.local/share/profanity/logs/profanity.log*
    $HOME/.local/share/xorg/Xorg.*.log
    $HOME/.config/ghb/EncodeLogs/*.log
    $HOME/.config/ghb/Activity.log.*
    $HOME/.weechat/logs/*.weechatlog"

# define functions
empty-trash() { rm -Rf $HOME/.local/share/Trash/expunged/* $HOME/.local/share/Trash/files/* $HOME/.local/share/Trash/info/* ; }
empty-trash-secure ()
{
    find $HOME/.local/share/Trash/ -type f -exec chmod 644 {} \;
    $vterm find $HOME/.local/share/Trash/ -type f -exec $secure {} \;
}
empty-cache() { rm -Rf $HOME/.cache/* ; }
empty-thumbnails() { rm -Rf $HOME/.thumbnails/* ; }
empty-history() { truncate -c -s 0 ${histolist} ; }
empty-logs() { truncate -c -s 0 ${logslist} ; }
secure-all()
{
    find $HOME/.local/share/Trash/ -type f -exec chmod 644 {} \;
    $vterm find $HOME/.local/share/Trash/ -type f -exec $secure {} \;
    $vterm find $HOME/.cache/ -type f -exec $secure {} \;
    $vterm find $HOME/.cache/* -type d -exec rm -Rf {} \;
    $vterm find $HOME/.thumbnails/ -type f -exec $secure {} \;
    $vterm $secure $histolist ;
    $vterm $secure $logslist ;
}

# launch CLI/GUI
if [ "$1" == "trash" ]; then
    empty-trash && echo -e "\n${trashcl}"
elif [ "$1" == "securetrash" ]; then
    echo "${securemsg}" & empty-trash-secure && echo -e "\n${securetrashcl}"
elif [ "$1" == "cache" ]; then
    empty-cache && echo -e "\n${cachecl}"
elif [ "$1" == "thumbnails" ]; then
    empty-thumbnails && echo -e "\n${thumbcl}"
elif [ "$1" == "history" ]; then
    empty-history && echo -e "\n${histocl}"
elif [ "$1" == "logs" ]; then
    empty-logs && echo -e "\n${logscl}"
elif [ "$1" == "all" ]; then
    empty-trash && empty-cache && empty-thumbnails && empty-history && empty-logs && echo -e "\n${homecl}"
elif [ "$1" == "secureall" ]; then
    echo "${securemsg}" & secure-all && echo -e "\n${securehomecl}"
elif [ "$1" == "help" ]; then
    echo "${helpclean}"
else
    choice=$(echo -e "${trash}\n${securetrash}\n${cache}\n${thumb}\n${histo}\n${logs}\n${all}\n${secureall}\n${cancel}" | \
        rofi -dmenu -p "$title")
    case "$choice" in
        ${trash}) empty-trash && notify-send "${trashcl}" && exit 0 ;;
        ${securetrash}) notify-send "${securemsg}" & empty-trash-secure && notify-send "${securetrashcl}" && exit 0 ;;
        ${cache}) empty-cache && notify-send "${cachecl}" && exit 0 ;;
        ${thumb}) empty-thumbnails && notify-send "${thumbcl}" && exit 0 ;;
        ${histo}) empty-history && notify-send "${histocl}" && exit 0 ;;
        ${logs}) empty-logs && notify-send "${logscl}" && exit 0 ;;
        ${all}) empty-trash && empty-cache && empty-thumbnails && empty-history && empty-logs && notify-send "${homecl}" && exit 0 ;;
        ${secureall}) notify-send "${securemsg}" & secure-all && notify-send "${securehomecl}" && exit 0 ;;
        ${cancel}) exit 0 ;;
        *) exit 0 ;;
    esac
fi
