#!/bin/bash
#----------------------------------------------------------------
# Description: Linux Lite Upgrade Script - Home
# About: For Linux Lite 3.- 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

# Release Upgrade
UPGR="3.8"
UPGRFULL="Linux Lite 3.8"

# Get current Linux Lite version
GETLLVER=$(awk '{print}' /etc/llver)

# Admin user variable
MYUSER=$(sudo -u ${SUDO_USER:-$USER} whoami)

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

# Application title
APPNAME="Linux Lite Upgrade Utility"

# Log file variables
LOGFILE=/tmp/lite-upgrade.log
REPORTERROR="This error has been logged to $LOGFILE."
# delete previous lite-upgrade.log file & create a new one
if [ -f "$LOGFILE" ]; then chown $MYUSER:$MYUSER $LOGFILE; fi
# log function
log_upgrade(){
    message="$@"
    echo '['$(date +%D\ %H:%M:%S)'] '$message >>$LOGFILE
}
> $LOGFILE
log_upgrade "---------- $APPNAME ----------"

# Functions

# Create backup directories for Linux Lite 3.0
MKDIRBUPS30() {
  log_upgrade "MKDIRBUPS30"
  echo "#⚫ Creating backup directories..." && sleep 2
  (mkdir -p /home/$MYUSER/.llupgradebackup/3.0 ; chown -R $MYUSER:$MYUSER /home/$MYUSER/.llupgradebackup) 2>&1 | tee -a $LOGFILE
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2;
    zenity --error --width="320" --title="$APPNAME - Error" --text="An error occurred while creating backup directories.\n\n$REPORTERROR\nUpgrade canceled."; exit 1
  fi
}

# Backup /home/$USER/ selected config files on Linux Lite 3.0
BUPHOMEUSER30() {
  log_upgrade "BUPHOMEUSER30"
  echo "#⚫ Backing up selected home files..." && sleep 2
  (chmod 755 -R /home/$MYUSER/.config/ 2>&1 | tee -a $LOGFILE; # ~/.config perms fix
  chmod 755 /home/$MYUSER/.config/htop/ 2>&1 | tee -a $LOGFILE;
  chmod 755 /home/$MYUSER/.config/lxterminal/ 2>&1 | tee -a $LOGFILE;
  cp /home/$MYUSER/.config/lxterminal/lxterminal.conf /home/$MYUSER/.llupgradebackup/3.0 ; chown -R $MYUSER:$MYUSER /home/$MYUSER/.llupgradebackup) 2>&1 | tee -a $LOGFILE
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2;
    zenity --error --width="320" --ok-label="Continue" --title="$APPNAME - Error" --text="An error occurred while backing up selected files in the home directory.\n\n$REPORTERROR"
  fi
}

# Create backup directories for Linux Lite 3.2
MKDIRBUPS32() {
  log_upgrade "MKDIRBUPS32"
  echo "#⚫ Creating backup directories..." && sleep 2
  (mkdir -p /home/$MYUSER/.llupgradebackup/3.2 ; chown -R $MYUSER:$MYUSER /home/$MYUSER/.llupgradebackup) 2>&1 | tee -a $LOGFILE
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2;
    zenity --error --width="320" --title="$APPNAME - Error" --text="An error occurred while creating backup directories.\n\n$REPORTERROR\nUpgrade canceled."; exit 1
  fi
}

# Backup /home/$USER/ select config files on Linux Lite 3.2
BUPHOMEUSER32() {
  log_upgrade "BUPHOMEUSER32"
  echo "#⚫ Backing up selected home files..." && sleep 2
  (chmod 755 -R /home/$MYUSER/.config/ 2>&1 | tee -a $LOGFILE; # ~/.config perms fix
  chmod 755 /home/$MYUSER/.config/htop/ 2>&1 | tee -a $LOGFILE;
  chmod 755 /home/$MYUSER/.config/lxterminal/ 2>&1 | tee -a $LOGFILE;
  cp /home/$MYUSER/.config/htop/htoprc /home/$MYUSER/.llupgradebackup/3.2 ; cp /home/$MYUSER/.config/lxterminal/lxterminal.conf /home/$MYUSER/.llupgradebackup/3.2 ; cp /home/$MYUSER/.conky/widget /home/$MYUSER/.llupgradebackup/3.2 ; chown -R $MYUSER:$MYUSER /home/$MYUSER/.llupgradebackup) 2>&1 | tee -a $LOGFILE
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2;
    zenity --error --width="320" --ok-label="Continue" --title="$APPNAME - Error" --text="An error occurred while backing up selected files in the home directory.\n\n$REPORTERROR"
  fi
}

# Create backup directories for Linux Lite 3.4
MKDIRBUPS34() {
  log_upgrade "MKDIRBUPS34"
  echo "#⚫ Creating backup directories..." && sleep 2
  (mkdir -p /home/$MYUSER/.llupgradebackup/3.4 ; chown -R $MYUSER:$MYUSER /home/$MYUSER/.llupgradebackup) 2>&1 | tee -a $LOGFILE
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2;
    zenity --error --width="320" --title="$APPNAME - Error" --text="An error occurred while creating backup directories.\n\n$REPORTERROR\nUpgrade canceled."; exit 1
  fi
}

# Backup /home/$USER/ select config files on Linux Lite 3.4
BUPHOMEUSER34() {
  log_upgrade "BUPHOMEUSER34"
  echo "#⚫ Backing up selected home files..." && sleep 2
  (chmod 755 -R /home/$MYUSER/.config/ 2>&1 | tee -a $LOGFILE; # ~/.config perms fix
  chmod 755 /home/$MYUSER/.config/htop/ 2>&1 | tee -a $LOGFILE;
  chmod 755 /home/$MYUSER/.config/lxterminal/ 2>&1 | tee -a $LOGFILE;
  cp /home/$MYUSER/.config/htop/htoprc /home/$MYUSER/.llupgradebackup/3.4 ; cp /home/$MYUSER/.config/lxterminal/lxterminal.conf /home/$MYUSER/.llupgradebackup/3.4 ; cp /home/$MYUSER/.conky/widget /home/$MYUSER/.llupgradebackup/3.4 ; chown -R $MYUSER:$MYUSER /home/$MYUSER/.llupgradebackup) 2>&1 | tee -a $LOGFILE
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2;
    zenity --error --width="320" --ok-label="Continue" --title="$APPNAME - Error" --text="An error occurred while backing up selected files in the home directory.\n\n$REPORTERROR"
  fi
}

# Create backup directories for Linux Lite 3.6
MKDIRBUPS36() {
  log_upgrade "MKDIRBUPS36"
  echo "#⚫ Creating backup directories..." && sleep 2
  (mkdir -p /home/$MYUSER/.llupgradebackup/3.6 ; chown -R $MYUSER:$MYUSER /home/$MYUSER/.llupgradebackup) 2>&1 | tee -a $LOGFILE
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2;
    zenity --error --width="320" --title="$APPNAME - Error" --text="An error occurred while creating backup directories.\n\n$REPORTERROR\nUpgrade canceled."; exit 1
  fi
}

# Backup /home/$USER/ select config files on Linux Lite 3.6
BUPHOMEUSER36() {
  log_upgrade "BUPHOMEUSER36"
  echo "#⚫ Backing up selected home files..." && sleep 2
  (chmod 755 -R /home/$MYUSER/.config/ 2>&1 | tee -a $LOGFILE; # ~/.config perms fix
  chmod 755 /home/$MYUSER/.config/htop/ 2>&1 | tee -a $LOGFILE;
  chmod 755 /home/$MYUSER/.config/lxterminal/ 2>&1 | tee -a $LOGFILE;
  cp /home/$MYUSER/.config/htop/htoprc /home/$MYUSER/.llupgradebackup/3.6 ; cp /home/$MYUSER/.config/lxterminal/lxterminal.conf /home/$MYUSER/.llupgradebackup/3.6 ; cp /home/$MYUSER/.conky/widget /home/$MYUSER/.llupgradebackup/3.6 ; chown -R $MYUSER:$MYUSER /home/$MYUSER/.llupgradebackup) 2>&1 | tee -a $LOGFILE
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2;
    zenity --error --width="320" --ok-label="Continue" --title="$APPNAME - Error" --text="An error occurred while backing up selected files in the home directory.\n\n$REPORTERROR"
  fi
}

# Add Lite Desktop Widget
ADDLITEWIDG() {
  log_upgrade "ADDLITEWIDG"
  UPDATER=$( stdbuf -oL /bin/bash \-c '(cd /home/"'$MYUSER'" && wget http://repo.linuxliteos.com/upgrade/3.0-3.2/liteconkywidget.tar.gz && tar xvzf liteconkywidget.tar.gz && rm liteconkywidget.tar.gz && sleep 2 )' 2>&1 | tee -a $LOGFILE |
  stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |
  zenity --progress --width=400 --percentage="$PERCENTAGE" --title="Adding Lite Desktop Widget..." --auto-close --no-cancel --text=" ")
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2 ; log_upgrade "ADDLITEWIDG - Error"
    zenity --error --width="340" --title="$APPNAME - Error" --title="Error" --text="An error occurred while adding the Lite Desktop Widget.\n\n$REPORTERROR"
  fi
unset UPDATER
}

# Improvements to the Lite Desktop Widget
UPDLITEWIDG() {
  log_upgrade "UPDLITEWIDG"
  UPDATER=$( stdbuf -oL /bin/bash \-c '(sed -i "s/Logged in as.*/${color}Logged in as:   \${alignr}\${color}\$USER"/g /home/"'$MYUSER'"/.conky/widget && sleep 2 )' 2>&1 |
  stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |
  zenity --progress --width=400 --percentage="$PERCENTAGE" --title="Updating the Lite Desktop Widget..." --auto-close --no-cancel --text=" ")
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2 ; log_upgrade "UPDLITEWIDG - Error"
    zenity --error --width="340" --title="$APPNAME - Error" --title="Error" --text="An error occurred while updating the Lite Desktop Widget.\n\n$REPORTERROR"
  fi
unset UPDATER
}

# Execute bash scripts   exec 1>$log_upgrade 2>&1
EXEBASHSCR() {
  log_upgrade "EXEBASHSCR"
  BASHSEXECDEFAULT=".config/xfce4/xfconf/xfce-perchannel-xml/thunar.xml"
  # check defaults
  sleep 2 |zenity --progress --width=400 --title="$APPNAME" --pulsate --auto-close --no-cancel --text="Adding Execute bash scripts feature..."
  grep -q -F 'misc-exec-shell-scripts-by-default' /home/$MYUSER/$BASHSEXECDEFAULT && echo $?
  if [ $? = 0 ]; then
    sed -i 's/.*misc-exec-shell-scripts-by-default.*/  <property name="misc-exec-shell-scripts-by-default" type="bool" value="true"\/>/' /home/$MYUSER/$BASHSEXECDEFAULT
  else # if defaults do not exist then set defaults
    sed -i '/channel>/i \  <property name="misc-exec-shell-scripts-by-default" type="bool" value="true"/>' /home/$MYUSER/$BASHSEXECDEFAULT
  fi
  grep -q -F 'misc-exec-shell-scripts-by-default' /etc/skel/$BASHSEXECDEFAULT && echo $?
  if [ $? = 0 ]; then
    sed -i 's/.*misc-exec-shell-scripts-by-default.*/  <property name="misc-exec-shell-scripts-by-default" type="bool" value="true"\/>/' /etc/skel/$BASHSEXECDEFAULT
  else # if defaults do not exist then set defaults
    sed -i '/channel>/i \  <property name="misc-exec-shell-scripts-by-default" type="bool" value="true"/>' /etc/skel/$BASHSEXECDEFAULT
  fi
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2 ; log_upgrade "EXEBASHSCR - Error"
    zenity --error --width="340" --title="$APPNAME - Error" --text="An error occurred while adding the Execute bash scripts feature.\n\n$REPORTERROR"
  fi
  unset BASHSEXECDEFAULT
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2 ; log_upgrade "EXEBASHSCR - Error"
    zenity --error --width="340" --title="$APPNAME - Error" --text="An error occurred while adding the Execute bash scripts feature.\n\n$REPORTERROR"
  fi
}

# Change Notification theme
NOTIFYTHM() {
  log_upgrade "NOTIFYTHM"
  echo "#⚫ Updating to new Notification theme..."
  cp /home/$MYUSER/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-notifyd.xml /home/$MYUSER/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-notifyd.xml.backup 2>&1 | tee -a $LOGFILE
  chown -R $MYUSER:$MYUSER /home/$MYUSER/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-notifyd.xml.backup 2>&1 | tee -a $LOGFILE
  sed -i '5d' /home/$MYUSER/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-notifyd.xml 2>&1 | tee -a $LOGFILE
  sed -i '5i\ \ <property name="theme" type="string" value="Default"/>' /home/$MYUSER/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-notifyd.xml 2>&1 | tee -a $LOGFILE
  sleep 2 ; if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2 ; log_upgrade "NOTIFYTHM - Error"
    zenity --error --width="340" --title="$APPNAME - Error" --text="An error occurred while changing the Notification theme.\n\n$REPORTERROR"
  fi
}

# Window snapping & tiling
WINSNAPTILE() {
  log_upgrade "WINSNAPTILE"
  XFWM4=".config/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml"
  ( sed -i 's/.*snap_resist.*/    <property name="snap_resist" type="bool" value="true"\/>/' /etc/skel/$XFWM4 2>&1 | tee -a $LOGFILE
  sed -i 's/.*tile_on_move.*/    <property name="tile_on_move" type="bool" value="true"\/>/' /etc/skel/$XFWM4 2>&1 | tee -a $LOGFILE
  sed -i 's/.*wrap_windows.*/    <property name="wrap_windows" type="bool" value="false"\/>/' /etc/skel/$XFWM4 2>&1 | tee -a $LOGFILE
  sed -i 's/.*snap_resist.*/    <property name="snap_resist" type="bool" value="true"\/>/' /home/$MYUSER/$XFWM4 2>&1 | tee -a $LOGFILE
  sed -i 's/.*tile_on_move.*/    <property name="tile_on_move" type="bool" value="true"\/>/' /home/$MYUSER/$XFWM4 2>&1 | tee -a $LOGFILE
  sed -i 's/.*wrap_windows.*/    <property name="wrap_windows" type="bool" value="false"\/>/' /home/$MYUSER/$XFWM4 2>&1 | tee -a $LOGFILE && sleep 2 |
  zenity --progress --width=400 --title="$APPNAME" --percentage=20  --auto-close --no-cancel --text="Setting Window snapping feature...")
  if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2 ; log_upgrade "WINSNAPTILE - Error"
    zenity --error --width="340" --title="$APPNAME - Error" --text="An error occurred while setting the Window snapping feature.\n\n$REPORTERROR"
  fi
unset XFWM4
}

# Modify terminal theme, file is: /home/$MYUSER/.config/lxterminal/lxterminal.conf
TERMTHEME() {
  log_upgrade "TERMTHEME"
  echo "#Updating to new Terminal theme..."
  mv /home/$MYUSER/.config/lxterminal/lxterminal.conf /home/$MYUSER/.config/lxterminal/lxterminal.conf.backup 2>&1 | tee -a $LOGFILE
  touch /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "[general]" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "fontname=Monospace 10" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "selchars=-A-Za-z0-9,./?%&#:_" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "scrollback=100000" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "bgcolor=#1A1E1F" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "bgalpha=65535" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "fgcolor=#f0f0f0" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "disallowbold=false" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "cursorblinks=true" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "cursorunderline=false" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "audiblebell=false" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "tabpos=top" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "hidescrollbar=false" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "hidemenubar=false" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "hideclosebutton=false" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "disablef10=true" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  echo "disablealt=false" >> /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  chown -R $MYUSER:$MYUSER /home/$MYUSER/.config/lxterminal/lxterminal.conf 2>&1 | tee -a $LOGFILE
  sleep 2 ; if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2; log_upgrade "TERMTHEME - Error"
    zenity --error --width="340" --title="$APPNAME - Error" --text="An error occurred while changing the Terminal theme.\n\n$REPORTERROR"
  fi
}

# Change to new HTOP/Resource Usage theme, file is: /home/$MYUSER/.config/htop/htoprc
HTOPTHEME() {
  log_upgrade "HTOPTHEME"
  echo "#⚫ Updating to new Resource Usage theme..."
  touch /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "fields=0 48 17 18 38 39 40 2 46 47 49 1" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "sort_key=46" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "sort_direction=1" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "hide_threads=0" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "hide_kernel_threads=1" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "hide_userland_threads=0" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "shadow_other_users=0" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "show_thread_names=0" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "show_program_path=1" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "highlight_base_name=0" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "highlight_megabytes=1" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "highlight_threads=1" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "tree_view=0" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "header_margin=1" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "detailed_cpu_time=0" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "cpu_count_from_zero=0" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "update_process_names=0" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "account_guest_in_cpu_meter=0" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "color_scheme=5" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "delay=15" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "left_meters=LeftCPUs2 Memory Swap" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "left_meter_modes=1 1 1" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "right_meters=RightCPUs2 Tasks LoadAverage Uptime" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  echo "right_meter_modes=1 2 2 2" >> /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  chown -R $MYUSER:$MYUSER /home/$MYUSER/.config/htop/htoprc 2>&1 | tee -a $LOGFILE
  sleep 2 ; if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2; log_upgrade "HTOPTHEME - Error"
    zenity --error --width="340" --title="$APPNAME - Error" --text="An error occurred while changing the Resource Usage theme.\n\n$REPORTERROR"
  fi
}

# Bluray additional support in VLC - USER
BLURAYVLCUSER() {
  log_upgrade "BLURAYVLCUSER"
  echo "#⚫ Adding additional Bluray support in VLC for users..."
   mkdir -p /home/$MYUSER/.config/aacs ; cd /home/$MYUSER/.config/aacs/ ; wget http://repo.linuxliteos.com/upgrade/3.0-3.6/KEYDB.cfg ; chown -R $MYUSER:$MYUSER /home/$MYUSER/.config/aacs 2>&1 | tee -a $LOGFILE
  sleep 2 ; if [ "${PIPESTATUS[0]}" -ne "0" ]; then echo "#✘ Error..." && sleep 2 ; log_upgrade "BLURAYVLCUSER - Error"
    zenity --error --width="340" --title="$APPNAME - Error" --text="An error occurred while adding additional Bluray support in VLC for users.\n\n$REPORTERROR"
  fi
}

# Set lite-welcome to autostart on first boot
LITEWELCOME() {
  log_upgrade "LITEWELCOME"
  echo "#⚫ Setting Lite Welcome to autostart..." && sleep 2
  if [ ! -f /home/$MYUSER/.config/autostart/lite_welcome.desktop ]; then
  UPDATER=$( ln -s /usr/share/applications/lite_welcome.desktop /home/$MYUSER/.config/autostart 2>&1 | tee -a $LOGFILE )
  else :; fi
unset UPDATER
}

REBOOT() {
  log_upgrade "Waiting for restart confirmation..." && log_upgrade "Moving upgrade log to user's home directory... All done."
    mv -f "$LOGFILE" /home/$MYUSER/ ; chown $MYUSER:$MYUSER "$LOGFILE"
while (true); do
  # Set dialog icon variable
  THEME=$(xfconf-query -c xsettings -p /Net/IconThemeName)
  if [ -f "/home/$USER/.icons/${THEME}/apps/32/system-reboot.png" ]; then APPICON="/home/$USER/.icons/${THEME}"
  elif [ -f "/usr/share/icons/${THEME}/apps/32/system-reboot.png" ]; then APPICON="/usr/share/icons/${THEME}"
  else APPICON="/usr/share/icons/Faenza"
  fi
  title="$APPNAME"
  exit_type=$(zenity --width="340" --height="190" --window-icon="$IC" --cancel-label="Continue without restart" --title="$title" --text='                <span font="Sans Bold 10">Upgrade routine completed</span>\n' \
                     --list --imagelist --print-column=2 --column="  " --column="Action" --column="Description" \
  "$APPICON/apps/32/system-reboot.png" "Restart" "Reboot the computer now" \
  "$APPICON/actions/32/system-shutdown.png" "Shutdown" "Shutdown the computer now")

  if [ $? -eq "1" ]; then zenity --info --title="$APPNAME" --text="\nDon't forget to restart your computer to complete the upgrade."; exit; fi

  # restart and shutdown options
  if [ "$exit_type" = "Restart" ]; then
    dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Restart
  elif [ "$exit_type" = "Shutdown" ]; then
      dbus-send --system --print-reply --dest="org.freedesktop.ConsoleKit" /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop
  else zenity --info --timeout="2" --ok-label="Back" --title="$APPNAME" --text="\nPlease choose one of the available options."; continue
  fi
  done
}

LL30() {
# Invoke arrays
ARRAYA=()       # Array for LL 3.0

# check the arch
log_upgrade "Checking Architecture:"
arch=$(uname -m) ; log_upgrade "$arch"

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)
    ARRAYA+=('MKDIRBUPS30')
    ARRAYA+=('BUPHOMEUSER30')
    ARRAYA+=('ADDLITEWIDG')
    ARRAYA+=('EXEBASHSCR')
    ARRAYA+=('NOTIFYTHM')
    ARRAYA+=('WINSNAPTILE')
    ARRAYA+=('TERMTHEME')
    ARRAYA+=('HTOPTHEME')
    ARRAYA+=('BLURAYVLCUSER')
    ARRAYA+=('LITEWELCOME')
    ;;
    amd64)
    ARRAYA+=('MKDIRBUPS30')
    ARRAYA+=('BUPHOMEUSER30')
    ARRAYA+=('ADDLITEWIDG')
    ARRAYA+=('EXEBASHSCR')
    ARRAYA+=('NOTIFYTHM')
    ARRAYA+=('WINSNAPTILE')
    ARRAYA+=('TERMTHEME')
    ARRAYA+=('HTOPTHEME')
    ARRAYA+=('BLURAYVLCUSER')
    ARRAYA+=('LITEWELCOME')
    ;;
esac
}

LL32() {
# Invoke arrays
ARRAYA=()       # Array for LL 3.2

# check the arch
log_upgrade "Checking Architecture:"
arch=$(uname -m) ; log_upgrade "$arch"

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)
    ARRAYA+=('MKDIRBUPS32')
    ARRAYA+=('BUPHOMEUSER32')
    ARRAYA+=('UPDLITEWIDG')
    ARRAYA+=('EXEBASHSCR')
    ARRAYA+=('NOTIFYTHM')
    ARRAYA+=('WINSNAPTILE')
    ARRAYA+=('TERMTHEME')
    ARRAYA+=('BLURAYVLCUSER')
    ARRAYA+=('LITEWELCOME')
    ;;
    amd64)
    ARRAYA+=('MKDIRBUPS32')
    ARRAYA+=('BUPHOMEUSER32')
    ARRAYA+=('UPDLITEWIDG')
    ARRAYA+=('EXEBASHSCR')
    ARRAYA+=('NOTIFYTHM')
    ARRAYA+=('WINSNAPTILE')
    ARRAYA+=('TERMTHEME')
    ARRAYA+=('BLURAYVLCUSER')
    ARRAYA+=('LITEWELCOME')
    ;;
esac
}

LL34() {
# Invoke arrays
ARRAYA=()       # Array for LL 3.4

# check the arch
log_upgrade "Checking Architecture:"
arch=$(uname -m) ; log_upgrade "$arch"

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)
    ARRAYA+=('MKDIRBUPS34')
    ARRAYA+=('BUPHOMEUSER34')
    ARRAYA+=('BLURAYVLCUSER')
    ARRAYA+=('LITEWELCOME')
    ;;
    amd64)
    ARRAYA+=('MKDIRBUPS34')
    ARRAYA+=('BUPHOMEUSER34')
    ARRAYA+=('BLURAYVLCUSER')
    ARRAYA+=('LITEWELCOME')
    ;;
esac
}

LL36() {
# Invoke arrays
ARRAYA=()       # Array for LL 3.6

# check the arch
log_upgrade "Checking Architecture:"
arch=$(uname -m) ; log_upgrade "$arch"

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)
    ARRAYA+=('MKDIRBUPS36')
    ARRAYA+=('BUPHOMEUSER36')
    ARRAYA+=('LITEWELCOME')
    ;;
    amd64)
    ARRAYA+=('MKDIRBUPS36')
    ARRAYA+=('BUPHOMEUSER36')
    ARRAYA+=('LITEWELCOME')
    ;;
esac
}

RUN() {
  log_upgrade "Executing upgrades..."
# Check if ARRAYA is empty - If it is empty skip the execution
if [ ${#ARRAYA[@]} -ne 0 ]; then
    x=0
    for k in "${!ARRAYA[@]}"; do  x=$(( $x + 1 )); done  # Get the total number of selected items in array A
    TOTAL_LINES=$x
    printf '%s \n' "${ARRAYA[@]}"|
    while read  line; do
      $line     # Execute functions one by one
      if [ $? = 1 ]; then log_upgrade "Error - $line"; zenity --error --title="$APPNAME" --text="\nAn 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

export -f log_upgrade; export MYUSER REPORTERROR UPGR APPNAME LOGFILE IC
/usr/bin/lite-upgrade-system-series3     # Run the next script

if grep "Linux Lite ${UPGR}" /etc/llver; then log_upgrade "Upgrade succeeded."; REBOOT; else log_upgrade "Upgrade failed - /etc/llver is not ${UPGR}"; zenity --info --title="$APPNAME" --text="Upgrade canceled."; fi
}

upgrade_exec() {
# check LL current version and start the appropriate function
log_upgrade "Upgrading version:"
# check if version release exists in /etc/llver. If not, create it from /etc/issue file
if [ "$(grep -c "Linux Lite" /etc/llver)" -ge 1 ]; then :; else grep "Linux Lite" /etc/issue | cut -c 1-14 > /etc/llver; fi
# check LL version and start the appropriate function
if grep "Linux Lite 3.0" /etc/llver; then log_upgrade "Linux Lite 3.0"; LL30
elif grep "Linux Lite 3.2" /etc/llver; then log_upgrade "Linux Lite 3.2"; LL32
elif grep "Linux Lite 3.4" /etc/llver; then log_upgrade "Linux Lite 3.4"; LL34
elif grep "Linux Lite 3.6" /etc/llver; then log_upgrade "Linux Lite 3.6"; LL36
else latest_version
fi
}

# We start here
latest_version() { if grep "Linux Lite 3.8" /etc/llver; then log_upgrade "Linux Lite $UPGR"; zenity --info --text="\nYou are already running the latest release."; exit 0 ; else :; fi }
latest_version
# First dialog box - we must ask for elevation from the start otherwise a regular user can run lite-upgrade-series3 unnecessarily
if [ $EUID -ne 0 ]; then
  zenity --question --icon-name="info" --width="430" --title="$APPNAME" \
         --window-icon="$IC" --cancel-label="Cancel" --ok-label="Upgrade" \
         --text="\nYou are currently running: <b>$GETLLVER</b>\n
You are about to upgrade to: <b>$UPGRFULL</b>\n
Your computer must remain connected to the internet during this time.\n
A connectivity check to the Linux Lite repositories will be conducted to make sure that the connection with the server can be established.\n
Once upgrades have taken place, you must restart the system to complete the upgrade process. Please save and close your work <b>now</b> before continuing.\n\n
Click <b>Cancel</b> to exit now or <b>Upgrade</b> to proceed.\n"
# If No is clicked then exit
if [ "${PIPESTATUS[0]}" -ne "0" ]; then
log_upgrade "user canceled - main dialog"
exit 0
fi

case $? in
    0) log_upgrade "Checking Internet access..."
    if eval "curl -sk google.com" >> /dev/null 2>&1; then
      :; log_upgrade "Internet connection check passed!"
    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."
            log_upgrade "the computer is not connected to the internet - $APPNAME aborted."
            exit 0
    fi
    log_upgrade "Checking connectivity to Master Repository..."
    wget -qnv -t2 -T8 --ignore-length --max-redirect=1  --spider http://repo.linuxliteos.com/linuxlite/db/version
        if [ ${?} -ne 0 ]; then
          log_upgrade "Cannot connect to Linux Lite Master Repository - aborted"
          zenity --error --title="$APPNAME - Aborted" --width="320" --ok-label="Close" \
          --text="\nLinux Lite Upgrade Utility was unable to establish connection with the Linux Lite master repository.\n
Please check your internet connection and try again.\n\nUpgrade has been canceled."
          exit 0
        else
          log_upgrade "Linux Lite Master is ONLINE - passed."; :
        fi
        gksu -m '
    <b>   Lite Upgrade requires Administrative privileges</b>

     Please enter your password to continue.'  $0; exit 0 ; if [ "${PIPESTATUS[@]}" -eq "255" ]; then log_upgrade "Authentication canceled - aborted"; exit 0 ; fi ;;
    1) :;;
    esac
fi

upgrade_exec
RUN

exit 0
