#! /bin/bash
#--------------------------------------------------------------------------------------------------------
# Name: Lite Updates Notify
# Description: Set an Update frequency notification on Linux Lite.
# Authors:  Jerry Bezencon, Ralphy
# Website: https://www.linuxliteos.com
#--------------------------------------------------------------------------------------------------------

# Ensure multi-language support
export LANG=C
# variables
LUN="Lite Updates Notify"
LUNTMPUSER=$(whoami)
TMPF="/tmp/myuser"
if [ ! -f "$TMPF" ]; then echo "$LUNTMPUSER" > "$TMPF"; chmod 600 "$TMPF"; else :; fi
ic="/usr/share/icons/zenity-llcc.png"
DIR=/var/spool/cron/crontabs
NTFY=/usr/local/sbin/updatesnotify-launcher
file=/etc/sudoers.d/lun
# Check administrative privileges
if [ $EUID -ne 0 ]; then
    yad --image="info" --borders=10 --geometry320x80 --button="Continue":1 --button="Authenticate"!gtk-ok:0 --window-icon="$ic" --title=" $LUN" --fixed --skip-taskbar \
        --text="\nYou need administrative privileges to make changes in\nLite Updates Notify.\n \nClick <b>Continue</b> to proceed without administrative privileges or <b>Authenticate</b> to enter your password next.\n"

   case $? in
    0) pkexec "$0" "$@"; if [ "${PIPESTATUS[@]}" -eq "126" ]; then rm -f "$TMPF"; fi; exit;;
    1) :;;
  252) rm -f "$TMPF"; exit 0
    esac
fi
# Start main dialog loop
while (true); do
LUNUSER=$(cat "$TMPF")
# icons
active=/usr/share/liteappsicons/lite-updatesnotify/clock-active32x32.png
inactive=/usr/share/liteappsicons/lite-updatesnotify/clock-inactive32x32.png
APPICON1=$inactive ; APPICON2=$inactive ; APPICON3=$inactive ; APPICON4=$inactive
APPICON5=$inactive ; APPICON6=$inactive ; APPICON7=$inactive ; APPICON8=$inactive
APPICON9=/usr/share/liteappsicons/lite-updatesnotify/disable-off_32x32.png

if grep -q -Fx '0 * * * * ionice -c2 -p$$ -n 7;nice -n 19 /usr/local/sbin/updatesnotify-launcher >/dev/null 2>&1' $DIR/$LUNUSER; then
  APPICON1=$active
elif grep -q -F '0 */6 * * * ionice -c2 -p$$ -n 7;nice -n 19 /usr/local/sbin/updatesnotify-launcher >/dev/null 2>&1' $DIR/$LUNUSER; then
  APPICON2=$active
elif grep -q -F '0 */12 * * * ionice -c2 -p$$ -n 7;nice -n 19 /usr/local/sbin/updatesnotify-launcher >/dev/null 2>&1' $DIR/$LUNUSER; then
  APPICON3=$active
elif grep -q -F '0 19 * * * ionice -c2 -p$$ -n 7;nice -n 19 /usr/local/sbin/updatesnotify-launcher >/dev/null 2>&1' $DIR/$LUNUSER; then
  APPICON4=$active
elif grep -q -F '0 19 */2 * * ionice -c2 -p$$ -n 7;nice -n 19 /usr/local/sbin/updatesnotify-launcher >/dev/null 2>&1' $DIR/$LUNUSER; then
  APPICON5=$active
elif grep -q -F '0 19 */7 * * ionice -c2 -p$$ -n 7;nice -n 19 /usr/local/sbin/updatesnotify-launcher >/dev/null 2>&1' $DIR/$LUNUSER; then
  APPICON6=$active
elif grep -q -F '0 19 */14 * * ionice -c2 -p$$ -n 7;nice -n 19 /usr/local/sbin/updatesnotify-launcher >/dev/null 2>&1' $DIR/$LUNUSER; then
  APPICON7=$active
elif grep -q -F '0 19 */21 * * ionice -c2 -p$$ -n 7;nice -n 19 /usr/local/sbin/updatesnotify-launcher >/dev/null 2>&1' $DIR/$LUNUSER; then
  APPICON8=$active
fi
if grep -q -Fx '#0 * * * * ionice -c2 -p$$ -n 7;nice -n 19 /usr/local/sbin/updatesnotify-launcher >/dev/null 2>&1' $DIR/$LUNUSER; then
    APPICON9=/usr/share/liteappsicons/litetweaks/appicon3-on.png
  else
    APPICON9=/usr/share/liteappsicons/lite-updatesnotify/disable-off_32x32.png
fi
# check if user's cron file exists... if doesn't exist, create it and set permissions
if [ ! -f "$DIR/$LUNUSER" ]; then
        (umask 177;touch $DIR/$LUNUSER && chown $LUNUSER:crontab $DIR/$LUNUSER)
printf "# DO NOT MANUALLY DELETE OR EDIT THE NEXT 2 LINES
#0 * * * * $LUNUSER ionice -c2 -p\$\$ -n 7;nice -n 19 $NTFY >/dev/null 2>&1
## $LUN - DO NOT MANUALLY DELETE OR EDIT THE ABOVE LINE ######" >> $DIR/$LUNUSER
    else
        if grep -q "$LUN" $DIR/$LUNUSER; then
        :
        else
            chown $LUNUSER:crontab $DIR/$LUNUSER;chmod 600 $DIR/$LUNUSER &&
            printf "# DO NOT MANUALLY DELETE OR EDIT THE NEXT 2 LINES
#0 * * * * $LUNUSER ionice -c2 -p\$\$ -n 7;nice -n 19 $NTFY >/dev/null 2>&1
## $LUN - DO NOT MANUALLY EDIT OR DELETE THIS LINE OR THE ONE ABOVE ######" >> $DIR/$LUNUSER
        fi
fi
# create and configure lun file
if [ ! -f "$file" ]; then
(umask 337;touch $file)
echo "Defaults visiblepw" >> $file
echo "%sudo ALL=(root) NOPASSWD:/usr/local/sbin/updatesnotify, /usr/local/sbin/updatesnotify-launcher" >> $file
fi
# Main dialog - schedules
selected=$(zenity --list --imagelist --separator="," --print-column="2,3" --hide-column=2 --width="550" --height=424 --title="     $LUN" --ok-label="Set" --cancel-label="Quit" --window-icon=$ic \
--text='          <span font="Sans Bold 10">Set the Updates notification frequency from the options below:</span>\n'  \
--column " Schedule " --column "Schedule#" --column "Frequency" --column "Description" \
"$APPICON1" "10" "Hourly" " Once every hour eg. 1pm, 2pm, 3pm etc" \
"$APPICON2" "11" "Every 6 Hours" " First run at midnight, then every 6 hours" \
"$APPICON3" "12" "Every 12 Hours" " First run at midnight, then every 12 hours" \
"$APPICON4" "13" "Daily" "Every day at 7pm" \
"$APPICON5" "14" "Every other day    " " Every other day at 7pm" \
"$APPICON6" "15" "Weekly" " Once every week at 7pm on a Monday" \
"$APPICON7" "16" "Every 2 Weeks" " Once every 2 weeks at 7pm on a Monday" \
"$APPICON8" "17" "Every 3 Weeks" " Once every 3 weeks at 7pm on a Monday" \
"$APPICON9" "20" "Disable                   " " Disable Updates notification")

if [ "$?" -eq "1" ]; then rm -f "$TMPF"; exit 0 ; fi

# read selection and split it
CHFREQ=$(awk -F, '{print $1}' <<<$selected)
dialog=$(awk -F, '{print $2}' <<<$selected)

if [[ $CHFREQ -eq "0" ]]; then
    yad --image="info" --borders=10 --geometry280x50 --button=gtk-ok:0 --window-icon="info" --title=" $LUN" --fixed --text="\nYou did not select a schedule. Please try again."
    continue
fi

if [[ $CHFREQ -eq "20" ]]; then
    yad --image="dialog-question" --borders=10 --geometry320x50 --button=gtk-no:1 --button=gtk-yes:0 --window-icon="dialog-question" --title=" $LUN" --fixed --text="\nAre you sure you want to disable Lite Updates notification?"
    confirm=$?
    if [ "$confirm" -eq "0" ]; then
        rm -f $file
        sed -i -n '1!G;h;$p' $DIR/$LUNUSER && sed -i '/ '"$LUN"' /!b;n;c#0 * * * * ionice -c2 -p\$\$ -n 7;nice -n 19 '"$NTFY"' >/dev/null 2>&1' $DIR/$LUNUSER  && sed -i -n '1!G;h;$p' $DIR/$LUNUSER
            if [ "${PIPESTATUS[0]}" -ne "0" ]; then

            yad --image="dialog-error" --borders=10 --geometry320x50 --button=gtk-no:1 --button=gtk-yes:0 --window-icon="error" --title=" $LUN - Error" --fixed \
                --text="\n<b>An error occurred while disabling updates notification</b> \nYou do not have sufficient permissions to perform this task. \n\nWould you like to authenticate now?\n"
                       elevate=$?
                       if [ "$elevate" -eq "0" ]; then pkexec "$0" "$@"; if [ "${PIPESTATUS[@]}" -eq "126" ]; then rm -f "$TMPF"; fi; exit 0
                        else
                            rm -f "$TMPF"; exit 0
                        fi
                continue
            else
                yad --image="info" --borders=10 --timeout=3 --geometry320x50 --button=gtk-ok:0 --window-icon="info" --title=" $LUN" --fixed --text="\nNotification Updates have been disabled."
                continue
            fi
        elif [ "$confirm" -eq "252" ]; then
            exit 0
        else
            continue
    fi
fi

if [[ $CHFREQ -eq "10" ]]; then
    sed -i -n '1!G;h;$p' $DIR/$LUNUSER && sed -i '/ '"$LUN"' /!b;n;c0 * * * * ionice -c2 -p\$\$ -n 7;nice -n 19 '"$NTFY"' >/dev/null 2>&1' $DIR/$LUNUSER && sed -i -n '1!G;h;$p' $DIR/$LUNUSER
elif [[ $CHFREQ -eq "11" ]]; then
    sed -i -n '1!G;h;$p' $DIR/$LUNUSER && sed -i '/ '"$LUN"' /!b;n;c0 */6 * * * ionice -c2 -p\$\$ -n 7;nice -n 19 '"$NTFY"' >/dev/null 2>&1' $DIR/$LUNUSER && sed -i -n '1!G;h;$p' $DIR/$LUNUSER
elif [[ $CHFREQ -eq "12" ]]; then
    sed -i -n '1!G;h;$p' $DIR/$LUNUSER && sed -i '/ '"$LUN"' /!b;n;c0 */12 * * * ionice -c2 -p\$\$ -n 7;nice -n 19 '"$NTFY"' >/dev/null 2>&1' $DIR/$LUNUSER && sed -i -n '1!G;h;$p' $DIR/$LUNUSER
elif [[ $CHFREQ -eq "13" ]]; then
    sed -i -n '1!G;h;$p' $DIR/$LUNUSER && sed -i '/ '"$LUN"' /!b;n;c0 19 * * * ionice -c2 -p\$\$ -n 7;nice -n 19 '"$NTFY"' >/dev/null 2>&1' $DIR/$LUNUSER && sed -i -n '1!G;h;$p' $DIR/$LUNUSER
elif [[ $CHFREQ -eq "14" ]]; then
    sed -i -n '1!G;h;$p' $DIR/$LUNUSER && sed -i '/ '"$LUN"' /!b;n;c0 19 */2 * * ionice -c2 -p\$\$ -n 7;nice -n 19 '"$NTFY"' >/dev/null 2>&1' $DIR/$LUNUSER && sed -i -n '1!G;h;$p' $DIR/$LUNUSER
elif [[ $CHFREQ -eq "15" ]]; then
    sed -i -n '1!G;h;$p' $DIR/$LUNUSER && sed -i '/ '"$LUN"' /!b;n;c0 19 */7 * 1 ionice -c2 -p\$\$ -n 7;nice -n 19 '"$NTFY"' >/dev/null 2>&1' $DIR/$LUNUSER && sed -i -n '1!G;h;$p' $DIR/$LUNUSER
elif [[ $CHFREQ -eq "16" ]]; then
    sed -i -n '1!G;h;$p' $DIR/$LUNUSER && sed -i '/ '"$LUN"' /!b;n;c0 19 */14 * 1 ionice -c2 -p\$\$ -n 7;nice -n 19 '"$NTFY"' >/dev/null 2>&1' $DIR/$LUNUSER && sed -i -n '1!G;h;$p' $DIR/$LUNUSER
elif [[ $CHFREQ -eq "17" ]]; then
    sed -i -n '1!G;h;$p' $DIR/$LUNUSER && sed -i '/ '"$LUN"' /!b;n;c0 19 */21 * 1 ionice -c2 -p\$\$ -n 7;nice -n 19 '"$NTFY"' >/dev/null 2>&1' $DIR/$LUNUSER && sed -i -n '1!G;h;$p' $DIR/$LUNUSER
fi

if [ "${PIPESTATUS[0]}" -ne "0" ]; then
    yad --image="dialog-error" --borders=10 --geometry320x50 --button=gtk-no:1 --button=gtk-yes:0 --window-icon="error" --title=" $LUN - Error" --fixed \
        --text="\n<b>An error occurred while scheduling updates notification</b> \nYou do not have sufficient permissions to perform this task. \n\nWould you like to authenticate now?\n"
                elevate=$?
                if [ "$elevate" -eq "0" ]; then pkexec "$0" "$@"; if [ "${PIPESTATUS[@]}" -eq "126" ]; then rm -f "$TMPF"; fi; exit 0
                else
                rm -f "$TMPF"; exit 0
            fi
        continue
    else
        service cron reload
        yad --image="info" --borders=10 --timeout=3 --geometry320x50 --button=gtk-ok:0 --window-icon="info" --title="$LUN" --fixed \
            --text="\n Updates notification has been successfully scheduled.\n\n Frequency:<b> $dialog</b>"
        continue
fi
done
exit 0
