#!/bin/bash
#=========================================#
# Simple update notifier (DNF) v0.3.9     #
#=========================================#

CONF="/usr/share/simple-notifier/scripts/update-check"
USER=$(who |sed 's/ .*//'|head -n1)
SET_CHECK=$(grep -e "sleep" /usr/share/simple-notifier/scripts/update-check|tail -n1 | \
awk '{
    gsub("m","",$2)
    if ($2 == 0) {
        print "Disabled"
    } else {
        h=$2/60
        print (h==1 ? "1 hour" : h " hours")
    }
}')

action=$(yad \
--width=425 \
--height=100 --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="Current Update Check Set at 👉️ $SET_CHECK\nChange Simple Update Notifier Options" \
'Check updates every 1 hour' 'Check updates every 2 hours' 'Check updates every 4 hours' '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
RUN=$(ps -ef | grep "dnf" | grep -v grep | wc -l)
if [[ "$RUN" -gt 0 ]]; then
/usr/share/simple-notifier/warning.py
exit
 fi
  fi
if [ -n "${action}" ]; then
case $action in
'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

