#!/bin/bash

# Kill any notifier(s) running
pkill -f 'python[23]? .*/usr/bin/apt-notifier.py'

ps -o args= $PPID | grep -q /usr/bin/apt-notifier-unhide-Icon
if [ "$?" -eq 0 ]
    then
      :
    else
      sleep .2
fi

# Do a redundant kill of any running notifier(s)
# started from /usr/bin - not used n the current version

if pgrep -f 'python[23]? .*/usr/bin/apt-notifier.py' >/dev/null; then
   pkill -f 'python[23]? .*/usr/bin/apt-notifier.py'
   sleep 0.1
fi

# started from /usr/lib/apt-notifer/modules/apt-notifier.py
if pgrep -f 'python3? .*/usr/lib/apt-notifier/modules/apt-notifier.py' >/dev/null; then
   pkill -f 'python3? .*/usr/lib/apt-notifier/modules/apt-notifier.py'
   sleep .1
fi

# rename user autostart script name
if [ -f ~/.config/autostart/mx-updater-autostart.desktop ]; then
   if [ -f ~/.config/autostart/apt-notifier-autostart.desktop ]; then
      rm ~/.config/autostart/mx-updater-autostart.desktop
   else
      mv ~/.config/autostart/mx-updater-autostart.desktop  ~/.config/autostart/apt-notifier-autostart.desktop
   fi
fi

# Remove any previously created /tmp/tmp****** files owned by current user,
# that have -rw------ permissions, and contain the pattern "sorted_list_of_upgrades()".
# not using those tmp-files  anymore - we keep it
for i in $(cd /tmp; find . -user $USER -perm 600 -regextype posix-egrep -regex '\./tmp[[:print:]]{6}' 2>/dev/null | cut -c3-);
  do
    grep -q "sorted_list_of_upgrades()" /tmp/$i;[ $? -ne 0 ]||rm /tmp/$i
  done

# create the apt-notifierrc file if it doesn't already exist
if ! [ -f ~/.config/apt-notifierrc ];
then
  touch ~/.config/apt-notifierrc
  chmod 644 ~/.config/apt-notifierrc
fi

# Remove unneeded/unused[General] line from apt-notifierrc file if present.
sed -i '/^\[General\].*/Id' ~/.config/apt-notifierrc

# Remove unneeded/unused AutoStart line from apt-notifierrc file if present.
sed -i '/^AutoStart.*/Id' ~/.config/apt-notifierrc

grep -q -e ^UpgradeType=upgrade -e ^UpgradeType=dist-upgrade ~/.config/apt-notifierrc
if [ "$?" -eq 0 ]
  then
  :
  else
  #
  #if a UpgradeType line not present,
  #or not equal to 'upgrade' or 'dist-upgrade'
  #set it to 'UpgradeType=dist-upgrade'
  #
  sed -i '/^UpgradeType/d' ~/.config/apt-notifierrc
  echo -e 'UpgradeType=dist-upgrade\n'>> ~/.config/apt-notifierrc
fi

#test to see if ~/.config/apt-notifierrc contains any blank lines or lines with only whitespace
grep -q ^[[:space:]]*$ ~/.config/apt-notifierrc
if [ "$?" = "0" ]
  then
  #cleanup any blank lines or lines with only whitespace
    sed -i '/^[[:space:]]*$/d' ~/.config/apt-notifierrc
  else
  #no blank lines or lines with only whitespace so do nothing
    :
fi

# create default timouts conf entries
if [ -e /etc/mx-version ]; then
   if ! [ -e ~/.config/MX-Linux/apt-notifier.conf ]; then
      [ -d ~/.config/MX-Linux ] || mkdir -p ~/.config/MX-Linux

      APT_NOTIFIER_UPDATER_CONFIG=/usr/lib/apt-notifier/bin/updater_config
      unset APT_NOTIFIER_TIMEOUT_DEFAULTS
      eval declare -A APT_NOTIFIER_TIMEOUT_DEFAULTS=( $($APT_NOTIFIER_UPDATER_CONFIG --shell 2>/dev/null | grep auto_close_timeout) ) 2>/dev/null
      cat <<DEFAULTS | sed 's/^[[:space:]]*//' > ~/.config/MX-Linux/apt-notifier.conf
      [MX]
       # apt-notifier defaults
       #
       # file: ~/.config/MX-Linux/apt-notifier.conf
       # generated by apt-notifier at $(date)
       #
       # Updater Reload and Upgrade auto close timeouts in seconds
       #
       # Change default values below.
       #
       reload_auto_close_timeout    = ${APT_NOTIFIER_TIMEOUT_DEFAULTS[reload_auto_close_timeout]:-6}
       upgrade_auto_close_timeout   = ${APT_NOTIFIER_TIMEOUT_DEFAULTS[upgrade_auto_close_timeout]:-10}
DEFAULTS
  fi
fi

# start the notifier (nicely)
ionice -c3 nice -n19 /usr/bin/python3 /usr/lib/apt-notifier/modules/apt-notifier.py & disown -h 1>/dev/null 2>&1

# Fix Systray Icons - fehlix's method, not using
#sleep 3
#declare -A B=([true]=false [false]=true); xp() { xfconf-query -c xfce4-panel -p "${@}" ;}; SF=$(xp /plugins  --list | grep -m1 /show-frame 2>/dev/null) || exit 0; xp $SF -n -t bool -s ${B[$(xp $SF)]}; xp $SF -n -t bool -s ${B[$(xp $SF)]};

exit 0

