#!/bin/bash
#-----------------------------------------------------------------------------------------
# Name: Linux Lite Updates
# Description: A GUI tool to easily install Updates in Linux Lite.
# Authors: Misko_2083, Jerry Bezencon, gerito1
# Website: https://www.linuxliteos.com
#-----------------------------------------------------------------------------------------

# Kill off any package managers that may be running
if [ "$(pidof synaptic)" ] 
then
   sudo killall -9 synaptic
fi

if [ ! -z "$(pgrep gdebi-gtk)" ]
then
   killall -9 gdebi-gtk
fi

# Updates log variable
UPDATES=$(mktemp /tmp/updateslist.XXXXXX)

# Linux Lite default dialog icon
ic="/usr/share/icons/zenity-llcc.png"

# Get list of available updated packages then populate them to /tmp/updateslist
zenity --question --title="Linux Lite Updates" --window-icon="/usr/share/icons/zenity-llcc.png" --text="We will now fetch the Updates list.\n\nClick Yes to continue or No to abort."
   if [ "$?" -eq "0" ];then

    APTUPDATE=$(grep '^deb' -c /etc/apt/sources.list) # Total of repositories registered, this is approximated
    sudo apt-get update 2>&1 | 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 --text="Updating package lists..." --window-icon=$ic --title="Updating Software Sources - please wait..." --percentage=0 --auto-close --width=600

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

                                        unset APTUPDATE
                                        rm $UPDATES
                                        unset UPDATES
                                        zenity --error \
                                        --title="Error" --text="$APPNAME couldn't fetch the package cache information lists."

                                        exit 1
                                fi
unset APTUPDATE

# Creates a list in /tmp/updateslist
apt-get --just-print upgrade 2>&1 | perl -ne 'if (/Inst\s([\w,\-,\d,\.,~,:,\+]+)\s\[([\w,\-,\d,\.,~,:,\+]+)\]\s\(([\w,\-,\d,\.,~,:,\+]+)\)? /i) {print "Name: $1 INSTALLED: $2 AVAILABLE: $3\n"}' | awk '{print NR,":\t"$0}' | tee $UPDATES

# Check if any updates are available, if there are none, script pops up dialog box saying 'No Updates Available', removes /tmp/updateslist.XXXXXX
if [  -z "$(cat $UPDATES)"  ]; then

   rm $UPDATES
   unset UPDATES
    zenity --info --window-icon="/usr/share/icons/zenity-llcc.png" --title="Linux Lite Updates" \
      --text="No Updates Available"

   exit 0
fi

# Insert text into  /tmp/updateslist.XXXXXX
sed -i -e '1 i\List of available Updates' -e '1 i\Click Update to continue or Cancel to stop the update process\n'  $UPDATES

# Erase existing available info
sudo dpkg --clear-avail

  else
       rm $UPDATES
       unset UPDATES
       exit 0
  fi

# Call the zenity dialog to show update list
zenity --text-info --ok-label="Update" --cancel-label="Cancel" --title="Linux Lite Updates" --width=780 --height=300 --filename="$UPDATES"
      if [ "$?" -eq "0" ];then

      # Continue script if no halt
        INSTALL_ICON="/usr/share/icons/zenity-llcc.png"
        APPNAME="Linux Lite Updates"

                 # Remove tmp file and unset variable
                 rm $UPDATES
                 unset UPDATES
#Begin upgrade
sudo DEBIAN_FRONTEND=noninteractive apt-get  -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade -y  2>&1 | tee /var/log/llupdates.log | awk ' BEGIN { FS=" "; total=1;end_download=0} /upgraded/ {total= $1 + $3;FS="[ :]" } /^Get:[[:digit:]]+/ {printf "#Downloading %s %s %s\n",$6,$(NF-1),$NF;print int(($2 - 1) * 100 / total); fflush(stdout)} /^\(Reading / {if (end_download==0){print 100;fflush(stdout);end_download=1}} /^(Preparing|Unpacking|Selecting|Processing|Setting|Download)/ {print "#", substr($0, 0, 128); fflush(stdout)}' \
    | ( zenity --window-icon=$ic --progress --width=600 --text="Downloading package(s)...\nThis may take a while." --title="Downloading - please wait..." --percentage=0 --auto-close ; zenity --progress --window-icon=$ic --width=600 --text="Installing and configuring packages...\nThis may take a while." --title="Installing - please wait..."  --pulsate --auto-close )

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

                                        zenity --error \
                                        --title="Error" --text="$APPNAME have failed.\n\nRefer to /var/log/llupdates.log\n\nfor more information."
                                        exit 0
                                fi

      # Halt updates script if user selects Cancel
       else
         rm $UPDATES
        unset UPDATES
   zenity --error --title="Updates Cancelled" --window-icon="${INSTALL_ICON}" --text="Updates Cancelled."
         exit 0
    fi

PROCEED=$(zenity --question --title="$APPNAME" --window-icon=/usr/share/icons/zenity-llcc.png --text="Updates have finished installing.\n\nWould you like to view the $APPNAME log?"; echo $?)
if [ ${PROCEED} -eq 1 ]; then
   zenity --info --title="$APPNAME" --window-icon="${INSTALL_ICON}" --text="$APPNAME Complete."
   exit;
else
   zenity --text-info --ok-label="Quit" --cancel-label="Cancel" --title="Updates Log" --width=700 --height=300 --filename="/var/log/llupdates.log"
fi

exit 0
