#!/bin/bash

TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=yad-autoremove

touch ~/.loggy
log="/tmp/$(date +%s)"

### These 2 lines of code check for internet connectivity 
# adapted from https://unix.stackexchange.com/questions/190513/shell-scripting-proper-way-to-check-for-internet-connectivity
if nc -zw1 google.com 443; then
  echo $"Internet connection detected"

### Check if user is Root, if not, pop up window asking for password, to run updater. If Cancelled, exit.
# adapted from  https://stackoverflow.com/questions/42875809/checking-sudo-in-bash-script-with-if-statements 
if [[ "$EUID" = 0 ]]; then
    echo $"already root"
else
    gksudo "antiX autoremove"
    if sudo true; then
        echo $"You are Root or running the script in sudo mode"
    else
        echo $"You entered the wrong password or you cancelled"
        exit 1
    fi
fi

env LANGUAGE=en sudo apt update 2> "${log}" |
while read -r line; do echo "# ${line}"; echo " ${line}" > ~/.loggy ; done |
    yad --progress --pulsate --center --no-buttons --auto-close --progress-text=$"Waiting for a Network connection..." --width=500 --height=100 --title=$"antiX - autoremove" 
string=$(cat ~/.loggy)
 for reqsubstr in 'up to date';do
  if [ -z "${string##*$reqsubstr*}" ] ;then
      x-terminal-emulator -T $"antiX - autoremove" -e /bin/bash -c "sudo apt autoremove -V && sleep 0.1 && yad --center --width=250 --text-align=center  --text=$'Finished' --button='OK':1 --title=$'antiX - autoremove'"
   fi
    done

### These 2 lines of code  end the part of the script that checks for internet connectivity
  else  yad --center --width=250 --text-align=center  --text=$"No Internet connection detected!" --title=$'antiX - autoremove' --button='OK':1 
  
fi