#!/bin/bash
#----------------------------------------------------------------
# Description: Linux Lite Upgrade Script - System
# About: For Linux Lite 4.- Series only
# Licence: GPLv2 Sharing is Caring
# Author: Jerry Bezencon, Ralphy
# Credits: Misko_2083
# Website: https://www.linuxliteos.com
#----------------------------------------------------------------

# Ensure multi-language support
export LANG=C

# Kill off any package managers that may be running
if [ "$(pidof synaptic)" ]; then killall -9 synaptic; else :; fi
if [ -z "$(pgrep gdebi-gtk)" ]; then :; else killall -9 gdebi-gtk; fi

# Keep it clean to reduce errors
apt-get clean all 2>&1 | tee -a $LOGFILE

LL40SYSTEM() {
# Invoke arrays
ARRAYB=()
ARRAYC=()

#Check the arch
arch=$(uname -m)

case "$arch" in
    x86)    arch="x86"  ;;
    i?86)   arch="x86"  ;;
    amd64)  arch="amd64"    ;;
    x86_64) arch="amd64"    ;;
    * ) echo "Your Arch '$arch' -> Its not supported."  ;;
esac

# Populate arrays based on the arch
case "$arch" in
    x86)
    ARRAYB+=('BUPETCSKEL40')
    ARRAYC+=('ADDWALLS')
    ARRAYC+=('UPDPKGS')
    ARRAYC+=('ERASEDPKGS')
    ARRAYC+=('INPKCL')
    ARRAYC+=('UPLSBISSUE')
    ARRAYC+=('UPDGRUB')
    ARRAYC+=('UPDPLYM')
    ;;
    amd64)
    ARRAYB+=('BUPETCSKEL40')
    ARRAYC+=('ADDWALLS')
    ARRAYC+=('UPDPKGS')
    ARRAYC+=('ERASEDPKGS')
    ARRAYC+=('INPKCL')
    ARRAYC+=('UPLSBISSUE')
    ARRAYC+=('UPDGRUB')
    ARRAYC+=('UPDPLYM')
    ;;
esac
}

LL42SYSTEM() {
# Invoke arrays
ARRAYB=()
ARRAYC=()

#Check the arch
arch=$(uname -m)

case "$arch" in
    x86)    arch="x86"  ;;
    i?86)   arch="x86"  ;;
    amd64)  arch="amd64"    ;;
    x86_64) arch="amd64"    ;;
    * ) echo "Your Arch '$arch' -> Its not supported."  ;;
esac

# Populate arrays based on the arch
case "$arch" in
    x86)
    ARRAYB+=('BUPETCSKEL42')
    ARRAYC+=('ADDWALLS')
    ARRAYC+=('UPDPKGS')
    ARRAYC+=('ERASEDPKGS')
    ARRAYC+=('INPKCL')
    ARRAYC+=('UPLSBISSUE')
    ARRAYC+=('UPDGRUB')
    ARRAYC+=('UPDPLYM')
    ;;
    amd64)
    ARRAYB+=('BUPETCSKEL42')
    ARRAYC+=('ADDWALLS')
    ARRAYC+=('UPDPKGS')
    ARRAYC+=('ERASEDPKGS')
    ARRAYC+=('INPKCL')
    ARRAYC+=('UPLSBISSUE')
    ARRAYC+=('UPDGRUB')
    ARRAYC+=('UPDPLYM')
    ;;
esac
}

LL44SYSTEM() {
# Invoke arrays
ARRAYB=()
ARRAYC=()

#Check the arch
arch=$(uname -m)

case "$arch" in
    x86)    arch="x86"  ;;
    i?86)   arch="x86"  ;;
    amd64)  arch="amd64"    ;;
    x86_64) arch="amd64"    ;;
    * ) echo "Your Arch '$arch' -> Its not supported."  ;;
esac

# Populate arrays based on the arch
case "$arch" in
    x86)
    ARRAYB+=('BUPETCSKEL44')
    ARRAYC+=('ADDWALLS')
    ARRAYC+=('UPDPKGS')
    ARRAYC+=('ERASEDPKGS')
    ARRAYC+=('INPKCL')
    ARRAYC+=('UPLSBISSUE')
    ARRAYC+=('UPDGRUB')
    ARRAYC+=('UPDPLYM')
    ;;
    amd64)
    ARRAYB+=('BUPETCSKEL44')
    ARRAYC+=('ADDWALLS')
    ARRAYC+=('UPDPKGS')
    ARRAYC+=('ERASEDPKGS')
    ARRAYC+=('INPKCL')
    ARRAYC+=('UPLSBISSUE')
    ARRAYC+=('UPDGRUB')
    ARRAYC+=('UPDPLYM')
    ;;
esac
}

LL46SYSTEM() {
# Invoke arrays
ARRAYB=()
ARRAYC=()

#Check the arch
arch=$(uname -m)

case "$arch" in
    x86)    arch="x86"  ;;
    i?86)   arch="x86"  ;;
    amd64)  arch="amd64"    ;;
    x86_64) arch="amd64"    ;;
    * ) echo "Your Arch '$arch' -> Its not supported."  ;;
esac

# Populate arrays based on the arch
case "$arch" in
    x86)
    ARRAYB+=('BUPETCSKEL46')
    ARRAYC+=('ADDWALLS')
    ARRAYC+=('UPDPKGS')
    ARRAYC+=('ERASEDPKGS')
    ARRAYC+=('INPKCL')
    ARRAYC+=('UPLSBISSUE')
    ARRAYC+=('UPDGRUB')
    ARRAYC+=('UPDPLYM')
    ;;
    amd64)
    ARRAYB+=('BUPETCSKEL46')
    ARRAYC+=('ADDWALLS')
    ARRAYC+=('UPDPKGS')
    ARRAYC+=('ERASEDPKGS')
    ARRAYC+=('INPKCL')
    ARRAYC+=('UPLSBISSUE')
    ARRAYC+=('UPDGRUB')
    ARRAYC+=('UPDPLYM')
    ;;
esac
}

# LOCAL BACK UPS

# Backup /etc/skel/ Linux Lite 4.0
BUPETCSKEL40() {
  log_upgrade "BUPETCSKEL40"
  echo "#⚫ Backing up /etc/skel files..." && sleep 2
  mkdir -p /etc/skel/.llupgradebackup/4.0 2>&1 | tee -a $LOGFILE
}

# Backup /etc/skel/ Linux Lite 4.2
BUPETCSKEL42() {
  log_upgrade "BUPETCSKEL42"
  echo "#⚫ Backing up /etc/skel files..." && sleep 2
  mkdir -p /etc/skel/.llupgradebackup/4.2 2>&1 | tee -a $LOGFILE
}

# Backup /etc/skel/ Linux Lite 4.4
BUPETCSKEL44() {
  log_upgrade "BUPETCSKEL44"
  echo "#⚫ Backing up /etc/skel files..." && sleep 2
  mkdir -p /etc/skel/.llupgradebackup/4.4 2>&1 | tee -a $LOGFILE
}

# Backup /etc/skel/ Linux Lite 4.6
BUPETCSKEL46() {
  log_upgrade "BUPETCSKEL46"
  echo "#⚫ Backing up /etc/skel files..." && sleep 2
  mkdir -p /etc/skel/.llupgradebackup/4.6 2>&1 | tee -a $LOGFILE
}

# SYSTEM MODIFICATIONS

# Add new wallpapers
ADDWALLS() {
  log_upgrade "ADDWALLS"
  UPDATER=$( stdbuf -oL /bin/bash \-c '(cd /tmp && wget http://repo.linuxliteos.com/upgrade/4.0-4.8/walls48.tar.gz &&  tar -xvf walls48.tar.gz -C /usr/share/backgrounds/xfce/ && cd /usr/share/backgrounds/xfce/ && chown root:root * && chmod 644 * && sleep 2 )' 2>&1 | tee -a $LOGFILE |
  stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |zenity --progress --width=600 --auto-close --no-cancel --percentage="$PERCENTAGE"  --title="Adding new wallpapers...")
unset UPDATER
}

# Get list of available updated packages
UPDPKGS() {
  log_upgrade "UPDPKGS"
  UPDATER=$( stdbuf -oL /bin/bash \-c '( apt-get update \-y )' 2>&1 | tee -a $LOGFILE | stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |
  zenity --progress --width=600 --auto-close --no-cancel --percentage="$PERCENTAGE" --title="Updating repositories..." --text="Updating repositories...")
  if [ $? = 1 ]; then zenity --info --width="300" --title="$APPNAME" --text="\nError:\n${line}"; fi
unset UPDATER
}

# Erase existing available info
ERASEDPKGS() {
  log_upgrade "ERASEDPKGS"
  UPDATER=$( stdbuf -oL /bin/bash \-c '( dpkg --clear-avail )' 2>&1 | tee -a $LOGFILE | stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |
  zenity --progress --width=600 --auto-close --no-cancel --percentage="$PERCENTAGE"  --title="Clearing dpkg..." --text="Clearing dpkg...")
  if [ $? = 1 ]; then zenity --info --width="300" --title="$APPNAME" --text="\nError:\n${line}"; fi
unset UPDATER
}

# Install new packages, remove obsolete packages, clean up
INPKCL() {
  log_upgrade "INPKCL"
  UPDATER=$( stdbuf -oL /bin/bash \-c '(DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \-y && apt-get install xfce4-cpufreq-plugin gnome-font-viewer \-y && apt-get autoremove \-y && apt-get purge --auto-remove upstart \-y && apt-get clean )' 2>&1 | tee -a $LOGFILE |
  stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |
  zenity --progress --width=600 --auto-close --no-cancel --percentage="$PERCENTAGE"  --title="Downloading and Installing new packages..." --text="Downloading and Installing new packages...")
unset UPDATER
}

# Update lsb_release and issue files /etc changes, update /etc/llver
UPLSBISSUE() {
  log_upgrade "UPLSBISSUE"
  UPDATER=$( stdbuf -oL /bin/bash \-c '( sed -i "s/DISTRIB_DESCRIPTION=.*/DISTRIB_DESCRIPTION=\"Linux Lite "'$UPGR'"\"/g" /etc/lsb-release && echo "Linux Lite $UPGR LTS \n \l" | tee /etc/issue && echo "Linux Lite $UPGR" | tee /etc/llver && sleep 2 )' 2>&1 | tee -a $LOGFILE | stdbuf -oL sed -n -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |
  zenity --progress --width=600 --auto-close --no-cancel --percentage="$PERCENTAGE"  --title="Updating version information...")
  if [ $? = 1 ]; then zenity --info --width="300" --title="$APPNAME" --text="\nError:\n${line}"; fi
unset UPDATER
}

# Update grub
UPDGRUB() {
  log_upgrade "UPDGRUB"
  UPDATER=$( stdbuf -oL /bin/bash \-c '( update-grub && sleep 2 )' 2>&1 | tee -a $LOGFILE | stdbuf -oL sed -n -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |
  zenity --progress --width=600 --auto-close --no-cancel --percentage="$PERCENTAGE" --title="Updating Grub..." --text="Updating Grub...")
  if [ $? = 1 ]; then zenity --info --width="300" --title="$APPNAME" --text="\nError:\n${line}"; fi
unset UPDATER
}

# Update /usr/share/plymouth/themes/ubuntu-text/ubuntu-text.plymouth
UPDPLYM() {
  log_upgrade "UPDPLYM"
  UPDATER=$( stdbuf -oL /bin/bash \-c '( sed -i "s/^title=Linux Lite.*$/title=Linux Lite "'$UPGR'"/g" /usr/share/plymouth/themes/ubuntu-text/ubuntu-text.plymouth ;
  sed -i "s/^title=Ubuntu.*$/title=Linux Lite "'$UPGR'"/g" /usr/share/plymouth/themes/ubuntu-text/ubuntu-text.plymouth ;
  sed -i "s/black=0x2c001e/black=0x000000/g" /usr/share/plymouth/themes/ubuntu-text/ubuntu-text.plymouth ;
  sed -i "s/white=0xffffff/white=0xffffff/g" /usr/share/plymouth/themes/ubuntu-text/ubuntu-text.plymouth ;
  sed -i "s/brown=0xff4012/brown=0xffe082/g" /usr/share/plymouth/themes/ubuntu-text/ubuntu-text.plymouth ;
  sed -i "s/blue=0x988592/blue=0x000000/g" /usr/share/plymouth/themes/ubuntu-text/ubuntu-text.plymouth && update-initramfs -u && sleep 2 )' 2>&1 | tee -a $LOGFILE |
  stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |
  zenity --progress --width=600 --auto-close --no-cancel --percentage="$PERCENTAGE" --title="Updating boot configuration please wait...")
  if [ $? = 1 ]; then zenity --info --width="300" --title="$APPNAME" --text="\nError:\n${line}"; fi
unset UPDATER
return
}

#Check the version and start  the appropriate function
LLVER=$(awk '{print $3}' /etc/llver)

  if [ "$LLVER" == "4.0" ]; then LL40SYSTEM
elif [ "$LLVER" == "4.2" ]; then LL42SYSTEM
elif [ "$LLVER" == "4.4" ]; then LL44SYSTEM
elif [ "$LLVER" == "4.6" ]; then LL46SYSTEM
elif [ "$LLVER" == "4.8" ]; then zenity --info --width="300" --title="$_APPNAME" --text="\nYou are already running the latest release."; exit 0
  fi

# Check if ARRAYB is empty
# If it is skip the execution
if [ ${#ARRAYB[@]} -ne 0 ]; then
    x=0
    for k in "${!ARRAYB[@]}"; do  x=$(( $x + 1 )); done  # Get the total number of selected items in array B
    TOTAL_LINES=$x
    i=0
    printf '%s \n' "${ARRAYB[@]}"|
    while read  line; do
      $line     # Execute functions one by one
        if [ $? = 1 ]; then log_upgrade "Error - $line"; zenity --error --width="300" --title="$APPNAME" --text="An error occurred while performing upgrades:\n ${line}"; exit 1 ; fi
        let i++
          (( PERCENTAGE = 100 \* ${i} / ${TOTAL_LINES} ))
        echo "$PERCENTAGE"
        if [ "$PERCENTAGE" == "100" ]; then echo "#⚫ Completed." && sleep 2; fi
    done| zenity --progress  --title="$APPNAME" --auto-kill --auto-close --no-cancel --width=400
fi

# Check if ARRAYC is empty
# If it is skip the execution
if [ ${#ARRAYC[@]} -ne 0 ]; then
    x=0
    for k in "${!ARRAYC[@]}"; do  x=$(( $x + 1 )); done  # Get the total number of selected items in the array
    i=0
    TOTAL_LINES=$x
    PERCENTAGE=0
    printf '%s \n' "${ARRAYC[@]}"|
    while read  line; do
        (( PERCENTAGE = 100 \* ${i} / ${TOTAL_LINES} ))
        $line     # Execute functions one by one
        if [ $? = 1 ]; then log_upgrade "Error - $line"; zenity --error --width="300" --title="$APPNAME" --text="An error occurred while performing upgrades:\n ${line}"; fi
        let i++
    if [ "$PERCENTAGE" == "100" ]; then echo "#⚫ Completed." && sleep 2 ; fi
    done
fi

unset ARRAYB
unset ARRAYC
unset MYUSER
