#!/bin/bash
set -e
#
#
#_______________________________________________
#THIS IS THE Terminal Version of mySystemHealth
#_______________________________________________
# Name   : mySystemHealth
# Licence: GPL3 (http://www.gnu.org/licenses/gpl.html)
# Author : Efatathios Iosifidis
# WebSite: http://iosifidis.gr
# Email  : stathis-(at)-iosifidis.gr
# Date   : 08-08-2023 (first release 17-07-2023)
# Version: 1.0
# System : Fedora Linux and derivatives.
# Description:
# This script will update your system by refreshing the package list, 
# downloading and installing any available updates, and removing any outdated packages and configuration files. 
#
## Script starts here

# Here are the Variables
DATE=$(date +%F_%T)
UCARE_VERSION="Fedora"

# Checks if the user has run the script with "sudo" or not
if [[ $EUID -ne 0 ]] ; then
    clear
    echo ""
    echo "mySystemHealth must be run as root or with sudo. Now I will just exit..." 1>&2
    echo ""
    sleep 2
    exit 1
fi

function WELCOME_SCREEN {
    echo "_______________________________________________________"
    echo "                                                       "
    echo "            mySystemHealth $UCARE_VERSION                "
    echo "            ~~~~~~~~~~~~~~~~~~~~~~~~~~~                "
    echo "                                                       "
    echo " Welcome to System Update and maintenance app.         "
    echo "                                                       "
    echo "                                                       "
    echo " This script will automatically update your system, 	 "
    echo " remove old kernels and obsolete packages,             "
    echo " update flatpacks and free up disk space.   		 "
    echo " It is easy to use and does not require any use        "
    echo " intervention.				         "
    echo "                   				         "
    echo " You can contribute to this script here:	         "
    echo " https://github.com/iosifidis/mySystemHealth	         "
    echo "                   				         "
    echo " If you find this script useful and have saved you time, "
    echo " you can make a donation to the author via PayPal.     "
    echo " The donation link is provided below.		         "
    echo "                                                       "
    echo "           https://www.paypal.me/eiosifidis            "
    echo "_______________________________________________________"
    echo
    echo " mySystemHealth will start in 5 seconds... "

    sleep 6
}

function MAINTENANCE {
    echo
    echo
    echo "#########################"
    echo "        Started"
    echo "#########################"
    echo

    ## Updates package lists
    sudo dnf check-update;
    echo
    echo "###############################"
    echo "Finished updating package lists"
    echo "###############################"
    sleep 1

    ## Updates packages and libraries
    sudo dnf upgrade -y;
    echo
    echo "###############################################"
    echo "Finished updating packages and system libraries"
    echo "###############################################"
    sleep 1
    echo

    ## Removes unneeded packages
    sudo dnf autoremove -y;
    echo
    echo "###################################"
    echo "Finished removing unneeded packages"
    echo "###################################"
    sleep 1
    echo

    ## Removes old kernels
    sudo dnf --setopt=protected_packages= remove -y $(rpm -q kernel-core | grep -v $(uname -r));
    echo
    echo "###################################"
    echo "Finished removing old kernels"
    echo "###################################"
    sleep 1
    echo

    ## Removes unused config files
    sudo dnf remove $(dnf repoquery --installonly --latest-limit=-1 -q) -y;
    echo
    echo "#####################################"
    echo "Finished removing unused config files"
    echo "#####################################"
    sleep 1
    echo

    ## Removes package files that can no longer be downloaded
    sudo dnf clean all;
    echo
    echo "######################################"
    echo "Cleaned downloaded temporary packages"
    echo "######################################"
    echo
    sleep 1

    ## Updates Flatpak packages
    sudo flatpak update -y;
    echo
    echo "######################################"
    echo "Finished updating Flatpak packages"
    echo "######################################"
    echo
    sleep 1

    ## Removes unused Flatpak runtimes and extensions
    sudo flatpak uninstall --unused -y;
    echo
    echo "##########################################################"
    echo "Finished removing unused Flatpak runtimes and extensions"
    echo "##########################################################"
    echo
    sleep 1
    
    ## Clears Flatpak cache
    sudo rm -rf /var/tmp/flatpak-cache*;
    echo
    echo "######################################"
    echo "Finished clearing Flatpak cache"
    echo "######################################"
    echo
    sleep 1

    ## Clears cached data
    free -h && sudo sysctl -w vm.drop_caches=3 && sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches && free -h;
    echo
    echo "###############################################"
    echo "Finished clearing cached data and system memory"
    echo "###############################################"
    echo
    sleep 1

    echo
    echo "#########################################"
    echo " Checking to see if a reboot is required "
    echo "#########################################"
    echo
    ## Check to see if a reboot is required
    if [ -f /var/run/reboot-required ]; then
        echo
        echo "* * * * * * * * * * * * * * * * * *"
        echo "*                                 *"
        echo "* Consider rebooting your system  *"
        echo "* to finish applying updates      *"
        echo "*                                 *"
        echo "* * * * * * * * * * * * * * * * * *"
        echo
    fi
}

function SHOW_HELP {
    cat << EOF

            mySystemHealth
                ~ $UCARE_VERSION ~
         All-in-one system update and maintenance app

    Usage: sudo mysystemhealth <parameter>

    If no parameter is specified, it will just do the regular
    maintenance:

     * Updates the list of available packages
     * Downloads and installs the available updates
     * Checks if there are older Linux kernels on the system and removes
       them. However, it keeps the current and one previous version of
       the kernel.
     * Cleans the cache of the downloaded packages
     * Removes obsolete packages
     * Removes orphan packets
     * Deletes package configuration files from packages that have been
       uninstalled by you.
     * Updates Flatpack.
     * Removes unused Flatpak runtimes and extensions
     * Clears Flatpack cache.

    Parameter:
         -h          display this help and exit

         -r          After completing all the tasks, reboot the system

         -s          After completing all the tasks, shutdown the system

EOF
}

function GOODBYE {
    sleep 2
    echo
    echo
    echo "#########################"
    echo "     All Done... GOODBYE"
    echo "#########################"
}

function SHUT_DOWN {
    sleep 1
    echo
    echo
    echo "#########################"
    echo "      Shutting down"
    echo "#########################"
    sudo shutdown now
}

function RE_BOOT {
    sleep 1
    echo
    echo
    echo "#########################"
    echo "        Rebooting"
    echo "#########################"
    sudo reboot
    ## End of script
}

# The main process starts
while [ "$1" != "" ]; do
    case $1 in
        -h | --help )        SHOW_HELP
                             exit
                                ;;
        -s | --shutdown )    WELCOME_SCREEN && MAINTENANCE && GOODBYE && SHUT_DOWN
                             exit
                                ;;
        -r | --reboot )      WELCOME_SCREEN && MAINTENANCE && GOODBYE && RE_BOOT
                             exit
                                ;;
        * )                  SHOW_HELP
                             exit 1
    esac
done
## If no parameter is given, just do the regular maintenance
WELCOME_SCREEN && MAINTENANCE && GOODBYE

