#!/bin/bash

#=========================================#
# Simple update notifier CLI (DNF) v0.3.8 #
#=========================================#

ICON='/usr/share/icons/hicolor/48x48/apps/simple-notifier.png'
USER=$(who |sed 's/ .*//'|head -n1)
# Run as root
# Warn if dnf is running
RUN=$(ps -ef | grep "dnf" | grep -v grep | wc -l)
if [[ "$RUN" -gt 0 ]]; then
su "$USER" -c "/usr/share/simple-notifier/warning.py"
exit 0
else
sleep 0.25
USER=$(who |sed 's/ .*//'|head -n1)
CONF="/home/"$USER"/.config/simple-notifier"
if ! test -d "$CONF"; then
mkdir -p "$CONF"
chmod 0777 "$CONF"
touch $CONF/history
chown -R "$USER":"$USER" "$CONF"
fi 
echo "__________________________________________________________________" >> "$CONF"/history
date | tee >> "$CONF"/history
cat /var/tmp/.simple-notifier/update_info | awk '{print}' >> "$CONF"/history
chown -R "$USER":"$USER" "$CONF"/history
sleep 0.25
fi
while true; do 
echo "#Updating system packages... Please wait!"  2>/dev/null
sleep 0.1
done | $(yad --window-icon="$ICON" --image="$ICON" --on-top --width=500 \
--height=100 --fixed --center --progress --pulsate --title="Simple Update Notifier" \
--text=$"\nRetrieving Packages and Updating System" \
--button=Close!/usr/share/simple-notifier/icons/exit.png --auto-close) &
dnf -y upgrade --refresh &>/dev/null
pgrep -n yad > /var/tmp/pid
sleep 0.50
PID=`cat /var/tmp/pid | awk '{print $0}'`
kill -9 $PID > /dev/null 2>&1 
rm -f /var/tmp/.simple-notifier/update_info
touch /var/tmp/.simple-notifier/update_info
sleep 1
echo "0" > /var/tmp/.simple-notifier/num_updates
echo "0" > /var/tmp/.simple-notifier/up2date
exit 0






