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

# Kill off package managers if running
if [ "$(pidof synaptic)" ];then killall -9 synaptic; fi
if [ ! -z "$(pgrep gdebi-gtk)" ];then killall -9 gdebi-gtk; fi

# variables
_MYLTUSER=$(cat "/tmp/myltuser")
_LT="Lite Tweaks"

# common functions
TWEAK_LOGS() {
_LTLOGS="/tmp/ltlogs.log"
touch "$_LTLOGS"; chown "$_MYLTUSER":"$_MYLTUSER" "$_LTLOGS"; chmod 600 "$_LTLOGS"
}

RUN_ICON() {
run_icon="/usr/share/icons/Papirus/24x24/apps/litetweaks.png"
}

VIEW_SAVE_LOG() {
if [ ${_FAILURE} -eq "1" ]; then rm -f "$_LTLOGS"; else
   zenity --text-info --width="750" --height="400" --cancel-label="Quit" --ok-label="Save..." --title="  $_LT - Log" --filename="$_LTLOGS" 2>/dev/null
  if [ "$?" -eq "0" ]; then
      # Save log
      szSavePath=$(zenity --width="550" --height="380" --title=" Save log" --file-selection --filename="/home/$_MYLTUSER/$_LTLOGS" --file-filter='*.log' \
                          --file-filter='All files | *' --save --confirm-overwrite 2>/dev/null)
      sudo -u ${SUDO_USER:-$_MYLTUSER} cp "$_LTLOGS" "$szSavePath"; rm -f "$_LTLOGS"; else rm -f "$_LTLOGS"
  fi
fi
}

# Get input from a previous file
FRM="$@"

# Check if the input is correct and populate array C
echo "$FRM" | grep "APTGETCLEAN" > /dev/null
if [ $? = 0 ]; then ARRAYC+=('CLEAN'); fi

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

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

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

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

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

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

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

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

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

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

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

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

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

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

echo $FRM | grep "AUTOREMOVE" > /dev/null
if [ $? = 0 ]; then ARRAYC+=('REMOVE'); fi

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

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

# common functions
ask-logout-super() {
  zenity --question --window-icon="info" --width="260" --height="80" --title="  $APPNAME" \
         --text="\nChanges will take effect after next login.\n\n Would you like to logout now?" 2>/dev/null
      if [[ "$?" -eq "0" ]]; then systemctl restart lightdm.service; fi
}

# Call the zenity window icon.
_ICON="/usr/share/icons/Papirus/24x24/apps/litetweaks.png"

CLEAN() {
    echo "#⚫ Cleaning the package cache..." && sleep 1
    apt-get clean
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
        zenity --error --width="260" --height="80" --title="    Error" \
               --text="\nAn error occurred while cleaning the package cache!" 2>/dev/null
        return
    fi
    echo "#✔ Package cache cleared." && sleep 1
}
# clear memory tweak
CLEARMEM() {
    echo "#⚫ Freeing up system memory, please wait..." && sleep 1
    sync; sh -c 'echo 3 >/proc/sys/vm/drop_caches'
    echo "#✔ System memory freed." && sleep 1
}
# locate large files tweak
FILES() {
    echo "#Getting ready..." && sleep 1 # prevent dialog overlapping
    echo "#⚫ Finding Large Files..."

# Main dialog window
INSTALLER_TITLE="Find and list all files in your system based on specified size ranges.
\nPlease note:\n
<b>*</b>  This is not a file removal tool. It is provided for informational purposes only.\n
<b>*</b>  Do not remove files from your system unless you are absolutely convinced that it is safe to do so.\n
Click <b>Quit</b> to exit now or <b>Continue</b> to proceed to the system scan."

# Application Name Variable
APPNAME="Find Large Files"

zenity --question --icon-name="info" --width="480" --height="200" --title="$APPNAME" --ok-label="Continue" --cancel-label="Quit" --window-icon="$_ICON" --text="${INSTALLER_TITLE}" 2>/dev/null
if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi

while (true); do echo "#Finding Large Files..." # Start scan loop
  if [ "$?" -eq "0" ]; then
      # Set the file size range
      scale_min=$(zenity --title="Minimum file size" --scale --text="Select the minimum file size (in MB)" --min-value=25 --max-value=5000 --value=25 --step=25 2>/dev/null)

      if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi

      size_min=$(echo $scale_min | sed "s/.*/+&M/")
      scale_max=$(zenity --title="Maximum file size" --scale --text="Select the maximum file size (in MB)\n• Minimum file size is $size_min\B" --min-value=25 --max-value=5000 --value=51 --step=25 2>/dev/null)

      if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi

      while [ $scale_max -le $scale_min ]; do
        zenity --info --width="280" --icon-name="error" --title="$APPNAME" --window-icon="$_ICON" --text="\nMaximum value must not be less or equal to minimum value." 2>/dev/null
        scale_max=$(zenity --title="$APPNAME" --scale --text="Select the maximum file size.\nMinimum file size is $size_min\B" --min-value="25" --max-value="5000" --value="51" --step="25" 2>/dev/null)

        if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi
      done

      size_max=$(echo $scale_max | sed "s/.*/-&M/")
      find_large=$("stdbuf -oL /bin/bash \-c '(find / -size $size_min -size $size_max -exec du -mh {} + 2>&1 | grep [0-9][MG] | sort -h -r ) 2>&1 | tee /var/log/findfilesrange.log'" |
      stdbuf -oL sed -e '/\*$/ s/^/# /p'|
      zenity --progress --pulsate --width="400" --window-icon="$_RUN_ICON" --no-cancel --auto-close --title="Scanning for files" --text="Please wait while your system is scanned..." 2>/dev/null)
      if [ "${PIPESTATUS[0]}" -ne "0" ]; then zenity --error --title="Error" --text="\n$APPNAME has failed." 2>/dev/null; return; fi
  else
    return
  fi
echo "#⚫ Displaying scan results..." && ${SUDO_USER:-$_MYLTUSER} mousepad /var/log/findfilesrange.log
echo "#⚫ Waiting for user input..."
# Scan loop question
zenity --question --width="260" --title="$APPNAME" --window-icon="$_ICON" --text="\nWould you like to run another scan?" 2>/dev/null
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; else continue; fi
done
}

# package system repair tweak
FIXAPT() {
    TWEAK_LOGS
    # Check Internet access
    echo "#⚫ Stage 1: Checking for an internet connection..."
    if eval "sudo -u ${SUDO_USER:-$_MYLTUSER} curl -sk google.com/" >> /dev/null 2>&1; then :; else # inform no internet connection
      zenity --info --width="360" --ok-label="Close" --title="$_LT - Aborted" \
             --text="\n<b>Your computer is not connected to the internet</b>
                     \nPackage System Repair requires internet access in order to fetch packages lists. The task has been aborted.
                     \nPlease check your Internet connection and try again." 2>/dev/null; break
    fi

sleep 2

    # Stage 2
    echo "#⚫ Stage 2: Running apt-get install -f."$"\n ✍ This might take some time, please wait..."
    apt-get install -f
      if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
      zenity --error --width="280" --height="80" --title="  $_LT - Error" \
             --text="\nAn error occurred while while installing partialy installed packages!" 2>/dev/null; return
      fi

sleep 3

    # Stage 3
    echo "#⚫ Stage 3: Rebuilding sources files."$"\n ✍ This might take some time, please wait..."
    # Recreate  /etc/apt/sources.list ...
    rm -f /etc/apt/sources.list.save && rm -f /etc/apt/sources.list && touch /etc/apt/sources.list
    echo "###### Ubuntu Main Repos" >> /etc/apt/sources.list
    add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main restricted universe multiverse"
    echo $'\n' >> /etc/apt/sources.list
    echo "###### Ubuntu Update Repos" >> /etc/apt/sources.list
    add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc)-security main restricted universe multiverse"
    add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc)-updates main restricted universe multiverse"
    add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc)-backports main restricted universe multiverse"
    echo $'\n' >> /etc/apt/sources.list
    echo "###### Ubuntu Partner Repos" >> /etc/apt/sources.list
    add-apt-repository -y "deb http://archive.canonical.com/ $(lsb_release -sc) partner"
    cp /etc/apt/sources.list /etc/apt/sources.list.save

    #Check the release version to reset /etc/apt/sources.list.d/linuxlite.list
    _LLVER=$(awk '{print $3}' /etc/llver | cut -d. -f1)
    _REL_VERSION_NAME=(emerald) # release version name array

    # Update /etc/apt/sources.list.d/linuxlite.list for EMERALD
    if [ "$_LLVER" == "5"  ]; then
        # Check for /etc/apt/sources.list.d/linuxlite.list file... If file exists:
        if  [ -f /etc/apt/sources.list.d/linuxlite.list.save ]; then
            rm -f /etc/apt/sources.list.d/linuxlite.list && rm -f /etc/apt/sources.list.d/linuxlite.list.save
            touch /etc/apt/sources.list.d/linuxlite.list
            echo "deb http://repo.linuxliteos.com/linuxlite/ ${_REL_VERSION_NAME[0]} main" >> /etc/apt/sources.list.d/linuxlite.list
            cp /etc/apt/sources.list.d/linuxlite.list  /etc/apt/sources.list.d/linuxlite.list.save

        # If file does not exist:
        elif [ ! -f /etc/apt/sources.list.d/linuxlite.list.save ]; then
            rm -f /etc/apt/sources.list.d/linuxlite.list
            touch /etc/apt/sources.list.d/linuxlite.list
            echo "deb http://repo.linuxliteos.com/linuxlite/ ${_REL_VERSION_NAME[0]} main" >> /etc/apt/sources.list.d/linuxlite.list
            cp /etc/apt/sources.list.d/linuxlite.list  /etc/apt/sources.list.d/linuxlite.list.save
        fi

sleep 2

    # Stage 4
    echo "#⚫ Stage 4: Cleaning the package system."$"\n ✍ This might take some time, please wait..."
    [ -d "/var/lib/apt/lists.old" ] || rm -Rf /var/lib/apt/lists.old
    apt-get clean && cd /var/lib/apt && mv lists lists.old && mkdir -p lists/partial && apt-get clean && apt-get update 2>&1

    # DON'T UPDATE UNSUPPORTED RELEASE - Sample
    elif [ "$_LLVER" == "6" ]; then
        zenity --warning --width=340 --height=80 --title="     $_LT - Warning" \
               --text="\n<b>Unsupported Linux Lite release version found</b> \n \n/etc/apt/sources.list.d/linuxlite.list and /etc/apt/sources.list.d/linuxlite.list.save files will not be reset. " 2>/dev/null
    fi

    # Unset variables used during FIXAPT
    unset _REL_VERSION_NAME
    unset _LLVER

sleep 2

    # Final Stage
    echo "#⚫ Final Stage: Running dpkg --configure -a"$"\n ✔ Completing Repair..."
    dpkg --configure -a && sleep 2
}

# bootup fix tweak
FIXBOOTUP() {
echo "#⚫ Fixing the bootup. This might take a while..."
# Check LL version
LL_VERSION=$(awk '{print $3}' /etc/llver 2>/dev/null|| echo "Unknown")
case $LL_VERSION in
5.0)
title="Linux Lite 5.0"
black="0x000000"
white="0xffffff"
brown="0xffe082"
blue="0x000000"
;;
5.2)
title="Linux Lite 5.2"
black="0x000000"
white="0xffffff"
brown="0xffe082"
blue="0x000000"
;;
5.4)
title="Linux Lite 5.4"
black="0x000000"
white="0xffffff"
brown="0xffe082"
blue="0x000000"
;;
5.6)
title="Linux Lite 5.6"
black="0x000000"
white="0xffffff"
brown="0xffe082"
blue="0x000000"
;;
5.8)
title="Linux Lite 5.8"
black="0x000000"
white="0xffffff"
brown="0xffe082"
blue="0x000000"
;;
Unknown)
zenity --info --title="Bootup fix" --text="Unknown OS" 2>/dev/null
return
;;
*)
zenity --info --title="$_LT" --text="\nBootup fix\nRelease not supported!" 2>/dev/null
return
;;
esac

APPNAME_ERROR=" Bootup Fix - Error"

sed -i "s/^title=.*$/title=$title/;s/^black=.*$/black=$black/;s/white=.*$/white=$white/;s/^brown=.*$/brown=$brown/;s/^blue=.*$/blue=$blue/" /usr/share/plymouth/themes/ubuntu-text/ubuntu-text.plymouth
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
    zenity --error --width="260" --height="80" --title="$APPNAME_ERROR" \
           --text="An error occurred while fixing the bootup! \n \nUnable to write to /etc/lsb-release" 2>/dev/null; return
  fi

update-initramfs -u
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
    zenity --error --width="260" --height="80" --title="$APPNAME_ERROR" \
           --text="An error occurred while fixing the bootup! \n \nStatus: update-initramfs failed" 2>/dev/null; return
  fi

echo "Linux Lite $LL_VERSION LTS \n \l" | tee /etc/issue
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
    zenity --error --width="260" --height="80" --title="$APPNAME_ERROR" \
           --text="An error occurred while fixing the bootup! \n \nUnable to write to /etc/issue" 2>/dev/null; return
  fi

sed -i 's/^DISTRIB_DESCRIPTION=.*$/DISTRIB_DESCRIPTION="Linux Lite '"$LL_VERSION"'"/g' /etc/lsb-release
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
    zenity --error --width="260" --height="80" --title="$APPNAME_ERROR" \
           --text="An error occurred while fixing the bootup! \n \nUnable to write to /etc/lsb-release" 2>/dev/null; return
  fi
echo "#✔ Bootup fix successfully completed" && sleep 1
}

# Hostname tweak
HNAME() {
_HOSTNAME=$(hostname)
echo "#Changing hostname. Please wait..." && sleep 1 ; echo "#Changing hostname... ${_HOSTNAME}"

while (true); do
_NEW_HOSTNAME=$(zenity --entry --width="350" --window-icon="$_ICON" --ok-label="Change" --cancel-label="Quit" --title=" Change Hostname" \
                 --text="Your computer's current hostname is: ' ${_HOSTNAME} '\n\nEnter the desired new hostname (e.g.: LinuxLite) below \nand click 'Change' to proceed.
                 \nNote: The hostname must not be empty, or start with \nhashtags, or contain whitespaces.\n" --entry-text="${_HOSTNAME}" 2>/dev/null)

if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi

# If hostname is only blank spaces or starts with a hashtag, warn and loop
echo "$_NEW_HOSTNAME" | egrep "^[[:space:]]*$|^#|[[:space:]]|*_.*" >/dev/null
  if [ $? = 0 ]; then
    zenity --warning --width="330" --height="60" --ok-label="Go Back" --title=" Hostname not changed" \
           --text="\nThe new hostname must not be empty, or start with hashtags, or contain white spaces or underscores.\n\nThe hostname cannot be changed." 2>/dev/null; continue
  # If old hostname and new hostname are the same, warn and loop
  elif [ "$_NEW_HOSTNAME" = "$_HOSTNAME" ]; then zenity --info --width="300" --height="60" --timeout="5" --ok-label="Back" \
    --title=" Hostname not changed" --text="\nNo new hostname was entered. Please try again." 2>/dev/null; continue
  else
    echo "#⚫ Changing hostname to ' $_NEW_HOSTNAME ' " && sleep 1
    echo "$_NEW_HOSTNAME" > /proc/sys/kernel/hostname
    sed -i 's/127.0.1.1.*/127.0.1.1\t'"$_NEW_HOSTNAME"'/g' /etc/hosts
    echo "$_NEW_HOSTNAME" > /etc/hostname
    systemctl restart NetworkManager
    cd /home/$_MYLTUSER; su $_MYLTUSER -c "xauth add $(xauth list | grep "$_HOSTNAME" | sed 's/'"$_HOSTNAME"'\//'"$_NEW_HOSTNAME"'\//g' | awk 'NR==1 {print}')"
    cp /usr/local/sbin/xauth-cleanup.desktop /home/"$_MYLTUSER"/.config/autostart/
    chown "$_MYLTUSER":"$_MYLTUSER" /home/"$_MYLTUSER"/.config/autostart/xauth-cleanup.desktop
    # update netbios name = hostname in Samba smb.conf
    _SMBCONF="/etc/samba/smb.conf"
    sed -i "s/netbios name =.*/netbios name = "$_NEW_HOSTNAME"/" "$_SMBCONF"
    echo "#⚫ Updating services, please wait..." && sleep 1
    systemctl reload smbd && systemctl restart nmbd # restart/reload samba services

    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
      zenity --error --width="290" --height="60" --ok-label="Close" \
             --title=" Error" --text="\nAn error occurred while changing the hostname." 2>/dev/null; return
    else
      zenity --info --width="340" --height="60" --timeout="8" --ok-label="Close" \
             --title=" Hostname changed" --text="\nThe hostname has been successfully updated.\n\nClose and reopen any Terminal window you may have open to reflect changes." 2>/dev/null; return
    fi
  fi
done
}
# kernel install tweak
INSTKERNELS() {
TWEAK_LOGS
RUN_ICON
# variables
_APPNAME="Kernel Installer"
_DL="Downloading and installing Kernel"
_VERSIONS="/tmp/ltkernvers"
touch "$_VERSIONS"; chmod 600 "$_VERSIONS"

updates_list () {
  # Repositories registered
  _SLIST=$(grep '^deb ' -c /etc/apt/sources.list)
  _SLISTD=$(grep -R --exclude="*.save" '^deb ' -c /etc/apt/sources.list.d/ | grep -c /)
  APTUPDATE=$(($_SLIST + $_SLISTD)) # Total of repositories registered, this is approximated
  apt-get update 2>&1 | tee "$_LTLOGS" |
  awk -v total=$APTUPDATE '/^Ign|^ Get/{count++;$1=""} FNR { if (total != 0){percentage=int (100*count/total);print (percentage < 90?percentage:90),"\n#",substr($0, 0, 128) }; fflush(stdout)}' \
  | zenity --progress --pulsate --width="600" --text="Updating package lists..." --window-icon="$run_icon" --title="Updating Software Sources - please wait..." --percentage="0" --no-cancel --auto-close 2>/dev/null

    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
      zenity --error --height="80" --ok-label="Close" --title="$_APPNAME - Error" --text="\n$_APPNAME couldn't fetch the package cache\ninformation lists." 2>/dev/null; return
    fi
  unset APTUPDATE
}

echo "# Kernel Installer..."

if eval "curl -sk google.com" >> /dev/null 2>&1; then :;
   else # Prompt ERROR internet connection check failed and exit
     zenity --info --width=320 --height=120 --ok-label="Close" --title="$_APPNAME - Aborted" \
            --text="\n<b>Your computer is not connected to the internet</b> \n\n$_APPNAME cannot continue. \nPlease check your Internet connection and try again." 2>/dev/null; exit 0
fi
if [ ! -s "$_VERSIONS" ]; then
  zenity --question --width="340" --height="80" --title="$_APPNAME" --window-icon="$run_icon" --ok-label="Continue" --cancel-label="Cancel" \
         --text="\n$_APPNAME will now fetch the Updates list.\n\nClick <b>Cancel</b> to exit now or <b>Continue</b> to proceed." 2>/dev/null
  if [ "$?" -ne "0" ]; then return; fi
  # fetch updates list
  updates_list
  # create kernel version list
  apt search linux-headers-linuxlite 2>/dev/null | cut -d'/' -f1 | sed -e '/Header/d' -e '/Linux/d' -e '/^$/d' | awk '{print $1}' | cut -c 25- | sort -t. -k1,1n -k2,2n | tail -n+3 > "$_VERSIONS"
else :
fi

while (true); do
  while read -r version; do
    versions+=("  FALSE  "); versions+=("$version"); versions+=("  Linux Lite Kernel Version ${version}")
  done < "$_VERSIONS"

  KERNLIST=$(zenity --width="360" --height="360" --window-icon="$run_icon" --title=" $_APPNAME" --hide-header --ok-label="Install" \
  --list --radiolist --text='<span font="Sans 11">         Select Kernel to install</span>\n' --hide-column="2" --print-column="2" \
  --column=" Pick " --column="select_ver" --column="Kernel version" "${versions[@]}" 2>/dev/null)

  if [ "$?" -eq "1" ]; then return; fi
  if [ -z "$KERNLIST" ]; then
    zenity --info --width="225" --height="60" --ok-label="Go Back" --title=" $_APPNAME" --text="\nNo kernel was selected for installation." 2>/dev/null; continue
  fi

  if [ "$KERNLIST" == "4.11.0" ]; then zenity --question --window-icon="warning" --icon-name="gtk-dialog-warning" --width="330" --height="80" --title="$_APPNAME - Important" \
  --text="<b>WARNING:</b>\n\nIf <b>NVIDIA or ATI</b> proprietary graphics drivers are installed, this Kernel may break their functionality.
\nIf you decide to continue with this Kernel install, you should be competent in repairing your graphics drivers \
as well as at getting out of trouble from a TTY, otherwise <b>DO NOT</b> install this Kernel.\n\nAre you sure you want to continue with the installation?" 2>/dev/null
      if [ "$?" -eq "1" ]; then continue; else :; fi
  fi

SHOWPROGINST=$(grep '^deb' -c /etc/apt/sources.list)
  if [[ "$KERNLIST" =~ ^(4.11.0|4.12.0)$ ]]; then
    for selected_kernel in "$KERNLIST"; do
      DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
      install linux-headers-linuxlite-"$selected_kernel" linux-image-linuxlite-"$selected_kernel" linux-firmware-image-linuxlite-"$selected_kernel" -y 2>&1 |
      awk -v total=$SHOWPROGINST '/^Ign|^ Get/{count++;$1=""} FNR { if (total != 0){percentage=int (1000*count/total);print (percentage < 90?percentage:99),"\n#",substr($0, 0, 128) }; fflush(stdout)}' |
      tee -a "$_LTLOGS" |
      zenity --progress --window-icon="$run_icon" --width="680" --height="80" --title="$_APPNAME" --text="$_DL $selected_kernel, please standby..." --percentage="0" --no-cancel --pulsate --auto-close 2>/dev/null
    done
  else
    for selected_kernel in "$KERNLIST"; do
      DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
      install linux-headers-linuxlite-"$selected_kernel" linux-image-linuxlite-"$selected_kernel" -y 2>&1 |
      awk -v total=$SHOWPROGINST '/^Ign|^ Get/{count++;$1=""} FNR { if (total != 0){percentage=int (1000*count/total);print (percentage < 90?percentage:99),"\n#",substr($0, 0, 128) }; fflush(stdout)}' |
      tee -a "$_LTLOGS" |
      zenity --progress --window-icon="$run_icon" --width="680" --height="80" --title="$_APPNAME" --text="$_DL $selected_kernel, please standby..." --percentage="0" --no-cancel --pulsate --auto-close 2>/dev/null
    done
  fi

  if [ "${PIPESTATUS[0]}" -ne "0" ]; then
    _FAILURE=$(zenity --question --icon-name="error" --window-icon="error" --width="280" --title="$_APPNAME - Error" \
                      --text="\nAn error occurred while downloading and\ninstalling Kernel Version $selected_kernel.\n\nWould you like to review the logs?" 2>/dev/null ; echo $?)
    sed -i '1 i\===========================\
Kernel Installer Error log\
===========================\
An error occurred while downloading and installing Kernel Version '${selected_kernel}'.\
Go to https://www.linuxliteos.com/forums/ and paste the log below into a new or existing thread for assistance.\n\
============ Log ===========\n' "$_LTLOGS"
    sed -i '/^\<0\>[[:space:]]/d' "$_LTLOGS"
    VIEW_SAVE_LOG; return
  else
    zenity --info --width="350" --height="80" --window-icon="$run_icon" --title="$_APPNAME" \
           --text="\nKernel Version $selected_kernel has been successfully installed.\nPlease reboot your system for changes to take effect." 2>/dev/null
    rm -f "$_LTLOGS"; return
  fi
done
}
# kernel remover tweak
REMKERNELS() {
TWEAK_LOGS
RUN_ICON
while (true); do
echo "#Kernel remover..."
APPNAME=" Kernel Remover"
# Check current Kernel
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
# Define what Kernel packages to look for
LINUXPKG="linux-(image|headers|firmware-image|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|firmware-image|restricted-modules)-(generic|i386|i686|server|common|rt|xen)"
# Find old Kernels
OLDKERNELS=$(dpkg -l|awk '{print $2}' | grep -E $LINUXPKG | grep -vE $METALINUXPKG | grep -v $CURKERNEL)
# Check if there are old Kernels
echo $OLDKERNELS | grep linux*
  if [ "${PIPESTATUS[1]}" -ne "0" ]; then
        zenity --info --width="260" --height="60" --title=" $APPNAME - Information" --text="\nNo additional kernels were found in your system for removal." 2>/dev/null; return
  fi
# sed command adds all FALSE entries in the column 'Select'
zen=(`echo "$OLDKERNELS" | sort -t. -k1,1n -k2,2n | sed -e  's/^/FALSE\n/'`)

# Main dialog window
DIALOG_TEXT='<span font="Sans 10">  For each kernel version to remove, select its:\n\n    <b>headers</b>\n    <b>image</b>\n    <b>image-extra</b>\n    <b>firmware</b> (if any)</span>\n
<span font="Sans 9">  The removal process will not begin until your selection\n  is reviewed and confirmed.</span>\n'

# Call the dialog. Don't change the separator here!
okernel=$(zenity --window-icon="$run_icon" --list --checklist --separator=" " --width=360 --height=400 --column="Select" --column="Kernels available for removal" \
                 --text="${DIALOG_TEXT}" --title="$APPNAME" --ok-label="Remove" "${zen[@]}" 2>/dev/null)
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi # If cancel is clicked then exit

  # Check if anything is selected
  echo $okernel | grep linux*
  if [ "${PIPESTATUS[1]}" -ne "0" ]; then
    zenity --info --width="260" --height="60" --window-icon="$run_icon" --title=" $APPNAME" --ok-label="Go Back" \
           --text="\nNo kernel headers and image were selected for removal. Try again." 2>/dev/null; continue
  fi

  # Split kernel selection
  selection_array=($okernel)
  KN_NAMES=""
  for ((i=0; i<${#selection_array[@]}; ++i)); do
    KN_NAMES=$KN_NAMES"✔ "${selection_array[$i]}"\n"
  done
  # Confirm kernel selection removal
SHOWPROGREM=$(grep '^deb' -c /etc/apt/sources.list)
  zenity --question --window-icon="$run_icon" --width="280" --height="60" --title=" $APPNAME"  --text="\nYou are about to remove:<b>\n\n$KN_NAMES </b>\nDo you want to proceed?" 2>/dev/null
  if [ "$?" -eq "0" ]; then
    echo "Removing kernels: ${okernel}" | tee "$_LTLOGS"
    apt-get purge -y $okernel; apt-get autoremove -y |
    awk -v total=$SHOWPROGREM '/^Ign|^ Get/{count++;$1=""} FNR { if (total != 0){percentage=int (1000*count/total);print (percentage < 90?percentage:99),"\n#",substr($0, 0, 128) }; fflush(stdout)}' |
    tee -a "$_LTLOGS" | zenity --progress --width="620" --height="80" --window-icon="$run_icon" --title=" $APPNAME" --percentage="0" --no-cancel --pulsate --auto-close --auto-kill \
                               --text="Removing selected kernels, please wait..." 2>/dev/null
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
      _FAILURE=$(zenity --question --icon-name="error" --window-icon="error" --width="240" --height="60" --title="$APPNAME Error" \
                        --text="\nAn error occurred while removing selected kernels.\n\nWould you like to review the logs?" 2>/dev/null ; echo $?)
      sed -i '1 i\===========================\
Kernel Remover Error log\
===========================\
An error occurred while removing selected kernels.\
Go to https://www.linuxliteos.com/forums/ and paste the log below into a new or existing thread for assistance.\n\
============ Log ===========\n' "$_LTLOGS"
      sed -i '/^\<0\>[[:space:]]/d' "$_LTLOGS"
      VIEW_SAVE_LOG; return
    else
      zenity --info --width="270" --title="$APPNAME" --window-icon="$run_icon" --text="\nSelected kernels were successfully removed." 2>/dev/null; rm -f "$_LTLOGS"; return
    fi
  fi
done
}
# login / logout options tweak
KIOSKMODE() {
# kiosk variables
kiosk_dir=/etc/xdg/xfce4/kiosk
kioskrc=/etc/xdg/xfce4/kiosk/kioskrc
pwrlogin=/etc/lightdm/lightdm-gtk-greeter.conf
APPNAME="Login & Logout Options"
APPICONS="/usr/share/liteappsicons/litetweaks"
ic="/usr/share/pixmaps/litetweaks.png"

if [ -d $kiosk_dir ]; then
  # check whether kioskrc and its settings exist
  if [ -f $kioskrc ]; then
      grep -q -F '[xfce4-panel]' $kioskrc || echo '[xfce4-panel]' >> $kioskrc
      grep -q -F 'CustomizePanel' $kioskrc || echo 'CustomizePanel=ALL' >> $kioskrc
      grep -q -F '[xfce4-session]' $kioskrc || echo '[xfce4-session]' >> $kioskrc
      grep -q -F 'CustomizeSplash' $kioskrc || echo 'CustomizeSplash=ALL' >> $kioskrc
      grep -q -F 'CustomizeChooser' $kioskrc || echo 'CustomizeChooser=ALL' >> $kioskrc
      grep -q -F 'CustomizeLogout' $kioskrc || echo 'CustomizeLogout=ALL' >> $kioskrc
      grep -q -F 'CustomizeCompatibility' $kioskrc || echo 'CustomizeCompatibility=%sudo' >> $kioskrc
      grep -q -F 'CustomizeSecurity' $kioskrc || echo 'CustomizeSecurity=NONE' >> $kioskrc
      grep -q -F 'Shutdown' $kioskrc || echo 'Shutdown=ALL' >> $kioskrc
      grep -q -F 'SaveSession' $kioskrc || echo 'SaveSession=ALL' >> $kioskrc
  else # if kioskrc and its settings don't exist, create defaults
      (umask 133;touch $kioskrc && chown root:root $kiosk_dir -R)
      printf "[xfce4-panel]\nCustomizePanel=ALL\n\n[xfce4-session]\nCustomizeSplash=ALL
CustomizeChooser=ALL\nCustomizeLogout=ALL\nCustomizeCompatibility=%%sudo
CustomizeSecurity=NONE\nShutdown=ALL\nSaveSession=ALL\n" >> $kioskrc
  fi
else
  mkdir $kiosk_dir
  (umask 133;touch $kioskrc && chown root:root $kiosk_dir -R)
printf "[xfce4-panel]\nCustomizePanel=ALL\n\n[xfce4-session]\nCustomizeSplash=ALL
CustomizeChooser=ALL\nCustomizeLogout=ALL\nCustomizeCompatibility=%%sudo
CustomizeSecurity=NONE\nShutdown=ALL\nSaveSession=ALL\n" >> $kioskrc
fi

while (true); do
echo '#Configuring Login & Logout options...'
grep -q "Shutdown=ALL" $kioskrc && echo $?
  if [ $? = 0 ]; then appicon1=$APPICONS/appicon1-on.png; else appicon1=$APPICONS/appicon1-off.png; fi
grep -q "Shutdown=NONE" $kioskrc && echo $?
  if [ $? = 0 ]; then appicon3=$APPICONS/appicon1-on.png; else appicon3=$APPICONS/appicon1-off.png; fi
grep -q "Shutdown=%sudo" $kioskrc && echo $?
  if [ $? = 0 ]; then appicon2=$APPICONS/appicon2-on.png; else appicon2=$APPICONS/appicon2-off.png; fi
grep -q "power" $pwrlogin && echo $?
  if [ $? = 0 ]; then
    appicon5=$APPICONS/login-power-on.png ; appicon6=$APPICONS/login-power-off1.png
  else appicon5=$APPICONS/login-power-on1.png ; appicon6=$APPICONS/login-power-off.png
  fi
  appicon4=$APPICONS/power-opt.png ; appicon7=$APPICONS/blank.png
answ=$(zenity --list --imagelist --title="$APPNAME" --window-icon="$_ICON" --width="630" --height="430" --hide-header \
              --cancel-label=" Quit" --ok-label=" Apply" --hide-column=2 --print-column=2 \
              --text='\n<span font="Sans Bold 11">                               Login and Logout Power Options:</span>\n' \
              --column=" " --column="ANSW" --column=" Mode" --column=" Description" \
              $appicon4 "40" "⚫ Login Screen" "Power Options" \
              $appicon5 "50" "Enable power options" "Shows power options on login screen" \
              $appicon6 "60" "Disable power options" "Hides power options on login screen" \
              $appicon7 "40" "" "" \
              $appicon4 "40" "⚫ Log Out Screen" "" \
              $appicon3 "10" "Show Logout only for all users" "Hides all other options for all accounts" \
              $appicon2 "20" "Show Logout only, except for admins" "Hides all other options, except for admins" \
              $appicon1 "30" "Show all options" "Shows all shutdown options for all users" 2>/dev/null)

  if [ "$?" -eq "1" ]; then break; fi
  # loop when no option is selected
  if [[ $answ -eq "0" ]]; then
      zenity  --info --window-icon="$_ICON" --title=" $APPNAME" --text="\nYou did not select an option. Please try again." 2>/dev/null; continue
  fi

  if [[ $answ -eq "10" ]]; then
    if grep -q "Shutdown=NONE" $kioskrc; then
      echo "#⚫ Show Logout only for all users..." && sleep .5 && echo "#⚫ No changes needed." && sleep 1
    else
      echo '#⚫ Show Logout only for all users...' && sleep 1 ; sed -i 's/Shutdown=.*/Shutdown=NONE/' $kioskrc
      echo '#✔ Show Logout only for all users.' && sleep 1
      ask-logout-super
    fi
  elif [[ $answ -eq "20" ]]; then
    if grep -q "Shutdown=%sudo" $kioskrc; then
      echo "#⚫ Show Logout only, except for admins..." && sleep .5 && echo "#⚫ No changes needed." && sleep 1
    else
      echo '#⚫ Show Logout only, except for admins...' && sleep 1 ; sed -i 's/Shutdown=.*/Shutdown=%sudo/' $kioskrc
      echo '#✔ Show Logout only, except for admins.' && sleep 1
      ask-logout-super
    fi
  elif [[ $answ -eq "30" ]]; then
    if grep -q "Shutdown=ALL" $kioskrc; then
      echo "#⚫ Show all shutdown options..." && sleep .5 && echo "#⚫ No changes needed." && sleep 1
    else
      echo '#⚫ Show all shutdown options...' && sleep 1 ; sed -i 's/Shutdown=.*/Shutdown=ALL/' $kioskrc
      echo '#✔ Show all shutdown options.' && sleep 1
      ask-logout-super
    fi
  # loop when no option is selected
  elif [[ $answ -eq "40" ]]; then
    echo '#No power option selected...' && sleep 1

  # Login screen power options
  elif [[ $answ -eq "50" ]]; then
    if grep -q "~power" $pwrlogin; then
      echo "#⚫ Enabling power options on login screen..." && sleep .5 && echo "#⚫ No changes needed." && sleep 1
    else
      echo '#⚫ Enabling power options on login screen...' && sleep .5 ; sed -i 's/indicators =.*/indicators = ~host;~spacer;~clock;~spacer;~power/' $pwrlogin
      echo '#✔ Power options enabled on login screen.' && sleep 1
      ask-logout-super
    fi
  elif [[ $answ -eq "60" ]]; then
    if grep -q "~~Linux Lite" $pwrlogin; then
      echo "#⚫ Disabling power options on login screen..." && sleep .5 && echo "#⚫ No changes needed." && sleep 1
    else
      echo '#⚫ Disabling power options on login screen...' && sleep 1 ; sed -i 's/indicators =.*/indicators = ~host;~spacer;~clock;~spacer;~~Linux Lite/' $pwrlogin
      echo '#✔ Power options disabled on login screen.' && sleep 1
      ask-logout-super
    fi
  else continue;
  fi
  done
}

LARCHIVES() {
echo "#Deleting archived logs..."
find /var/log \( -name '*.gz' -o -name '*.old' -o -name 'vboxadd*' -o -name '*.0' -o -name '*.1' -o -name '*.2' -o -name '*.3' -o -name '*.4' -o -name '*.5' -o -name '*.6' -o -name '*.7' \) -exec rm -rf {} \;
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
    zenity --error --width="260" --height="80" --title="    Error" --text="\nAn error occurred while deleting archived logs!" 2>/dev/null; return
  fi
echo "#✔ Archived logs cleared" && sleep 1
}

SYSDLOGS() {
echo "#Deleting SystemD logs..."
find /var/log/journal \( -name '*.journal' \) -exec rm -rf {} \;
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
    zenity --error --width="260" --height="80" --title="    Error" --text="\nAn error occurred while deleting SystemD logs!" 2>/dev/null; return
  fi
echo "#✔ SystemD logs cleared" && sleep 1
}

MGMTSAVESESSION() {
    echo "#Manage Save Session..." && sleep 1
    kiosk_dir=/etc/xdg/xfce4/kiosk
    kioskrc=/etc/xdg/xfce4/kiosk/kioskrc
    APPNAME="Manage Save Session"
    LITEAPPSICONS=/usr/share/liteappsicons/litetweaks
    ic="/usr/share/pixmaps/litetweaks.png"

    if [ -d $kiosk_dir ]; then
    # check whether kioskrc and its settings exist
        if [ -f $kioskrc ]; then
            grep -q -F '[xfce4-panel]' $kioskrc || echo '[xfce4-panel]' >> $kioskrc
            grep -q -F 'CustomizePanel' $kioskrc || echo 'CustomizePanel=ALL' >> $kioskrc
            grep -q -F '[xfce4-session]' $kioskrc || echo '[xfce4-session]' >> $kioskrc
            grep -q -F 'CustomizeSplash' $kioskrc || echo 'CustomizeSplash=ALL' >> $kioskrc
            grep -q -F 'CustomizeChooser' $kioskrc || echo 'CustomizeChooser=ALL' >> $kioskrc
            grep -q -F 'CustomizeLogout' $kioskrc || echo 'CustomizeLogout=ALL' >> $kioskrc
            grep -q -F 'CustomizeCompatibility' $kioskrc || echo 'CustomizeCompatibility=%sudo' >> $kioskrc
            grep -q -F 'CustomizeSecurity' $kioskrc || echo 'CustomizeSecurity=NONE' >> $kioskrc
            grep -q -F 'Shutdown' $kioskrc || echo 'Shutdown=ALL' >> $kioskrc
            grep -q -F 'SaveSession' $kioskrc || echo 'SaveSession=ALL' >> $kioskrc
        else # if kioskrc and its settings don't exist, create defaults
            (umask 133;touch $kioskrc && chown root:root $kiosk_dir -R)
            printf "[xfce4-panel]\nCustomizePanel=ALL\n\n[xfce4-session]\nCustomizeSplash=ALL
CustomizeChooser=ALL\nCustomizeLogout=ALL\nCustomizeCompatibility=%%sudo
CustomizeSecurity=NONE\nShutdown=ALL\nSaveSession=ALL\n" >> $kioskrc
        fi
    else
        mkdir $kiosk_dir
        (umask 133;touch $kioskrc && chown root:root $kiosk_dir -R)
        printf "[xfce4-panel]\nCustomizePanel=ALL\n\n[xfce4-session]\nCustomizeSplash=ALL
CustomizeChooser=ALL\nCustomizeLogout=ALL\nCustomizeCompatibility=%%sudo
CustomizeSecurity=NONE\nShutdown=ALL\nSaveSession=ALL\n" >> $kioskrc
    fi
# main loop
while (true); do
echo "#Manage Save Session..."
grep -q "SaveSession=ALL" $kioskrc && echo $?
  if [ $? = 0 ]; then appicon1=$LITEAPPSICONS/appicon1-on.png; else appicon1=$LITEAPPSICONS/appicon1-off.png; fi
grep -q "SaveSession=NONE" $kioskrc && echo $?
  if [ $? = 0 ]; then appicon3=$LITEAPPSICONS/appicon3-on.png; else appicon3=$LITEAPPSICONS/appicon3-off.png; fi
grep -q "SaveSession=%sudo" $kioskrc && echo $?
  if [ $? = 0 ]; then appicon2=$LITEAPPSICONS/appicon2-on.png; else appicon2=$LITEAPPSICONS/appicon2-off.png; fi
# main dialog
answ=$(zenity --list --imagelist --title="$APPNAME" --window-icon="$_ICON" --width="520" --height="270" \
              --cancel-label=" Quit" --ok-label=" Apply" --hide-column=2 --print-column=2 \
              --text='                            <span font="Sans Bold 10">Enable or Disable Save Session:</span>\n' \
              --column=" " --column="ANSW" --column=" Save Session" --column=" Description" \
              $appicon1 "30" "Enable for all users" "Enables Save Session for all users" \
              $appicon2 "20" "Enable for admins" "Enables Save Session for Admins only" \
              $appicon3 "10" "Disable for all users" "Disables Save Session for all users" 2>/dev/null)
# exit on Quit or X
if [ "$?" -eq "1" ]; then break; fi
# prompt when no option is selected
if [[ $answ -eq "0" ]]; then
    zenity  --info --title="$APPNAME" --text="\nYou did not select an option. Please try again." 2>/dev/null; continue
fi
# disables Save Session for all users
if [[ $answ -eq "10" ]]; then
  if grep -q "SaveSession=NONE" $kioskrc; then
    echo "#⚫ Disabling Save Session for all users..." && sleep .5 && echo "#⚫ No changes needed." && sleep 1
  else
    sed -i 's/SaveSession=.*/SaveSession=NONE/' $kioskrc
    echo '#⚫ Disabling Save Session for all users...' && sleep .5 && echo "#✔ Disabling Save Session for all users." && sleep 1
    ask-logout-super
  fi
# enable Save Session for admins only
elif [[ $answ -eq "20" ]]; then
  if grep -q "SaveSession=%sudo" $kioskrc; then
    echo "#⚫ Enabling Save Session for admins only..." && sleep .5 && echo "#⚫ No changes needed." && sleep 1
  else
    sed -i 's/SaveSession=.*/SaveSession=%sudo/' $kioskrc
    echo '#⚫ Enabling Save Session for admins only...' && sleep .5 && echo "#✔ Enabling Save Session for admins only." && sleep 1
    ask-logout-super
  fi
# enable Save Session for all users
elif [[ $answ -eq "30" ]]; then
  if grep -q "SaveSession=ALL" $kioskrc; then
    echo "#⚫ Enabling Save Session for all users..." && sleep .5 && echo "#⚫ No changes needed." && sleep 1
  else
    sed -i 's/SaveSession=.*/SaveSession=ALL/' $kioskrc
    echo '#⚫ Enabling Save Session for all users...' && sleep .5 && echo "#✔ Enabling Save Session for all users." && sleep 1
    ask-logout-super
  fi
else continue;
fi
done
}
# numlock tweak
NUMLOCK() {
echo "#Getting Numlock status..." && sleep 1
APPNAME="   $_LT - Numlock"
LITEAPPSICONS=/usr/share/liteappsicons/litetweaks
# The loop
while (true); do
echo "#Configuring Numlock..."
# Get Numlock current status
grep -q "numlockx on" /etc/lightdm/lightdm.conf && echo $?
  if [ $? = 0 ]; then appicon1=$LITEAPPSICONS/appicon1-on.png; else appicon1=$LITEAPPSICONS/appicon1-off.png; fi
grep -q "numlockx off" /etc/lightdm/lightdm.conf && echo $?
  if [ $? = 0 ]; then appicon2=$LITEAPPSICONS/appicon3-on.png; else appicon2=$LITEAPPSICONS/appicon3-off.png; fi

# Enable/Disable NumLock
EDNL=$(zenity --list --imagelist --width="380" --height="230" --cancel-label="Quit" --ok-label="Apply" --window-icon="$_ICON" \
              --title="$APPNAME" --text='            <span font="Sans Bold 10">Enable or Disable Numlock at login</span>\n' \
              --print-column=2 --column "  " --column "Numlock" --column "Description" \
              $appicon1 "Enable" "Enables Numlock at login" \
              $appicon2 "Disable" "Disables Numlock at login" 2>/dev/null)

if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi
# enable numlock
if [[ "$EDNL" =~ "Enable" ]]; then
  if grep -q "numlockx on" /etc/lightdm/lightdm.conf; then
    echo "#⚫ Numlock is already enabled. No changes needed." && sleep 2
  else
    echo "#⚫ Enabling Numlock at login..." && sleep 1
    sed -i "s/numlockx off/numlockx on/g" /etc/lightdm/lightdm.conf && echo "#✔ Enabling Numlock at login." && sleep 1
  fi
# disable numlock
  elif [[ "$EDNL" =~ "Disable" ]]; then
    if grep -q "numlockx off" /etc/lightdm/lightdm.conf; then
      echo "#⚫ Numlock is already disabled. No changes needed." && sleep 2
    else
      echo "#⚫ Disabling Numlock at login..." && sleep 1
      sed -i "s/numlockx on/numlockx off/g" /etc/lightdm/lightdm.conf && echo "#✔ Disabling Numlock at login." && sleep 1
    fi
else
  zenity --info --width="280" --height="60" --timeout=5 --height="60" \
         --title=" Numlock" --text="\nYou did not select an option. Please try again." 2>/dev/null
fi
done
}
# preload tweak
PRELOAD() {
echo "#⚫ Getting Apps Preload status. Please wait..." && sleep 1
APPNAME="   $_LT - Preload Apps"
SERVICE="preload"
LITEAPPSICONS=/usr/share/liteappsicons/litetweaks
appicon3=/usr/share/liteappsicons/litesoftware/ll-remove-software_32x32.png
# the loop
while (true); do
echo "#Configuring Preload..."
# Is preload installed?
if [ -z  "$(dpkg -l | grep -E '^ii' | grep preload)" ]; then
  # if not installed, then prompt user for installation
  zenity --question --width="330" --height="80" --title="    $APPNAME" \
         --text="\nPreload is currently not installed in your system.\n\nWould you like to install Preload now?" 2>/dev/null
   answer=$?
  # Check Internet access
  if eval "sudo -u ${SUDO_USER:-$_MYLTUSER} curl -sk google.com" >> /dev/null 2>&1; then :; else # Prompt ERROR internet connection
    (zenity --info --width="460" --height="80" --ok-label="Close" --title="$_LT - No Internet connection detected" \
                   --text="\n<b>Your computer doesn't seem to be connected to the Internet</b> \n \n$_LT will install Preload from your local cache if it's available. \
    \nIf the package does not exist in your system cache, Preload installation will fail. \nYou can always check your Internet connection and try again at a later time." 2>/dev/null)
  fi

    if [ "$answer" -eq "0" ]; then
      echo "#⚫ Installing Preload. Please wait..." & apt-get install preload -y # install preload
      if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
        zenity --error --width="260" --height="80" --title="    Error" \
               --text="\nAn error occurred while installing Preload!" 2>/dev/null; return
      else
        echo "#✔ Preload successfully installed." && sleep 1 ; continue
      fi
  else # when user refuses installation, exit out Preload Apps Tweak
    exit 0
  fi
fi
# check if preload service is running and assign variables
if ps ax | grep -v grep | grep $SERVICE > /dev/null; then
  appicon1=$LITEAPPSICONS/appicon1-on.png ; appicon2=$LITEAPPSICONS/appicon3-off.png
else
  appicon1=$LITEAPPSICONS/appicon1-off.png ; appicon2=$LITEAPPSICONS/appicon3-on.png
fi

# Preload Status Main dialog
echo "#Configuring Preload..."
PRLOAD=$(zenity --list --imagelist --width="440" --height=290 --cancel-label="Quit" --ok-label="Change Status" --window-icon="$_ICON" --title="$APPNAME" \
        --text='                           <span font="Sans Bold 11">Preload Apps Status</span>\n\n             • <span font="Sans 9">Preload service is always started during boot up</span>' \
        --print-column=2 --column="  " --column="Preload" --column="Description" \
        $appicon1 "Running" "Preload service is currently running" \
        $appicon2 "Stopped" "Preload service is currently stopped" \
        $appicon3 "Remove" "Stop service and Uninstall Preload" 2>/dev/null)

if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi

  if [[ $PRLOAD =~ Running ]]; then # Start
    if ps ax | grep -v grep | grep $SERVICE > /dev/null; then
      echo "#⚫ Service is already running. No changes needed." && sleep 2 ; continue
    else
      echo "#⚫ Starting Preload..." && sleep 1 && service preload start
      echo "#✔ Preload has been started." && sleep 1 ; continue
    fi
    elif [[ $PRLOAD =~ Stopped ]]; then # Stop
      if ps ax | grep -v grep | grep $SERVICE > /dev/null; then
        echo "#⚫ Stopping Preload..." && sleep 1 && service preload stop
        echo "#✔ Preload has been stopped." && sleep 1
      else
        echo "#⚫ Service is not running. No changes needed." && sleep 2 ; continue
      fi
    elif [[ $PRLOAD =~ Remove ]]; then # Remove Preload
      zenity --question --width="265" --title="$APPNAME" --text="\nAre you sure you want to remove Preload?" 2>/dev/null
      if [ "$?" -eq "0" ]; then
        echo "#⚫ Removing Preload. Please wait..." & service preload stop & apt-get remove preload -y
        if [ "${PIPESTATUS[0]}" -ne "0" ]; then
          echo "#Error..." && sleep 1
          zenity --error --width="260" --height="80" --title="    Error" \
                 --text="\nAn error occurred while removing Preload!" 2>/dev/null; return
        else
          echo "#✔ Preload successfully removed." && sleep 1 ; return
        fi
      fi
    else
      zenity  --info --title=" $APPNAME" --text="\nYou did not select an option. Please try again." 2>/dev/null
  fi
done
}

RCONFIG() {
echo "#Removing residual configuration files..."
dpkg --purge `dpkg -l | grep '^rc' | awk '{print $2}'`
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
    zenity --error --width="260" --height="80" --title="    Error" \
           --text="\nAn error occurred while removing residual configuration files!" 2>/dev/null; return
  fi
echo "#✔ Residual configuration files cleared" && sleep 1
}

REMOVE() {
echo "#Removing unneeded packages, please wait..."
apt-get autoclean -y && apt-get autoremove -y
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "# Error..." && sleep 1
    zenity --error --width="260" --height="80" --title="    Error"
           --text="\nAn error occurred while removing unneeded packages!" 2>/dev/null; return
  fi
_DATE=$(date "+%Y-%m-%d %H")
echo "0" > "/home/$_MYLTUSER/.local/share/.dryapt"
echo "$_DATE" >> "/home/$_MYLTUSER/.local/share/.dryapt"
echo "#✔ Unneeded packages cleared" && sleep 1
}

TLP() {
echo "#⚫ Getting TLP status. Please wait..." && sleep 1
APPNAME="   $_LT - TLP"
LITEAPPSICONS=/usr/share/liteappsicons/litetweaks
appicon3=/usr/share/liteappsicons/litesoftware/ll-remove-software_32x32.png
# the loop
while (true); do
# Is TLP installed?
if [ -z  "$(dpkg -l | grep -E '^ii' | grep tlp)" ]; then
  # if not installed, then prompt user for installation
  zenity --question --width="330" --height="80" --title="    $APPNAME" \
         --text="\nTLP is currently not enabled in your system.\n\nWould you like to enable TLP support now?" 2>/dev/null
         answer=$?
  # Check Internet access
  if eval "curl -sk google.com" >> /dev/null 2>&1; then :; else # Prompt ERROR internet connection
    (zenity --question --icon-name="info" --width="360" --height="90" --ok-label="Continue" --cancel-label="Cancel" --title="$_LT - No Internet connection" \
            --text="\n<b>Your computer is not connected to the internet</b> \n\n* $_LT will attempt to install TLP package from the local cache if available.
\n* If the package does not exist in your system cache, TLP installation will fail.\n\nYou can always check your internet connection and try again at a later time." 2>/dev/null)
    answer=$?
  fi
  if [ "$answer" -eq "0" ]; then echo "#⚫ Installing and Starting the TLP service. Please wait..." && sleep 2
    apt-get install tlp tlp-rdw smartmontools ethtool -y # install TLP
      if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
        zenity --error --width="260" --height="80" --title="    Error" --text="\nAn error occurred while installing TLP!" 2>/dev/null; return
      else
        echo "#✔ TLP successfully installed and started." && sleep 2 && tlp start ; continue
      fi
  else exit 0 # when user refuses installation, exit out TLP Tweak
  fi
fi

# Check if TLP service is running and assign variables
if [ -z  "$(tlp-stat | grep "power save = enabled")" ]; then appicon1=$LITEAPPSICONS/appicon1-on.png ; appicon2=$LITEAPPSICONS/appicon3-off.png
else appicon1=$LITEAPPSICONS/appicon1-off.png ; appicon2=$LITEAPPSICONS/appicon3-on.png
fi

# TLP Status Main dialog
sTLP=$(zenity --list --imagelist --width="365" --height=238 --cancel-label="Quit" --ok-label="Change Status" --window-icon="$_ICON" --title="$APPNAME" \
               --text='                          <span font="Sans Bold 11">      TLP Status</span>\n\n         • <span font="Sans 9">The TLP service is always started during boot up</span>' \
               --print-column=2 --column="  " --column="TLP" --column="Description" \
               $appicon1 "Running" "TLP service is currently running" \
               $appicon3 "Remove" "Uninstall TLP" 2>/dev/null)

if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi
if [[ $sTLP =~ Running ]]; then # Start
  if [ -z  "$(tlp-stat | grep "power save = enabled")" ]; then echo "#⚫ TLP is already running. No changes needed." && sleep 2 ; continue
  else
    echo "#⚫ Starting TLP..." && sleep .5 && tlp start
    echo "#✔ TLP has been started." && sleep 1 ; continue
  fi

elif [[ $sTLP =~ Remove ]]; then # Remove TLP
  zenity --question --width="265" --title="$APPNAME" --text="\nAre you sure you want to remove TLP?" 2>/dev/null
  if [ "$?" -eq "0" ]; then
    echo "#⚫ Removing TLP. Please wait..." && apt-get remove tlp tlp-rdw smartmontools ethtool -y
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
      zenity --error --width="260" --height="80" --title="  Error" --text="\nAn error occurred while removing TLP!" 2>/dev/null; return
    else
      echo "#✔ TLP successfully removed." && sleep 2 ; return
    fi
  fi
else
  zenity  --info --height="40" --title=" $APPNAME" --text="\nYou did not select an option. Please try again."
fi
done
}

ZRAM() {
echo "#⚫ Getting zRAM status. Please wait..." && sleep 1
APPNAME="   $_LT - zRAM"
LITEAPPSICONS=/usr/share/liteappsicons/litetweaks
appicon3=/usr/share/liteappsicons/litesoftware/ll-remove-software_32x32.png
# the loop
while (true); do
echo "#Configuring zRAM..."
# Is zram installed?
if [ -z  "$(dpkg -l | grep -E '^ii' | grep zram-config)" ]; then
  # if not installed, then prompt user for installation
  zenity --question --width="330" --height="80" --title="    $APPNAME" \
         --text="\nzRAM is currently not enabled in your system.\n\nWould you like to enable zRAM support now?" 2>/dev/null
         answer=$?
  # Check Internet access
  if eval "curl -sk google.com" >> /dev/null 2>&1; then :; else # Prompt ERROR internet connection
    (zenity --question --icon-name="info" --width="360" --height="90" --ok-label="Continue" --cancel-label="Cancel" --title="$_LT - No Internet connection" \
            --text="\n<b>Your computer is not connected to the internet</b> \n\n* $_LT will attempt to install zRAM package from the local cache if available.
\n* If the package does not exist in your system cache, zRAM installation will fail.\n\nYou can always check your internet connection and try again at a later time." 2>/dev/null)
    answer=$?
  fi
  if [ "$answer" -eq "0" ]; then echo "#⚫ Installing zRAM. Please wait..." && sleep 1
    apt-get install zram-config -y # install zram
      if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
        zenity --error --width="260" --height="80" --title="    Error" --text="\nAn error occurred while installing zRAM!" 2>/dev/null; return
      else
        echo "#✔ zRAM successfully installed." && sleep 1 & systemctl start zram-config ; continue
      fi
  else exit 0 # when user refuses installation, exit out zRAM Tweak
  fi
fi
# check if zRAM service is running and assign variables
if [ -e /dev/zram0 ]; then appicon1=$LITEAPPSICONS/appicon1-on.png ; appicon2=$LITEAPPSICONS/appicon3-off.png
else appicon1=$LITEAPPSICONS/appicon1-off.png ; appicon2=$LITEAPPSICONS/appicon3-on.png
fi

# zRAM Status Main dialog
echo "#Configuring zRAM..."
sZRAM=$(zenity --list --imagelist --width="400" --height=280 --cancel-label="Quit" --ok-label="Change Status" --window-icon="$_ICON" --title="$APPNAME" \
               --text='                          <span font="Sans Bold 11">      zRAM Status</span>\n\n         • <span font="Sans 9">zRAM service is always started during boot up</span>' \
               --print-column=2 --column="  " --column="zRAM" --column="Description" \
               $appicon1 "Running" "zRAM service is currently running" \
               $appicon2 "Stopped" "zRAM service is currently stopped" \
               $appicon3 "Remove" "Stop service and Uninstall zRAM" 2>/dev/null)

if [ "${PIPESTATUS[0]}" -ne "0" ]; then return; fi
if [[ $sZRAM =~ Running ]]; then # Start
  if [ -e /dev/zram0 ]; then echo "#⚫ zRAM is already running. No changes needed." && sleep 2 ; continue
  else
    echo "#⚫ Starting zRAM..." && sleep .5 && service zram-config start
    echo "#✔ zRAM has been started." && sleep 1 ; continue
  fi
elif [[ $sZRAM =~ Stopped ]]; then # Stop
  if [ -e /dev/zram0 ]; then
    echo "#⚫ Stopping zRAM..." & service zram-config stop
    echo "#✔ zRAM has been stopped." && sleep 2
  else
    echo "#⚫ zRAM is not running. No changes needed." && sleep 2 ; continue
  fi
elif [[ $sZRAM =~ Remove ]]; then # Remove zram
  zenity --question --width="265" --title="$APPNAME" --text="\nAre you sure you want to remove zRAM?" 2>/dev/null
  if [ "$?" -eq "0" ]; then
    echo "#⚫ Removing zRAM. Please wait..." && service zram-config stop && apt-get remove zram-config -y
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#Error..." && sleep 1
      zenity --error --width="260" --height="80" --title="  Error" --text="\nAn error occurred while removing zRAM!" 2>/dev/null; return
    else
      echo "#✔ zRAM successfully removed." && sleep 2 ; return
    fi
  fi
else
  zenity  --info --height="40" --title=" $APPNAME" --text="\nYou did not select an option. Please try again."
fi
done
}

## Arrays execution
_RUN_ICON="/usr/share/icons/Papirus/32x32/apps/litetweaks.png"
x=0
for k in "${!ARRAYC[@]}"; do  x=$(( $x + 1 )); done  # Get the total number of selected items in the array
TOTAL_LINES=$x
printf '%s \n' "${ARRAYC[@]}"|
while read  line
  do
    $line     # Execute functions one by one
    if [ $? = 1 ]; then
      zenity --info --title=" $_LT" --text="Error:\n${line}" 2>/dev/null
    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="  $_LT" --auto-kill --auto-close 2>/dev/null

unset ARAYC
unset FRM
unset okernel
unset PROCEED
unset APPNAME
unset INSTALLER_TITLE
