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

# 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 "AUTOREMOVE" > /dev/null
if [ $? = 0 ];then
ARRAYC+=('REMOVE')
fi

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

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

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

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

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

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

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

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

CLEAN() {
echo "#Cleaning the package cache..."
sleep 2
    sudo apt-get clean
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                        echo "#Error..."
                    sleep 2
                                        zenity --error \
                                        --title="Error" --text="There was an error while\nCleaning the package cache!"
                        fi
}

LARCHIVES() {
echo "#Deleting archived logs..."
sleep 2
    sudo find /var/log \( -name '*.gz' -o -name '*.0' -o -name '*.1' \) -exec rm -rf {} \;
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                        echo "#Error..."
                    sleep 2
                                        zenity --error \
                                        --title="Error" --text="There was an error while\nDeleting archived logs!"
                        fi
}

REMOVE() {
    echo "#Removing unneeded packages, please wait..."
    sleep 2
            sudo apt-get autoremove -y 
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                        echo "#Error..."
                    sleep 2
                                        zenity --error \
                                        --title="Error" --text="There was an error while\nRemoving unneeded packages!"
                        fi
}

HNAME() {
	current_hostname=$(cat /etc/hostname)

	newhost=$(zenity --entry --title="Change Hostname" --text="Your current hostname is:\n\n${current_hostname}\n\nEnter your new hostname or cancel." --entry-text="${current_hostname}")
	
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
	    fi
	
	#If the user entered only blank spaces or starts with a hashtag, abort
	echo "$newhost" | egrep "^[[:space:]]*$|^#" >/dev/null
	if [ $? = 0 ]; then
		zenity --warning \
		--title="Hostname not changed" --text="Hostname can not be empty or start with a hash\nHostname hasn't been changed!"
	 	return
	fi
	
	sed -i "s/$current_hostname/$newhost/g" /etc/hosts
	sed -i "s/$current_hostname/$newhost/g" /etc/hostname

# Reboot dialogue 
# Credit to xunil: http://bbs.archbang.org/viewtopic.php?id=279
title="Hostname changed"
exit_type=`zenity  --width="400" --height="280" --text="New hostname set to:\n\n<b>$newhost</b>\n\nYou MUST reboot to apply the change." --title="$title" --list --radiolist --column="Select" \
    --column="Exit Type" --column="Description" \
    TRUE "Reboot" "Reboot your computer"\
    FALSE "Shutdown" "Shutdown your computer"\
    FALSE "Cancel" "Continue using your computer" `

# User must select a target type (Check if they cancelled)
if [ ! "$exit_type" ]; then
    zenity --error --title="Error" --text="You must make a selection"
		zenity --info \
		--title="Hostname changed" --text="New hostname set to:\n\n<b>$newhost</b>\n\nYou MUST reboot to apply the change."
    exit
fi

# Action to perform by user
if [ "$exit_type" = "Reboot" ]
then

        # Do Reboot here
	sudo shutdown -r now
elif [ "$exit_type" = "Shutdown" ]
then
       # Do Shutdown here
	sudo shutdown -h now 
else

    # Do Cancel here
		zenity --info \
		--title="Hostname changed" --text="New hostname set to:\n\n<b>$newhost</b>\n\nYou MUST reboot to apply the change."
    exit 0
fi
return
}

RCONFIG() {
    echo "#Removing residual configuration files..."
    sleep 1
            sudo dpkg --purge `dpkg -l | grep '^rc' | awk '{print $2}'` 
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                        echo "#Error..."
                    sleep 2
                                        zenity --error \
                                        --title="Error" --text="There was an error while\nRemoving residual configuration files!"
                        fi
return
}

BROWSER() {
echo "#Changing the Default Web Browser..."

file="/etc/gnome/defaults.list"
test_1=(` grep "application/xhtml+xml" $file`)
test_2=(` grep "text/html" $file`)
test_3=(`grep "x-scheme-handler/http" $file`)
test_4=(` grep "x-scheme-handler/https" $file`)

hash firefox 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0"  ]; then
    if [ "${test_1[0]}" = "application/xhtml+xml=firefox.desktop;" ] && [ "${test_2[0]}" = "text/html=firefox.desktop;" ] && [ "${test_3[0]}" = "x-scheme-handler/http=firefox.desktop;" ] && [ "${test_4[0]}" = "x-scheme-handler/https=firefox.desktop;" ]; then
            firefox=("TRUE" "Firefox" "default" "Firefox is currently your default web browser" )
    else
        firefox=("FALSE" "Firefox" "not default" "Set firefox as the default web browser" )
    fi
else 
    firefox=()
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_2[0]}" = "text/html=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" "default" "Chromium is currently your default web browser" )
    else
        chromium=("FALSE" "Chromium" "not default" "Set Chromium as the default web browser" )
    fi
else
    chromium=()
fi

hash midori 2>/dev/null
if [ "${PIPESTATUS[0]}" -eq "0"  ]; then
    if [ "${test_1[0]}" = "application/xhtml+xml=midori.desktop;firefox.desktop;" ] && [ "${test_2[0]}" = "text/html=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" "default" "Midori is currently your default web browser" )
    else
        midori=("FALSE" "Midori" "not default" "Set Midori as the default web browser" )
    fi
else
    midori=()
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_2[0]}" = "text/html=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" "default" "Google Chrome is currently your default web browser" )
    else
        googlechrome=("FALSE" "Google Chrome" "not default" "Set Google Chrome as the default web browser" )
    fi
else
    googlechrome=()
fi

        selection=$(zenity --window-icon="$ic" --list --radiolist --width=650 --height=200 --column="Select" --column="Name" --column="Status" \
        --column="Description" --text="Select the web browser you wish to set as default." --title="Choose default browser" --ok-label="Set" --cancel-label="Cancel" \
        "${firefox[@]}" "${googlechrome[@]}" "${chromium[@]}" "${midori[@]}" )
        
        # "The kill switch"
         if [ "${PIPESTATUS[0]}" -ne "0" ]; then
            return
        fi
    case "$selection" in
    "Firefox") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=firefox.desktop;!g" /etc/gnome/defaults.list `;
           `sed -i "s!^"${test_2[0]}"!text/html=firefox.desktop;!g" /etc/gnome/defaults.list `; \
           `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=firefox.desktop;!g" /etc/gnome/defaults.list `; \
           `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=firefox.desktop;!g" /etc/gnome/defaults.list `;;
    "Google Chrome") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=google-chrome.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `; \
       `sed -i "s!^"${test_2[0]}"!text/html=google-chrome.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `; \
       `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=google-chrome.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `; \
       `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=google-chrome.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `;;
    "Chromium") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=chromium-browser.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `; \
       `sed -i "s!^"${test_2[0]}"!text/html=chromium-browser.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `; \
       `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=chromium-browser.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `; \
       `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=chromium-browser.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `;;
    "Midori") `sed -i "s!^"${test_1[0]}"!application/xhtml+xml=midori.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `; \
       `sed -i "s!^"${test_2[0]}"!text/html=midori.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `; \
       `sed -i "s!^"${test_3[0]}"!x-scheme-handler/http=midori.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `; \
       `sed -i "s!^"${test_4[0]}"!x-scheme-handler/https=midori.desktop;firefox.desktop;!g" /etc/gnome/defaults.list `;;
    esac
unset file
}

FILES() {
echo "#Finding Large Files..."

# Main window dialogue
INSTALLER_TITLE="Find files within a range
---------------------------------------------------------------------------------------------------------------------------
This will find and list all files on your system in a range.

<b>Do not remove a file from your system unless you know what you are doing.
This is not a removal tool, it is provided as information only.</b>

When you click on Yes, this window will close and the process will begin.
---------------------------------------------------------------------------------------------------------------------------
Click on Yes to continue or No to cancel the system scan.\nWould you like to continue?"

# Call the zenity window icon location.
INSTALL_ICON="/usr/share/pixmaps/litecleaner.png"
APPNAME="Find files in a range"

      zenity --question --title="$APPNAME" --window-icon="${INSTALL_ICON}" --text="${INSTALLER_TITLE}"
        if [ "$?" -eq "0" ]; then

# Set the file size range
scale_min=$(zenity --title="Select the minimum file size" --scale --text="Select the minimum file size (in MB)" --min-value=25 --max-value=5000 --value=25 --step=25)

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

size_min=$(echo $scale_min | sed "s/.*/+&M/")

scale_max=$(zenity --title="Select the maximum file size" --scale --text="Select the maximum file size (in MB)\nMinimum file size is $size_min\B" --min-value=25 --max-value=5000 --value=51 --step=25)

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

while [ $scale_max -le $scale_min ]; do
      zenity --info --title="$APPNAME" --window-icon="${INSTALL_ICON}" --text="Maximum value must never be smaller or equal  to minimum value.\nSet it again or click Cancel to exit."
            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)

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

size_max=$(echo $scale_max | sed "s/.*/-&M/")

find_large=$(gksudo "stdbuf -oL /bin/bash \-c '(sudo 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 --title="Please wait whilst your system is scanned..." --pulsate \
--width=600 --auto-close )

            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
              zenity --error \
              --title="Error" --text="$APPNAME has failed."
             return
            fi
          else
            return
          fi

PROCEED=$(zenity --question --title="$APPNAME" --window-icon=/usr/share/icons/zenity-llcc.png --text="Scan complete.\n\nWould you like to view the results?"; echo $?)
if [ ${PROCEED} -eq 1 ]; then
      zenity --info --title="Scan Complete" --window-icon="${INSTALL_ICON}" --text="Click OK to close."
else
    leafpad /var/log/findfilesrange.log &
fi
}

KERNELS() {
# 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|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|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 --title="Results" --text="No additional kernels found"
    return
fi

# sed command adds all FALSE entries in the column 'Select'
zen=(`echo "$OLDKERNELS" | sort | sed -e  's/^/FALSE\n/'`)

# Main window dialogue
DIALOG_TEXT="This will remove kernels you no longer wish to use.

Be sure to select both the header and the image for each version of
the kernel that you wish to remove.

When you click on Remove, this window will close and the process will begin.

Click on Cancel to halt the cleaning process."

# Call the zenity window icon location.
ic="/usr/share/icons/zenity-llcc.png"
APPNAME="Kernel Cleaner"

# Call the dialog. Don't change the separator here!
okernel=$(zenity --window-icon="$ic" --list --checklist --separator=" " --width=500 --height=380   --column="Select" --column="Kernel List" \
--text="${DIALOG_TEXT}" --title="$APPNAME" --ok-label="Remove" --cancel-label="Cancel" "${zen[@]}" ) 

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

# Check if anything is selected
echo $okernel | grep linux*
if [ "${PIPESTATUS[1]}" -ne "0" ]; then
    zenity --info --title='Cleaner' --text='Nothing was selected.'
    return
fi

zenity --question --title="$APPNAME"  --text="Do you want to proceed? You are about to remove:\n$okernel"
 if [ "$?" -eq "0" ]; then
      sudo apt-get purge -y $okernel | zenity --progress --title='Removing Additional Kernels' --text='Removing...' --no-cancel --pulsate --width=400 --auto-close --auto-kill

                        if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                        
                                        zenity --error \
                                        --title="Error" --text="$APPNAME has failed."
                                        return
                                fi

                else

                                        return
                fi

zenity --info --title="$APPNAME" --window-icon="${INSTALL_ICON}" --text="$APPNAME Complete."
return

}

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
2.0)
title="Linux Lite 2.0"
black="0x000000"
white="0xffffff"
brown="0xffff00"
blue="0x000000"
;;
2.2)
title="Linux Lite 2.2"
black="0x000000"
white="0xffffff"
brown="0xffff00"
blue="0x000000"
;;
2.4)
title="Linux Lite 2.4"
black="0x000000"
white="0xffffff"
brown="0xffff00"
blue="0x000000"
;;
2.6)
title="Linux Lite 2.6"
black="0x000000"
white="0xffffff"
brown="0xffff00"
blue="0x000000"
;;
2.8)
title="Linux Lite 2.8"
black="0x000000"
white="0xffffff"
brown="0xffff00"
blue="0x000000"
;;
Unknown)
zenity --info --title="Bootup fix" --text="Unknown OS"
return
;;
*)
zenity --info --title="Lite Tweaks" --text="Bootup fix\nRelease not supported!"
return
;;
esac

sudo sed -i "s/^title=.*$/title=$title/;s/^black=.*$/black=$black/;s/white=.*$/white=$white/;s/^brown=.*$/brown=$brown/;s/^blue=.*$/blue=$blue/" /lib/plymouth/themes/ubuntu-text/ubuntu-text.plymouth
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                        echo "#Error"
                    sleep 1
                                        zenity --error \
                                        --title="Error" --text="There was an error while\nFixing the bootup!\nUnable to write to /etc/lsb-release"
                    return
                        fi

 sudo update-initramfs -u
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                        echo "#Error"
                    sleep 1
                                        zenity --error \
                                        --title="Error" --text="There was an error while\nFixing the bootup!\nStatus: update-initramfs fail."
                    return
                        fi

echo "Linux Lite $LL_VERSION LTS \n \l" | sudo tee /etc/issue
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                        echo "#Error"
                    sleep 1
                                        zenity --error \
                                        --title="Error" --text="There was an error while\nFixing the bootup!\nUnable to write to /etc/issue"
                    return
                        fi

sudo 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 \
                                        --title="Error" --text="There was an error while\nFixing the bootup!\nUnable to write to /etc/lsb-release"
                    return
                        fi
}

FIXAPT() {
    echo "#Stage 1: Repairing the package system."$"\nPlease wait, this might take some time..."
sudo apt-get clean
cd /var/lib/apt
sudo mv lists lists.old
sudo mkdir -p lists/partial
sudo apt-get clean
sudo apt-get update
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                        echo "#Error"
                    sleep 2
                                        zenity --error \
                                        --title="Error" --text="There was an error while\nFetching package lists!"
                    return
                        fi
    echo "#Stage 2: Repairing the package system."$"\nPlease wait, this might take some time..."

sudo apt-get install -f
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                        echo "#Error"
                    sleep 2
                                        zenity --error \
                                        --title="Error" --text="There was an error while\nInstalling partialy installed packages!"
                    return
                        fi
    echo "#Stage 3: Repairing the package system."$"\nPlease wait, this might take some time..."

# Check for /etc/apt/sources.list.save file.If the file is not present then run...
if [ !  -f /etc/apt/sources.list.save ]; then
    sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
    sudo touch /etc/apt/sources.list
    sudo add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
fi

sudo rm /etc/apt/sources.list
sudo cp /etc/apt/sources.list.save /etc/apt/sources.list
sudo apt-get update

    echo "#Final Stage: Repairing the package system."$"\nCompleting Repair..."

sudo dpkg --configure -a
}

    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="Lite Tweak" --text="Error:\n${line}"
            fi    
        let i++
         (( PERCENTAGE = 100 \* ${i} / ${TOTAL_LINES} ))
         echo "$PERCENTAGE"

        if [ "$PERCENTAGE" == "100" ]; then
            echo "#Done"
            sleep 2
        fi
    
    done| zenity --progress  --title="Lite Tweaks" --auto-kill --auto-close --width=300 --no-cancel

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