#! /bin/bash
#--------------------------------------------------------------------------------------------------------
# Name: Lite Tweaks
# Description: A collection of tools to tweak your Linux Lite system.
# Authors: Misko_2083, John Jenkins, Jerry Bezencon, Ralphy
# Website: https://www.linuxliteos.com
#--------------------------------------------------------------------------------------------------------

# locale support
export LANG=C
export LANG=C.UTF-8
export TEXTDOMAINDIR=/usr/share/locale/
export TEXTDOMAIN=lite-tweaks

# Set functions for non-root execution
# The ones that need administrative privileges go to the other file: lite-tweaks-super
# Autoclean module check
_DRYAPT="$HOME/.local/share/.dryapt"
_NOWDT=$(date "+%Y-%m-%d %H")
_CHKMD=$(stat "$_DRYAPT" 2>/dev/null | grep 'Change' | awk '{print $2,$3}' | cut -c 1-13)
_CHKMDAPT=$(cat "$_DRYAPT" 2>/dev/null | tail -n+2)
if [ -f "$_DRYAPT" ] && [ "$(cat "$_DRYAPT" | head -n+1)" -gt 0 ]; then :
elif [ -f "$_DRYAPT" ] && [ "$(cat "$_DRYAPT" | head -n+1)" -eq 0 ]; then
  if [ "$_CHKMD" == "$_CHKMDAPT" ]; then :; fi
  if [ "$_CHKMDAPT" != "$_NOWDT" ]; then bash -c "/usr/local/sbin/dryapt" & fi
else bash -c "/usr/local/sbin/dryapt" &
fi

# function - clean up common files
_clean_up() {
  rm -f "$TMPLTF"
  [ -e /tmp/ltlogs.log ] && rm -f /tmp/ltlogs.log
}


# Brave Cache tweak
BRAVE() {
  echo """$(gettext "#⚫ Removing Brave browser cache...")" && sleep 1 ; rm -rf "$HOME/.cache/BraveSoftware/Brave-Browser/Default/Cache/"
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while removing Brave browser cache!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Brave browser cache cleared.")" && sleep 1
}
# Chrome Cache tweak
CHROME() {
  echo """$(gettext "#⚫ Removing Google Chrome browser cache...")" && sleep 1 ; rm -rf "$HOME/.cache/google-chrome/"
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while removing Google Chrome browser cache!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Google Chrome browser cache cleared.")" && sleep 1
}
# Chromium Cache tweak
CHROMIUM() {
  echo """$(gettext "#⚫ Removing Chromium browser cache...")" && sleep 1 ; rm -rf "$HOME/.cache/chromium/"
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while removing Chromium browser cache!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Chromium browser cache cleared.")" && sleep 1
}
# Firefox Cache tweak
FIREFOX() {
  echo """$(gettext "#⚫ Removing Firefox browser cache...")" && sleep 1 ; rm -rf "$HOME/.cache/mozilla/"
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while removing Firefox browser cache!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Firefox browser cache cleared.")" && sleep 1
}
# Microsoft Edge Cache tweak
MSEDGECA() {
  echo """$(gettext "#⚫ Removing Microsoft Edge browser cache...")" && sleep 1 ; rm -rf "$HOME/.cache/microsoft-edge/"
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while removing Microsoft Edge browser cache!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Microsoft Edge browser cache cleared.")" && sleep 1
}
# Midori Cache tweak
MIDORI() {
  echo """$(gettext "#⚫ Removing Midori browser cache...")" && sleep 1 ; rm -rf "$HOME/.cache/midori/"
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while removing Midori browser cache!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Midori browser cache cleared.")" && sleep 1
}
# Opera Cache tweak
OPERA() {
  echo """$(gettext "#⚫ Removing Opera browser cache...")" && sleep 1 ; rm -rf "$HOME/.cache/opera/Cache/"
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while removing Opera browser cache!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Opera browser cache cleared.")" && sleep 1
}
# Pale Moon Cache tweak
PALEMOON() {
  echo """$(gettext "#⚫ Removing Pale Moon browser cache...")" && sleep 1 ; rm -rf "$HOME/.cache/moonchild productions/pale moon/"
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while removing Pale Moon browser cache!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Pale Moon browser cache cleared.")" && sleep 1
}
# Vivaldi Cache tweak
VIVALDI() {
  echo """$(gettext "#⚫ Removing Vivaldi browser cache...")" && sleep 1 ; rm -rf "$HOME/.cache/vivaldi/"
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while removing Vivaldi browser cache!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Vivaldi browser cache cleared.")" && sleep 1
}
# Taskbar Restore
TASKBARRES() {
  echo """$(gettext "#⚫ Restoring the Taskbar...")" && sleep 1 ; xfce4-panel --quit; pkill xfconfd; rm -rf ~/.config/xfce4/panel; rm -rf ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-panel.xml; sleep 2; cp -R /etc/skel/.config/xfce4/panel ~/.config/xfce4/; cp /etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-panel.xml ~/.config/xfce4/xfconf/xfce-perchannel-xml/; xfce4-panel &
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while restoring the Taskbar!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Taskbar restored.")" && sleep 1
}
# Thumbnails Cache tweak
THUMBNAILS() {
  echo """$(gettext "#⚫ Deleting Thumbnail Cache...")" && sleep 1 ; rm -rf "$HOME"/.cache/thumbnails/*
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while deleting Thumbnail Cache!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Thumbnail Cache cleared.")" && sleep 1
}
# Trash Bin tweak
TRASH() {
  echo """$(gettext "#⚫ Emptying the Trash bin...")" && sleep 1 ; rm -rf "$HOME"/.local/share/Trash/files/*
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while emptying the Trash bin!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Trash bin emptied.")" && sleep 1
}
# Whisker Menu tweak
WHISKER() {
  echo """$(gettext "#⚫ Cleaning Whisker Menu Recent items...")"; sleep 1; sed -i 's/^recent=.*$/recent=/g' ~/.config/xfce4/panel/whiskermenu-*.rc
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo """$(gettext "# Error...")" && sleep 1
      zenity --error --width="260" --height="80" --title="""$(gettext " Error")" --text="""$(gettext "\nAn error occurred while cleaning Whisker Menu Recently Used items!")" 2>/dev/null; return
    fi
  echo """$(gettext "#✔ Whisker Menu Recent Items cleared.")" && sleep 1 ; echo """$(gettext "#⚫ Logout then Login to see cleared Recent Items...")" && sleep 4
}
# Default Web Browser tweak
DBROWSER() {
echo """$(gettext "#⚫ Looking for installed web browsers...")" && sleep 1
_MIMEAPPS="$HOME/.config/mimeapps.list"
echo """$(gettext "#Configuring default web browser...")"
# Create mimeapps.list if it doesn't exist
if [ ! -f "$_MIMEAPPS" ]; then
      touch "$_MIMEAPPS"
  echo "[Added Associations]" >> "$_MIMEAPPS"  && echo "[Default Applications]" >> "$_MIMEAPPS"
  sed -i -e '1a\application/xhtml+xml=firefox.desktop \nx-scheme-handler/http=firefox.desktop \nx-scheme-handler/https=firefox.desktop' -e '/[Default Applications] /a\text/html=firefox.desktop' "$_MIMEAPPS"
else # If file exists make sure mime options are set before proceeding
  grep "application/xhtml+xml" "$_MIMEAPPS" || sed -i '/Added Associations/a application/xhtml+xml=firefox.desktop' "$_MIMEAPPS"
  grep "x-scheme-handler/http" "$_MIMEAPPS" || sed -i '/Added Associations/a x-scheme-handler/http=firefox.desktop' "$_MIMEAPPS"
  grep "x-scheme-handler/https" "$_MIMEAPPS" || sed -i '/Added Associations/a x-scheme-handler/https=firefox.desktop' "$_MIMEAPPS"
  grep "text/html" "$_MIMEAPPS" || sed -i '/Default Applications/a text/html=firefox.desktop' "$_MIMEAPPS"
fi

test_1=(` grep "application/xhtml+xml" "$_MIMEAPPS"`)
test_3=(` grep "x-scheme-handler/http" "$_MIMEAPPS"`)
test_4=(` grep "x-scheme-handler/https" "$_MIMEAPPS"`)

hash brave-browser 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0" ]; then
  if [ "${test_1[0]}" = "application/xhtml+xml=brave-browser.desktop;firefox.desktop;" ] \
  && [ "${test_3[0]}" = "x-scheme-handler/http=brave-browser.desktop;firefox.desktop;" ] \
  && [ "${test_4[0]}" = "x-scheme-handler/https=brave-browser.desktop;firefox.desktop;" ]; then
    bravebrowser=(TRUE "Brave" """$(gettext "  ✔ default")" """$(gettext "Brave is set as the default Web browser")")
  else bravebrowser=(FALSE "Brave" """$(gettext "not default")" """$(gettext "Set Brave as the default Web browser")")
  fi
else bravebrowser=()
fi

hash firefox 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0" ]; then
  if [ "${test_1[0]}" = "application/xhtml+xml=firefox.desktop;" ] \
  && [ "${test_3[0]}" = "x-scheme-handler/http=firefox.desktop;" ] \
  && [ "${test_4[0]}" = "x-scheme-handler/https=firefox.desktop;" ]; then
    firefox=(TRUE "Firefox" """$(gettext "  ✔ default")" """$(gettext "Firefox is set as the default Web browser")")
  else firefox=(FALSE "Firefox" """$(gettext "not default")" """$(gettext "Set Firefox as the default Web browser")")
  fi
else firefox=()
fi

hash google-chrome 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0" ]; then
  if [ "${test_1[0]}" = "application/xhtml+xml=google-chrome.desktop;firefox.desktop;" ] \
  && [ "${test_3[0]}" = "x-scheme-handler/http=google-chrome.desktop;firefox.desktop;" ] \
  && [ "${test_4[0]}" = "x-scheme-handler/https=google-chrome.desktop;firefox.desktop;" ]; then
    googlechrome=(TRUE "Google Chrome" """$(gettext "  ✔ default")" """$(gettext "Google Chrome is set as the default Web browser")")
  else googlechrome=(FALSE "Google Chrome" """$(gettext "not default")" """$(gettext "Set Google Chrome as the default Web browser")")
  fi
else googlechrome=()
fi

hash chromium-browser 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0" ]; then
  if [ "${test_1[0]}" = "application/xhtml+xml=chromium-browser.desktop;firefox.desktop;" ] \
  && [ "${test_3[0]}" = "x-scheme-handler/http=chromium-browser.desktop;firefox.desktop;" ] \
  && [ "${test_4[0]}" = "x-scheme-handler/https=chromium-browser.desktop;firefox.desktop;" ]; then
    chromium=(TRUE "Chromium" """$(gettext "  ✔ default")" """$(gettext "Chromium is set as the default Web browser")")
  else chromium=(FALSE "Chromium" """$(gettext "not default")" """$(gettext "Set Chromium as the default Web browser")")
  fi
else chromium=()
fi

hash microsoft-edge 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0" ]; then
  if [ "${test_1[0]}" = "application/xhtml+xml=microsoft-edge.desktop;firefox.desktop;" ] \
  && [ "${test_3[0]}" = "x-scheme-handler/http=microsoft-edge.desktop;firefox.desktop;" ] \
  && [ "${test_4[0]}" = "x-scheme-handler/https=microsoft-edge.desktop;firefox.desktop;" ]; then
    msedgedef=(TRUE "Microsoft Edge" """$(gettext "  ✔ default")" """$(gettext "Microsoft Edge is set as the default Web browser")")
  else msedgedef=(FALSE "Microsoft Edge" """$(gettext "not default")" """$(gettext "Set Microsoft Edge as the default Web browser")")
  fi
else msedgedef=()
fi

hash midori 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0" ]; then
  if [ "${test_1[0]}" = "application/xhtml+xml=midori.desktop;firefox.desktop;" ] \
  && [ "${test_3[0]}" = "x-scheme-handler/http=midori.desktop;firefox.desktop;" ] \
  && [ "${test_4[0]}" = "x-scheme-handler/https=midori.desktop;firefox.desktop;" ]; then
    midori=(TRUE "Midori" """$(gettext "  ✔ default")" """$(gettext "Midori is set as the default Web browser")")
  else midori=(FALSE "Midori" """$(gettext "not default")" """$(gettext "Set Midori as the default Web browser")")
  fi
else midori=()
fi

hash opera 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0" ]; then
  if [ "${test_1[0]}" = "application/xhtml+xml=opera.desktop;firefox.desktop;" ] \
  && [ "${test_3[0]}" = "x-scheme-handler/http=opera.desktop;firefox.desktop;" ] \
  && [ "${test_4[0]}" = "x-scheme-handler/https=opera.desktop;firefox.desktop;" ]; then
    opera=(TRUE "Opera" """$(gettext "  ✔ default")" """$(gettext "Opera is set as the default Web browser")")
  else opera=(FALSE "Opera" """$(gettext "not default")" """$(gettext "Set Opera as the default Web browser")")
  fi
else opera=()
fi

hash palemoon 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0" ]; then
  if [ "${test_1[0]}" = "application/xhtml+xml=palemoon.desktop;firefox.desktop;" ] \
  && [ "${test_3[0]}" = "x-scheme-handler/http=palemoon.desktop;firefox.desktop;" ] \
  && [ "${test_4[0]}" = "x-scheme-handler/https=palemoon.desktop;firefox.desktop;" ]; then
    palemoon=(TRUE "Pale Moon" """$(gettext "  ✔ default")" """$(gettext "Pale Moon is set as the default Web browser")")
  else palemoon=(FALSE "Pale Moon" """$(gettext "not default")" """$(gettext "Set Pale Moon as the default Web browser")")
  fi
else palemoon=()
fi

hash vivaldi 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0" ]; then
  if [ "${test_1[0]}" = "application/xhtml+xml=vivaldi-stable.desktop;firefox.desktop;" ] \
  && [ "${test_3[0]}" = "x-scheme-handler/http=vivaldi-stable.desktop;firefox.desktop;" ] \
  && [ "${test_4[0]}" = "x-scheme-handler/https=vivaldi-stable.desktop;firefox.desktop;" ]; then
    vivaldi=(TRUE "Vivaldi" """$(gettext "  ✔ default")" """$(gettext "Vivaldi is set as the default Web browser")")
  else vivaldi=(FALSE "Vivaldi" """$(gettext "not default")" """$(gettext "Set Vivaldi as the default Web browser")")
  fi
else vivaldi=()
fi
selection=$(zenity --window-icon="$ic" --list --radiolist --width=650 --height=300 --column="""$(gettext "Select")" \
                   --title="""$(gettext "Default Web Browser")" --ok-label="""$(gettext "Set")" --cancel-label="""$(gettext "Cancel")" \
                   --column="""$(gettext "Name")" --column="""$(gettext "Status")" --column="""$(gettext "Description")" \
                   --text="""$(gettext "                 <span font='Sans Bold 10'>Choose the Web browser you want to set as the default</span>\n\n                 <span font='Sans Bold 10'>Currently supported Web Browsers:</span>\n                 <span font='Sans 10'>Brave, Firefox, Chrome, Chromium, Edge, Midori, Opera, Palemoon, Vivaldi</span>\n")" \
                   "${bravebrowser[@]}" "${firefox[@]}" "${chromium[@]}" "${googlechrome[@]}" "${msedgedef[@]}" "${midori[@]}" "${opera[@]}" "${palemoon[@]}" "${vivaldi[@]}" 2>/dev/null )

# "The kill switch"
if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi
sleep 1
  case "$selection" in
  "Brave") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=brave-browser.desktop;firefox.desktop;!g" "$_MIMEAPPS" `;
    `sed -i "s!^"text/html=.*"!text/html=brave-browser.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=brave-browser.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=brave-browser.desktop;firefox.desktop;!g" "$_MIMEAPPS" `;
    xdg-settings set default-web-browser brave-browser.desktop;;
  "Firefox") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=firefox.desktop;!g" "$_MIMEAPPS" `;
    `sed -i "s!^"text/html=.*"!text/html=firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=firefox.desktop;!g" "$_MIMEAPPS" `;
    xdg-settings set default-web-browser firefox.desktop;;
  "Chromium") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=chromium-browser.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"text/html=.*"!text/html=chromium-browser.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=chromium-browser.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=chromium-browser.desktop;firefox.desktop;!g" "$_MIMEAPPS" `;
    xdg-settings set default-web-browser chromium.desktop;;
  "Google Chrome") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=google-chrome.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"text/html=.*"!text/html=google-chrome.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=google-chrome.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=google-chrome.desktop;firefox.desktop;!g" "$_MIMEAPPS" `;
    xdg-settings set default-web-browser google-chrome.desktop;;
  "Microsoft Edge") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=microsoft-edge.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"text/html=.*"!text/html=microsoft-edge.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=microsoft-edge.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=microsoft-edge.desktop;firefox.desktop;!g" "$_MIMEAPPS" `;
    xdg-settings set default-web-browser microsoft-edge.desktop;;
  "Midori") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=midori.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"text/html=.*"!text/html=midori.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=midori.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=midori.desktop;firefox.desktop;!g" "$_MIMEAPPS" `;
    xdg-settings set default-web-browser midori.desktop;;
  "Opera") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=opera.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"text/html=.*"!text/html=opera.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=opera.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=opera.desktop;firefox.desktop;!g" "$_MIMEAPPS" `;
    xdg-settings set default-web-browser opera.desktop;;
  "Pale Moon") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=palemoon.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"text/html=.*"!text/html=palemoon.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=palemoon.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=palemoon.desktop;firefox.desktop;!g" "$_MIMEAPPS" `;
    xdg-settings set default-web-browser palemoon.desktop;;
  "Vivaldi") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=vivaldi-stable.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"text/html=.*"!text/html=vivaldi-stable.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=vivaldi-stable.desktop;firefox.desktop;!g" "$_MIMEAPPS" `; \
    `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=vivaldi-stable.desktop;firefox.desktop;!g" "$_MIMEAPPS" `;
    xdg-settings set default-web-browser vivaldi.desktop;;
  esac
echo "#✔ $selection $(gettext "is now the default browser.")" && sleep 1
}
# Display Disk Usage tweak
DU() {
# the loop
while (true); do
echo "#Disk Usage..."
DU_TEXT='<span font="Sans 10">* Double click a Partition to open in file manager.\n* Click columns header to sort the information.\n</span>'
DU_TITLE="Disk Usage"
TMP_DU=$(mktemp /tmp/diskusage.XXXXX)

df -h -T| tail -n+2 | while read fs type size used rest target; do
    if [[ $rest ]] ; then
        echo "$fs" "$type" "$size"B "$used"B "$rest"B "${target[@]}" |grep /dev/sd |
        # Workaround for disk labels that contain whitespaces(number of characters that can be divided by whitespace =e-s)
        awk '{print $1,"\n",$2,"\n",$3,"\n",$4,"\n",$5,"\n",$6}BEGIN{ s = 7; e = 35; }{for (i=s; i<=e; i++) printf("%s%s", $(i), i<e ? OFS : "\n"); }'
    fi
done | sed -e 's/[ \t]*$//' $1|  tee $TMP_DU

SEL=$(cat $TMP_DU | zenity --list --ok-label="Open in File Manager" --cancel-label="Quit" --window-icon="$ic" --width="600" --height="350" --title="  ${DU_TITLE}" --text="${DU_TEXT}" \
                    --column="Device" --column="Filesystem" --column="Size" --column="Used" --column="Free" --column="%Used" --column="Partition" --print-column="7"| cut -d '|' -f2 2>/dev/null)

  if [ -z "$SEL" ]; then rm -f $TMP_DU; return; fi
thunar "$( echo $SEL)"
done
}
# Hibernate / Suspend tweak
HSBTNS() {
_LITEAPPSICONS="/usr/share/liteappsicons/litetweaks/"
APPNAME="  Lite Tweaks - Hibernate / Suspend buttons"
_XFCE4SESSION="$HOME/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-session.xml"
#check defaults on first run
grep -q -F '<property name="shutdown" type="empty">' "$_XFCE4SESSION" && echo $?
  if [ $? = 0 ]; then :; else
  # if defaults do not exist then set defaults
  sed -i '/channel>/i \
    <property name="shutdown" type="empty">\
        <property name="ShowHibernate" type="bool" value="true"/>\
        <property name="ShowSuspend" type="bool" value="true"/>\
        <property name="ShowHybridSleep" type="bool" value="true"/>\
        <property name="ShowSwitchUser" type="bool" value="true"/>\
    </property>' "$_XFCE4SESSION"
  fi

# The loop
while (true); do
  echo "#Configuring Hibernate, Suspend buttons..."
  if grep -q -F '<property name="ShowHibernate" type="bool" value="true"/>' "$_XFCE4SESSION"; then
    statushibernatebtn="$_LITEAPPSICONS/shown32.png" ; actionhibernatebtn="Hide button" ; deschibernatebtn="Hide Hibernate button on the Logout screen" ; valhibernatebtn="1"
  else
    statushibernatebtn="$_LITEAPPSICONS/hidden32.png" ; actionhibernatebtn="Show button  " ; deschibernatebtn="Show Hibernate button on the Logout screen" ; valhibernatebtn="0"
  fi

  if grep -q -F '<property name="ShowSuspend" type="bool" value="true"/>' "$_XFCE4SESSION"; then
    statussuspendbtn="$_LITEAPPSICONS/shown32.png" ; actionsuspendbtn="Hide button" ; descsuspendbtn="Hide Suspend button on the Logout screen" ; valsuspendbtn="1"
  else
    statussuspendbtn="$_LITEAPPSICONS/hidden32.png" ; actionsuspendbtn="Show button  " ; descsuspendbtn="Show Suspend button on the Logout screen" ; valsuspendbtn="0"
  fi

  if grep -q -F '<property name="ShowHybridSleep" type="bool" value="true"/>' "$_XFCE4SESSION"; then
    statushybridbtn="$_LITEAPPSICONS/shown32.png" ; actionhybridbtn="Hide button" ; deschybridbtn="Hide Hybrid Sleep button on the Logout screen" ; valhybridbtn="1"
  else
    statushybridbtn="$_LITEAPPSICONS/hidden32.png" ; actionhybridbtn="Show button  " ; deschybridbtn="Show Hybrid Sleep button on the Logout screen" ; valhybridbtn="0"
  fi

  if grep -q -F '<property name="ShowSwitchUser" type="bool" value="true"/>' "$_XFCE4SESSION"; then
    statusswitchuserbtn="$_LITEAPPSICONS/shown32.png" ; actionswitchuserbtn="Hide button" ; descswitchuserbtn="Hide Switch User button on the Logout screen" ; valswitchuserbtn="1"
  else
    statusswitchuserbtn="$_LITEAPPSICONS/hidden32.png" ; actionswitchuserbtn="Show button  " ; descswitchuserbtn="Show Switch User button on the Logout screen" ; valswitchuserbtn="0"
  fi

    statushidebtn="$_LITEAPPSICONS/appicon3-off.png" ; actionshidebtn="Hide all" ; deschidedbtn="Hide all buttons on the Logout screen"
    statusshowbtn="$_LITEAPPSICONS/appicon1-off.png" ; actionsshowbtn="Show all" ; descshowbtn="Show all buttons on the Logout Screen"

    HSBTN=$(zenity --list --imagelist --width="640" --height="360" --cancel-label="Quit" --ok-label="Apply Action" --window-icon=$ic --hide-column=5 \
                   --title="$APPNAME" --text='                           <span font="Sans Bold 11">Hibernate / Suspend buttons preferences</span>\n' \
                   --print-column=5 --column "Status  " --column "Button" --column "Action" --column "Description" --column "val" \
                   "$statushibernatebtn" "Hibernate" "$actionhibernatebtn" "$deschibernatebtn" "ab" \
                   "$statussuspendbtn" "Suspend" "$actionsuspendbtn" "$descsuspendbtn" "bc" \
                   "$statushybridbtn" "Hybrid Sleep" "$actionhybridbtn" "$deschybridbtn" "cd" \
                   "$statusswitchuserbtn" "Switch User" "$actionswitchuserbtn" "$descswitchuserbtn" "de" \
                   "$statushidebtn" " *all*" "$actionshidebtn" "$deschidedbtn" "ef" \
                   "$statusshowbtn" " *all*" "$actionsshowbtn" "$descshowbtn" "fg" 2>/dev/null )

    if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi
    # get it done
    if [[ "$HSBTN" =~ "ab" ]]; then
      if grep -q -F '<property name="ShowHibernate" type="bool" value="true"/>' $_XFCE4SESSION; then
        sed -i 's/.*ShowHibernate.*/      <property name="ShowHibernate" type="bool" value="false"\/>/' $_XFCE4SESSION
      else
        sed -i 's/.*ShowHibernate.*/      <property name="ShowHibernate" type="bool" value="true"\/>/' $_XFCE4SESSION
      fi
    elif [[ "$HSBTN" =~ "bc" ]]; then
      if grep -q -F '<property name="ShowSuspend" type="bool" value="true"/>' $_XFCE4SESSION; then
        sed -i 's/.*ShowSuspend.*/      <property name="ShowSuspend" type="bool" value="false"\/>/' $_XFCE4SESSION
      else
        sed -i 's/.*ShowSuspend.*/      <property name="ShowSuspend" type="bool" value="true"\/>/' $_XFCE4SESSION
      fi

    elif [[ "$HSBTN" =~ "cd" ]]; then
      if grep -q -F '<property name="ShowHybridSleep" type="bool" value="true"/>' $_XFCE4SESSION; then
        sed -i 's/.*ShowHybridSleep.*/      <property name="ShowHybridSleep" type="bool" value="false"\/>/' $_XFCE4SESSION
      else
        sed -i 's/.*ShowHybridSleep.*/      <property name="ShowHybridSleep" type="bool" value="true"\/>/' $_XFCE4SESSION
      fi

    elif [[ "$HSBTN" =~ "de" ]]; then
      if grep -q -F '<property name="ShowSwitchUser" type="bool" value="true"/>' $_XFCE4SESSION; then
        sed -i 's/.*ShowSwitchUser.*/      <property name="ShowSwitchUser" type="bool" value="false"\/>/' $_XFCE4SESSION
      else
        sed -i 's/.*ShowSwitchUser.*/      <property name="ShowSwitchUser" type="bool" value="true"\/>/' $_XFCE4SESSION
      fi

    elif [[ "$HSBTN" =~ "ef" ]]; then
      sed -i 's/.*ShowHibernate.*/      <property name="ShowHibernate" type="bool" value="false"\/>/' $_XFCE4SESSION
      sed -i 's/.*ShowSuspend.*/      <property name="ShowSuspend" type="bool" value="false"\/>/' $_XFCE4SESSION
      sed -i 's/.*ShowHybridSleep.*/      <property name="ShowHybridSleep" type="bool" value="false"\/>/' $_XFCE4SESSION
      sed -i 's/.*ShowSwitchUser.*/      <property name="ShowSwitchUser" type="bool" value="false"\/>/' $_XFCE4SESSION
    elif [[ "$HSBTN" =~ "fg" ]]; then
      sed -i 's/.*ShowHibernate.*/      <property name="ShowHibernate" type="bool" value="true"\/>/' $_XFCE4SESSION
      sed -i 's/.*ShowSuspend.*/      <property name="ShowSuspend" type="bool" value="true"\/>/' $_XFCE4SESSION
      sed -i 's/.*ShowHybridSleep.*/      <property name="ShowHybridSleep" type="bool" value="true"\/>/' $_XFCE4SESSION
      sed -i 's/.*ShowSwitchUser.*/      <property name="ShowSwitchUser" type="bool" value="true"\/>/' $_XFCE4SESSION
    else
      zenity --info --width="280" --height="60" --timeout=5 --height="60" \
             --title="""$(gettext " Lite Tweaks - Info")" --text="""$(gettext "\nNo option was selected.\nNo changes have been made.")" 2>/dev/null; return
    fi
    zenity --question --window-icon="info" --width="320" --height="80" --title="""$(gettext " Hibernate, Suspend buttons")" \
           --text="""$(gettext "\nYour preferences have been set. Changes will take effect after next reboot.\n\n Would you like to reboot now?")" 2>/dev/null
    if [[ "$?" -eq "0" ]]; then systemctl reboot ; killall lite-tweaks ; fi
  done
}

# Set the dialogue window icon
ic="/usr/share/icons/Papirus/24x24/apps/litetweaks.png"

ARRAYS() {
# Invoke arrays
ARRAYA=()    # Array for automatic cleaning tool
ARRAYB=()    # Array for cleaning tools that require root

# Populate arrays - ARRAY A

echo "$selection" | grep "BRAVE" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('BRAVE'); fi

echo "$selection" | grep "CHROME" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('CHROME'); fi

echo "$selection" | grep "CHROMIUM" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('CHROMIUM'); fi

echo "$selection" | grep "DBROWSER" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('DBROWSER'); fi

echo "$selection" | grep "DU" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('DU'); fi

echo "$selection" | grep "FIREFOX" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('FIREFOX'); fi

echo "$selection" | grep "HSBTNS" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('HSBTNS'); fi

echo "$selection" | grep "MSEDGECA" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('MSEDGECA'); fi

echo "$selection" | grep "MIDORI" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('MIDORI'); fi

echo "$selection" | grep "OPERA" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('OPERA'); fi

echo "$selection" | grep "PALEMOON" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('PALEMOON'); fi

echo "$selection" | grep "TASKBARRES" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('TASKBARRES'); fi

echo "$selection" | grep "THUMBNAILS" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('THUMBNAILS'); fi

echo "$selection" | grep "TRASH" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('TRASH'); fi

echo "$selection" | grep "VIVALDI" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('VIVALDI'); fi

echo "$selection" | grep "WHISKER" > /dev/null
if [ $? = 0 ]; then ARRAYA+=('WHISKER'); fi

# Populate arrays - ARRAY B
echo "$selection" | grep "AUTOREMOVE" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('AUTOREMOVE'); fi

echo "$selection" | grep "FIXBOOTUP" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('FIXBOOTUP'); fi

echo "$selection" | grep "CLEARMEM" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('CLEARMEM'); fi

echo "$selection" | grep "HOSTNAME" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('HOSTNAME'); fi

echo "$selection" | grep "INSTKERNELS" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('INSTKERNELS'); fi

echo "$selection" | grep "REMKERNELS" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('REMKERNELS'); fi

echo "$selection" | grep "FILESIZES" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('FILESIZES'); fi

echo "$selection" | grep "LOGARCHIVES" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('LOGARCHIVES'); fi

echo "$selection" | grep "SYSDARCLOGS" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('SYSDARCLOGS'); fi

echo "$selection" | grep "MGMTSAVESESSION" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('MGMTSAVESESSION'); fi

echo "$selection" | grep "EDNUMLOCK" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('EDNUMLOCK'); fi

echo "$selection" | grep "APTGETCLEAN" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('APTGETCLEAN'); fi

echo "$selection" | grep "FIXAPT" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('FIXAPT'); fi

echo "$selection" | grep "KIOSKMODE" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('KIOSKMODE'); fi

echo "$selection" | grep "PRELOAD" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('PRELOAD'); fi

echo "$selection" | grep "RESIDCONFIG" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('RESIDCONFIG'); fi

echo "$selection" | grep "TLP" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('TLP'); fi

echo "$selection" | grep "ZRAM" > /dev/null
if [ $? = 0 ]; then ARRAYB+=('ZRAM'); fi
}

RUN() {
# Check if ARRAYA is empty. If empty, skip the execution
run_icon="/usr/share/icons/Papirus/24x24/apps/litetweaks.png"
if [ ${#ARRAYA[@]} -ne 0 ]; then
    x=0
    for k in "${!ARRAYA[@]}"; do  x=$(( $x + 1 )); done  # Get the total number of selected items in array A
    TOTAL_LINES=$x
    printf '%s \n' "${ARRAYA[@]}"|
    while read  line
        do
            $line     # Execute functions one by one
            if [ $? = 1 ]; then
                zenity --error --title="""$(gettext "  Lite Tweaks")" --text="\nThere was an error while performing cleaning:\n ${line}" 2>/dev/null
                exit 1
            fi
        let i++
         (( PERCENTAGE = 100 \* ${i} / ${TOTAL_LINES} ))
         echo "$PERCENTAGE"

        if [ "$PERCENTAGE" == "100" ]; then
            echo "#Done"
            sleep 1
        fi
    done| zenity --progress --width="320" --window-icon="$run_icon" --pulsate --no-cancel --title="  Lite Tweaks" --auto-kill --auto-close 2>/dev/null
fi

# Check if ARRAYB is empty. If empty, skip the execution
if [ ${#ARRAYB[@]} -ne 0 ]; then
   case $? in
    0) pkexec /usr/bin/lite-tweaks-super "${ARRAYB[@]}" # Open lite-tweaks-super with superuser privileges and pass the array B
    ;;
    1) return
  esac
fi
}

CHECK() { # Execute checks - organized following Main zenity dialog window order

# Check Autoremove packages
if [ -f "$_DRYAPT" ] && [ "$(cat "$_DRYAPT" | head -n+1)" -gt 0 ]; then
  _DRYAPTVAL=$(cat "$_DRYAPT" | head -n+1)
  _autoremove=(FALSE "AUTOREMOVE" """$(gettext "Autoremove Packages")" """$(gettext "Clean")" """$(gettext "Packages")" """$(gettext "Safe")" """$(gettext "You can autoremove") "$_DRYAPTVAL" $(gettext "packages")")
else _autoremove=()
fi

# Check if the Brave browser cache exists
if [ -d  "$HOME/.cache/BraveSoftware/Brave-Browser/Default/Cache/" ]; then
  BRCACHESIZE=$(du -sh "$HOME/.cache/BraveSoftware/Brave-Browser/Default/Cache/" | awk '{print $1}' | sed 's/$/B/')
  brave=(FALSE "BRAVE" """$(gettext "Brave Cache")" """$(gettext "Clean")" """$(gettext "Internet")" """$(gettext "Safe")" """$(gettext "Remove") "$BRCACHESIZE" $(gettext "from your Brave cache")")
else brave=()
fi

# Check if the google-chrome browser cache exists
if [ -d  "$HOME/.cache/google-chrome/" ]; then
  GCCACHESIZE=$(du -sh "$HOME/.cache/google-chrome/" | awk '{print $1}' | sed 's/$/B/')
  chrome=(FALSE "CHROME" """$(gettext "Chrome Cache")" """$(gettext "Clean")" """$(gettext "Internet")" """$(gettext "Safe")" """$(gettext "Remove") "$GCCACHESIZE" $(gettext "from your Chrome cache")")
else chrome=()
fi

# Check if the chromium browser cache exists
if [  -d  "$HOME/.cache/chromium/" ]; then
  CHRCACHESIZE=$(du -sh "$HOME/.cache/chromium/" | awk '{print $1}' | sed 's/$/B/')
  chromium=(FALSE "CHROMIUM" """$(gettext "Chromium Cache")" """$(gettext "Clean")" """$(gettext "Internet")" """$(gettext "Safe")" """$(gettext "Remove") "$CHRCACHESIZE" $(gettext "from your Chromium cache")")
else chromium=()
fi

# Check if firefox browser cache exists
if [  -d  "$HOME/.cache/mozilla/" ]; then
  FFCACHESIZE=$(du -sh "$HOME/.cache/mozilla/"| awk '{print $1}'| sed 's/$/B/')
  firefox=(FALSE "FIREFOX" """$(gettext "Firefox Cache")" """$(gettext "Clean")" """$(gettext "Internet")" """$(gettext "Safe")" """$(gettext "Remove") "$FFCACHESIZE" $(gettext "from your Firefox cache")" )
else firefox=()
fi

# Check if archived /var/log files exist
if [ "$(find /var/log -type f \( -iname \*.gz -o -iname \*.old -o -iname \vboxadd* -o -iname \*.0 -o -iname \*.1 -o -iname \*.2 -o -iname \*.3 -o -iname \*.4 -o -iname \*.5 -o -iname \*.6 -o -iname \*.7 \) 2>/dev/null -maxdepth 100 -size +1k)" ]; then
  LOGARCHIVESIZE=$(find /var/log -type f \( -iname \*.old -o -iname \*.gz -o -iname \vboxadd* -o -iname \*.0 -o -iname \*.1 -o -iname \*.2 -o -iname \*.3 -o -iname \*.4 -o -iname \*.5 -o -iname \*.6 -o -iname \*.7 \) 2>/dev/null -exec du -csh '{}' + | tail -1 | awk '{print $1}' | sed 's/$/B/')
  logarchive=(FALSE "LOGARCHIVES" """$(gettext "Log Archives")" """$(gettext "Clean")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "Remove") "$LOGARCHIVESIZE" $(gettext "worth of archived logs")")
else logarchive=()
fi

# Check if archived /var/log/journal files exist
if [ "$(find /var/log/journal -type f \( -iname \*.journal \) 2>/dev/null -maxdepth 100 -size +1k)" ]; then
  SYSDARCHIVESIZE=$(find /var/log -type f \( -iname \*.journal \) 2>/dev/null -exec du -csh '{}' + | tail -1 | awk '{print $1}' | sed 's/$/B/')
  sysdarchive=(FALSE "SYSDARCLOGS" """$(gettext "SystemD Log cleaner")" """$(gettext "Clean")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "Remove") "$SYSDARCHIVESIZE" $(gettext "worth of SystemD logs")")
else sysdarchive=()
fi

# Check if  Edge browser cache exists
if [  -d  "$HOME/.cache/microsoft-edge/" ]; then
  EDGCACHESIZE=$(du -sh "$HOME/.cache/microsoft-edge/" | awk '{print $1}' | sed 's/$/B/')
  msedge=(FALSE "MSEDGECA" """$(gettext "Microsoft Edge Cache")" """$(gettext "Clean")" """$(gettext "Internet")" """$(gettext "Safe")" """$(gettext "Remove") "$EDGCACHESIZE" $(gettext "from your Microsoft Edge cache")")
else msedge=()
fi

# Check if  midori browser cache exists
if [  -d  "$HOME/.cache/midori/" ]; then
  CHRCACHESIZE=$(du -sh "$HOME/.cache/midori/" | awk '{print $1}' | sed 's/$/B/')
  midori=(FALSE "MIDORI" """$(gettext "Midori Cache")" """$(gettext "Clean")" """$(gettext "Internet")" """$(gettext "Safe")" """$(gettext "Remove") "$CHRCACHESIZE" $(gettext "from your Midori cache")")
else midori=()
fi

# Check if  opera browser cache exists
if [  -d  "$HOME/.cache/opera/Cache/" ]; then
  CHRCACHESIZE=$(du -sh "$HOME/.cache/opera/Cache/" | awk '{print $1}' | sed 's/$/B/')
  opera=(FALSE "OPERA" """$(gettext "Opera Cache")" """$(gettext "Clean")" """$(gettext "Internet")" """$(gettext "Safe")" """$(gettext "Remove") "$CHRCACHESIZE" $(gettext "from your Opera cache")")
else opera=()
fi

# Check if  Pale Moon browser cache exists
if [  -d  "$HOME/.cache/moonchild productions/pale moon/" ]; then
  CHRCACHESIZE=$(du -sh "$HOME/.cache/moonchild productions/pale moon/" | awk '{print $1}' | sed 's/$/B/')
  palemoon=(FALSE "PALEMOON" """$(gettext "Pale Moon Cache")" """$(gettext "Clean")" """$(gettext "Internet")" """$(gettext "Safe")" """$(gettext "Remove") "$CHRCACHESIZE" $(gettext "from your Pale Moon cache")")
else palemoon=()
fi

# Check package cache exists
if [ "$(find /var/cache/apt/archives/ -maxdepth 1 -size +1000k)" ]; then
  APTCACHESIZE=$(du -sh /var/cache/apt/archives/ 2>/dev/null | awk '{print $1}' | sed 's/$/B/' )
  aptcache=(FALSE "APTGETCLEAN" """$(gettext "Package Cache")" """$(gettext "Clean")" """$(gettext "Packages")" """$(gettext "Safe")" """$(gettext "Clean") "$APTCACHESIZE" $(gettext "from your package cache")")
else aptcache=()
fi

# Check if preload is installed
if [ -z  "$(dpkg -l | grep -E '^ii' | grep preload)" ]; then
  preload=(FALSE "PRELOAD" """$(gettext "Preload Apps")" """$(gettext "Performance")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "Fetch commonly used apps into Memory")")
else preload=(FALSE "PRELOAD" """$(gettext "Preload Apps")" """$(gettext "Performance")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "Preload is currently installed (select to modify)")")
fi

# Check if there are any residual configuration files
if [ ! -z  "$(dpkg -l | grep '^rc')"  ]; then
  RESCONFIGQTY=$(dpkg -l | grep '^rc' | awk '{print $2}'| wc -w)
  resconfig=(FALSE "RESIDCONFIG" """$(gettext "Residual Config Files")" """$(gettext "Clean")" """$(gettext "Packages")" """$(gettext "Safe")" """$(gettext "Remove") "$RESCONFIGQTY" $(gettext "residual configuration files")")
else resconfig=()
fi

# Taskbar Restore
  taskres=(FALSE "TASKBARRES" """$(gettext "Taskbar Restore")" """$(gettext "Fix")" """$(gettext "UI")" """$(gettext "Safe")" """$(gettext "Restore the Taskbar and Tray icons to default")")

# Check if any thumbnails exist
if [ $(du -sk "$HOME/.cache/thumbnails/" | awk '{print $1}')  -gt 100  ]; then
  THUMBCACHESIZE=$(du -sh "$HOME"/.cache/thumbnails/ | awk '{print $1}' | sed 's/$/B/')
  thumbn=(FALSE "THUMBNAILS" """$(gettext "Thumbnail Cache")" """$(gettext "Clean")" """$(gettext "Images")" """$(gettext "Safe")" """$(gettext "Remove") "$THUMBCACHESIZE" $(gettext "from your thumbnail cache")")
else thumbn=()
fi

# Check if the Trash is empty
if [  "$(ls -A "$HOME/.local/share/Trash/files/")" ]; then
  TRASHCACHESIZE=$(du -sh "$HOME/.local/share/Trash/" | awk '{print $1}' | sed 's/$/B/')
  trash=(FALSE "TRASH" """$(gettext "Trash Bin")" """$(gettext "Clean")" """$(gettext "Home")" """$(gettext "Safe")" """$(gettext "Remove") "$TRASHCACHESIZE" $(gettext "from your Trash bin")")
else trash=()
fi

# Check if  vivaldi-stable browser cache exists
if [  -d  "$HOME/.cache/vivaldi/" ]; then
  CHRCACHESIZE=$(du -sh "$HOME/.cache/vivaldi/" | awk '{print $1}' | sed 's/$/B/')
  vivaldi=(FALSE "VIVALDI" """$(gettext "Vivaldi Cache")" """$(gettext "Clean")" """$(gettext "Internet")" """$(gettext "Safe")" """$(gettext "Remove") "$CHRCACHESIZE" $(gettext "from your Vivaldi browser cache")")
else vivaldi=()
fi

# Check if Whisker Menu has Recently Used items
if [ -z "$(cat "$HOME"/.config/xfce4/panel/whiskermenu-*.rc | grep "^recent=" | cut -d '=' -f2)" ]; then
  whisker_recent=(FALSE "WHISKER" """$(gettext "Whisker Menu")" """$(gettext "Clean")" """$(gettext "Menu")" """$(gettext "Safe")" """$(gettext "The Menu has no Recently Used items to clean")" )
else
  whisker_test=$(cat "$HOME"/.config/xfce4/panel/whiskermenu-*.rc | grep "^recent=" | cut -d '=' -f2 | sed 's/,/ /g'| wc -w)
  whisker_recent=(FALSE "WHISKER" """$(gettext "Whisker Menu")" """$(gettext "Clean")" """$(gettext "Menu")" """$(gettext "Safe")" """$(gettext "Remove") "$whisker_test" $(gettext "Recently Used items from the Menu")" )
fi

# Check if zram is installed
if [ -z  "$(dpkg -l | grep -E '^ii' | grep zram-config)" ]; then
  zram=(FALSE "ZRAM" "zRAM" """$(gettext "Performance")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "Use Memory more efficiently on older systems")")
else zram=(FALSE "ZRAM" "zRAM" """$(gettext "Performance")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "zRAM is currently installed (select to modify)")")
fi
}

# Main loop
while (true); do
MYLTUSER=$(sudo -u ${SUDO_USER:-$USER} whoami)
TMPLTF="/tmp/myltuser"
if [ ! -f "$TMPLTF" ]; then echo "$MYLTUSER" > "$TMPLTF"; chmod 600 "$TMPLTF"; else :; fi
# first run the checks
CHECK
# Main zenity dialog window
selection=$(zenity --list --checklist --width=890 --height=620 --window-icon="$ic" --ok-label="""$(gettext "Begin")" --cancel-label="""$(gettext "Quit")" --title="""$(gettext " Lite Tweaks")" \
--column="""$(gettext "Select")" --column="SELECTION" --column="""$(gettext "Name")" --column="""$(gettext "Task")" --column="""$(gettext "Category")" \
--column="""$(gettext "Grade")" --column="""$(gettext "Description")" --hide-column="2" \
--text="""$(gettext "  <span font='Roboto 13'>Select the task you wish to perform, then click the Begin button. Sort by clicking on the columns header.
\n  <b>Grade</b>:\n  <span foreground='green'>Safe</span> - Safe to perform, no harm can be done.
  <span foreground='red'>Caution</span> - Proceed with caution, involves altering the system files.
\n  You can select multiple tasks for execution (Administrative privileges are required for some tasks).\n</span>")" \
"${_autoremove[@]}" \
FALSE "FIXBOOTUP" """$(gettext "Bootup Fix")" """$(gettext "Fix")" """$(gettext "Repair")" """$(gettext "Caution")" """$(gettext "Restore the boot splash to Linux Lite")" \
"${brave[@]}" \
"${chrome[@]}" \
"${chromium[@]}" \
FALSE "CLEARMEM" """$(gettext "Clear Memory")" """$(gettext "Performance")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "Free up Memory on your system")" \
FALSE "DBROWSER" """$(gettext "Default Web Browser")" """$(gettext "Preferences")" """$(gettext "Internet")" """$(gettext "Safe")" """$(gettext "Set your default Web browser")" \
FALSE "DU" """$(gettext "Display Disk Usage")" """$(gettext "Information")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "Display overall disk usage")" \
"${firefox[@]}" \
FALSE "HSBTNS" """$(gettext "Hibernate, Suspend")" """$(gettext "Preferences")" """$(gettext "Session")" """$(gettext "Safe")" """$(gettext "Hide or show these buttons at Logout")" \
FALSE "HOSTNAME" """$(gettext "Hostname")" """$(gettext "Edit")" """$(gettext "System")" """$(gettext "Caution")" """$(gettext "Change the computer hostname")" \
FALSE "INSTKERNELS" """$(gettext "Kernel Installer")" """$(gettext "Administration")" """$(gettext "System")" """$(gettext "Caution")" """$(gettext "Install specific kernels you wish to boot from")" \
FALSE "REMKERNELS" """$(gettext "Kernel Remover")" """$(gettext "Clean")" """$(gettext "System")" """$(gettext "Caution")" """$(gettext "Remove kernels no longer needed")" \
FALSE "FILESIZES" """$(gettext "Locate Large Files")" """$(gettext "Information")" """$(gettext "System")" """$(gettext "Caution")" """$(gettext "Find large files in your system")" \
"${logarchive[@]}" \
FALSE "KIOSKMODE" """$(gettext "Login & Logout Options")" """$(gettext "Administration")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "Enable or disable basic Login & Logout options")" \
FALSE "MGMTSAVESESSION" """$(gettext "Manage Save Session")" """$(gettext "Administration")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "Enable or disable saving of your session")" \
"${msedge[@]}" \
"${midori[@]}" \
FALSE "EDNUMLOCK" """$(gettext "Numlock")" """$(gettext "Edit")" """$(gettext "System")" """$(gettext "Safe")" """$(gettext "Enable/Disable Numlock at Login")" \
"${opera[@]}" \
"${aptcache[@]}" \
FALSE "FIXAPT" """$(gettext "Package System Repair")" """$(gettext "Fix")" """$(gettext "Repair")" """$(gettext "Caution")" """$(gettext "Restore the package management system")" \
"${palemoon[@]}" \
"${preload[@]}" \
"${resconfig[@]}" \
"${sysdarchive[@]}" \
"${taskres[@]}" \
"${thumbn[@]}" \
FALSE "TLP" """$(gettext "TLP (Laptops only)")" """$(gettext "Performance")" """$(gettext "System")" """$(gettext "Caution")" """$(gettext "TLP may help improve battery life in Laptops")" \
"${trash[@]}" \
"${vivaldi[@]}" \
"${whisker_recent[@]}" \
"${zram[@]}" 2>/dev/null )

if [ "${PIPESTATUS[0]}" -ne "0" ]; then _clean_up; exit 0 ; fi # If Quit is clicked then exit

echo "$selection" | grep '[a-zA-Z0-9]' # Check if anything is selected
if [ "${PIPESTATUS[1]}" -ne "0" ]; then
  zenity --info --ok-label="""$(gettext "Go Back")" --width="320" --height="80" --title="""$(gettext " Lite Tweaks")" \
         --text="""$(gettext "\n<b>No tasks were selected for execution</b> \n\nPlease try again selecting a at least one task.")" 2>/dev/null; continue
fi

ARRAYS #Separate selected items into arrays A and array B
RUN
done
exit 0
