#!/bin/bash
#=========================================#
# Simple update notifier (DNF) v0.3.8     #
#=========================================#
CONF="/usr/share/simple-notifier/scripts/update-check"
action=$(yad \
--width=425 \
--height=75 --fixed --on-top \
--image=/usr/share/simple-notifier/icons/simple-notifier.png \
--button="Cancel"!/usr/share/simple-notifier/icons/exit.png:1 \
--button="Save"!/usr/share/simple-notifier/icons/ok.svg:0 \
--center \
--window-icon=/usr/share/simple-notifier/icons/simple-notifier16.png \
--title='Update Settings' \
--entry \
--text=$'Setup Simple Update Notifier Options' \
'Refresh updates and quit' 'Check updates every 1 hour' 'Check updates every 2 hours' 'Check updates every 4 hours' 'Disable all update notifications')
if [ -n "${action}" ]; then
case $action in
'Refresh updates and quit')
RUN=$(ps -ef | grep "dnf" | grep -v grep | wc -l)
if [[ "$RUN" -gt 0 ]]; then
/usr/share/simple-notifier/warning.py
exit 0
else
script_dir="$( cd "$( dirname "$0" )" && pwd )"
script="`basename $0`"
script_path_n_name="${script_dir}/${script}"
GUI_SU="pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY"
if [ "$UID" != "0" ]; then
/usr/share/simple-notifier/refresh.py &
RUN=$(ps -ef | grep "dnf" | grep -v grep | wc -l)
if [[ "$RUN" -gt 0 ]]; then
/usr/share/simple-notifier/warning.py
exit
else
# File paths
UPDATE_FILE="/var/tmp/.simple-notifier/num_updates"
TMP_FILE="/var/tmp/.simple-notifier/update_info"

# Function to check updates
    run_check() {
        dnf -q check-update --refresh 2>/dev/null | awk -v update_file="$UPDATE_FILE" -v tmp_file="$TMP_FILE" '
        BEGIN {
            c=0
            # Truncate the update file at start
            close(update_file)
        }
        /^Obsoleting/ {in_obsolete=1; next}

        # Normal updates → show + count
        !in_obsolete && NF > 1 && /^[[:alnum:]]/ {
            print >> update_file
            c++
            next
        }

        # Obsoleting section
        in_obsolete && NF > 2 && /^[[:alnum:]]/ {
            print >> update_file
            if ($3 != "<unknown>") { c++ }
        }

        END {
            print c > tmp_file
        }'
        cp -r $TMP_FILE $UPDATE_FILE
        chmod 0777 "$UPDATE_FILE" "$TMP_FILE"
    }
run_check
fi
fi
 fi
exit 0
 ;;
'Check updates every 1 hour')
script_dir="$( cd "$( dirname "$0" )" && pwd )"
script="`basename $0`"
script_path_n_name="${script_dir}/${script}"
GUI_SU="pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY"
$GUI_SU /usr/share/simple-notifier/scripts/1_hour; exit;
 ;;
'Check updates every 2 hours')
script_dir="$( cd "$( dirname "$0" )" && pwd )"
script="`basename $0`"
script_path_n_name="${script_dir}/${script}"
GUI_SU="pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY"
$GUI_SU /usr/share/simple-notifier/scripts/2_hours; exit;
 ;;
'Check updates every 4 hours')
script_dir="$( cd "$( dirname "$0" )" && pwd )"
script="`basename $0`"
script_path_n_name="${script_dir}/${script}"
GUI_SU="pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY"
$GUI_SU /usr/share/simple-notifier/scripts/4_hours; exit;
 ;;
'Disable all update notifications')
script_dir="$( cd "$( dirname "$0" )" && pwd )"
script="`basename $0`"
script_path_n_name="${script_dir}/${script}"
GUI_SU="pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY"
if [ "$UID" != "0" ]; then
$GUI_SU /usr/share/simple-notifier/scripts/disable; exit;
fi
 ;;
esac
fi

